blob: 08ea1b3f78b2bddad26eac32307d5f5ac2d65bd2 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.aries.component.dsl.internal;
import org.apache.aries.component.dsl.OSGi;
import org.apache.aries.component.dsl.OSGiRunnable;
import org.apache.aries.component.dsl.Publisher;
import org.apache.aries.component.dsl.OSGiResult;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Filter;
import org.osgi.framework.InvalidSyntaxException;
/**
* @author Carlos Sierra Andrés
*/
public class OSGiImpl<T> implements OSGi<T> {
protected OSGiImpl(OSGiRunnable<T> operation) {
_operation = operation;
}
public static <T> OSGi<T> create(OSGiRunnable<T> runnable) {
return new OSGiImpl<>(
(ec, op) -> new OSGiResultImpl(runnable.run(ec, op)));
}
@Override
public OSGiResult run(ExecutionContext executionContext) {
return run(executionContext, x -> NOOP);
}
public OSGiResult run(
ExecutionContext executionContext, Publisher<? super T> op) {
return _operation.run(executionContext, op);
}
static Filter buildFilter(
ExecutionContext executionContext, String filterString, Class<?> clazz) {
Filter filter;
String string = buildFilterString(filterString, clazz);
try {
filter = executionContext.getBundleContext().createFilter(string);
}
catch (InvalidSyntaxException e) {
throw new RuntimeException(e);
}
return filter;
}
static String buildFilterString(String filterString, Class<?> clazz) {
if (filterString == null && clazz == null) {
throw new IllegalArgumentException(
"Both filterString and clazz can't be null");
}
StringBuilder stringBuilder = new StringBuilder();
if (filterString != null) {
stringBuilder.append(filterString);
}
if (clazz != null) {
boolean extend = !(stringBuilder.length() == 0);
if (extend) {
stringBuilder.insert(0, "(&");
}
stringBuilder.
append("(objectClass=").
append(clazz.getName()).
append(")");
if (extend) {
stringBuilder.append(")");
}
}
return stringBuilder.toString();
}
OSGiRunnable<T> _operation;
}