use of org.osgi.framework.InvalidSyntaxException in project cucumber-jvm by cucumber.
the class OsgiObjectFactory method getService.
private Object getService(Class<?> type, long timeout, String filter) {
final String serviceName = type.getName() + " " + filter;
try {
final long tryUntil = System.currentTimeMillis() + timeout;
Object service = null;
search: while (service == null) {
final Collection<?> serviceReferences = bundleContext.getServiceReferences(type, filter.isEmpty() ? null : filter);
for (Object serviceReference : serviceReferences) {
service = bundleContext.getService((ServiceReference<?>) serviceReference);
break search;
}
if (tryUntil < System.currentTimeMillis())
break;
Thread.sleep(20);
}
if (service == null)
throw new ServiceNotFoundException(serviceName);
return service;
} catch (InvalidSyntaxException e) {
throw new IllegalArgumentException(e);
} catch (InterruptedException e) {
throw new TimeoutException(serviceName);
}
}
use of org.osgi.framework.InvalidSyntaxException in project camel by apache.
the class Activator method updateAvailableScriptLanguages.
private void updateAvailableScriptLanguages() {
ServiceReference<LanguageResolver> ref = null;
try {
Collection<ServiceReference<LanguageResolver>> references = context.getServiceReferences(LanguageResolver.class, "(resolver=default)");
if (references.size() == 1) {
// Unregistry the old language resolver first
if (registration != null) {
registration.unregister();
registration = null;
}
ref = references.iterator().next();
LanguageResolver resolver = context.getService(ref);
Dictionary props = new Hashtable();
// Just publish the language resolve with the language we found
props.put("language", getAvailableScriptNames());
registration = context.registerService(LanguageResolver.class, resolver, props);
}
} catch (InvalidSyntaxException e) {
LOG.error("Invalid syntax for LanguageResolver service reference filter.");
} finally {
if (ref != null) {
context.ungetService(ref);
}
}
}
use of org.osgi.framework.InvalidSyntaxException in project camel by apache.
the class AbstractFeatureTest method getOsgiService.
@SuppressWarnings("unchecked")
public static <T> T getOsgiService(BundleContext bundleContext, Class<T> type, String filter, long timeout) {
ServiceTracker tracker;
try {
String flt;
if (filter != null) {
if (filter.startsWith("(")) {
flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")" + filter + ")";
} else {
flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")(" + filter + "))";
}
} else {
flt = "(" + Constants.OBJECTCLASS + "=" + type.getName() + ")";
}
Filter osgiFilter = FrameworkUtil.createFilter(flt);
tracker = new ServiceTracker(bundleContext, osgiFilter, null);
tracker.open(true);
// Note that the tracker is not closed to keep the reference
// This is buggy, as the service reference may change i think
Object svc = tracker.waitForService(timeout);
if (svc == null) {
Dictionary<?, ?> dic = bundleContext.getBundle().getHeaders();
LOG.warn("Test bundle headers: " + explode(dic));
for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, null))) {
LOG.warn("ServiceReference: " + ref + ", bundle: " + ref.getBundle() + ", symbolicName: " + ref.getBundle().getSymbolicName());
}
for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, flt))) {
LOG.warn("Filtered ServiceReference: " + ref + ", bundle: " + ref.getBundle() + ", symbolicName: " + ref.getBundle().getSymbolicName());
}
throw new RuntimeException("Gave up waiting for service " + flt);
}
return type.cast(svc);
} catch (InvalidSyntaxException e) {
throw new IllegalArgumentException("Invalid filter", e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
use of org.osgi.framework.InvalidSyntaxException in project camel by apache.
the class CamelKarafTestSupport method getOsgiService.
@SuppressWarnings("unchecked")
protected <T> T getOsgiService(Class<T> type, String filter, long timeout) {
ServiceTracker tracker = null;
try {
String flt;
if (filter != null) {
if (filter.startsWith("(")) {
flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")" + filter + ")";
} else {
flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")(" + filter + "))";
}
} else {
flt = "(" + Constants.OBJECTCLASS + "=" + type.getName() + ")";
}
Filter osgiFilter = FrameworkUtil.createFilter(flt);
tracker = new ServiceTracker(bundleContext, osgiFilter, null);
tracker.open(true);
// Note that the tracker is not closed to keep the reference
// This is buggy, as the service reference may change i think
Object svc = type.cast(tracker.waitForService(timeout));
if (svc == null) {
Dictionary dic = bundleContext.getBundle().getHeaders();
System.err.println("Test bundle headers: " + explode(dic));
for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, null))) {
System.err.println("ServiceReference: " + ref);
}
for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, flt))) {
System.err.println("Filtered ServiceReference: " + ref);
}
throw new RuntimeException("Gave up waiting for service " + flt);
}
return type.cast(svc);
} catch (InvalidSyntaxException e) {
throw new IllegalArgumentException("Invalid filter", e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
use of org.osgi.framework.InvalidSyntaxException in project aries by apache.
the class ApplicationMetadataImpl method setup.
/**
* setup the application metadata from the appManifest
* @param appManifest application.mf manifest
*/
private void setup(Manifest appManifest) {
Map<String, String> appMap = readManifestIntoMap(appManifest);
// configure the appSymbolicName and appVersion
this.appSymbolicName = appMap.get(AppConstants.APPLICATION_SYMBOLIC_NAME);
this.appVersion = new Version(appMap.get(AppConstants.APPLICATION_VERSION));
this.appName = appMap.get(AppConstants.APPLICATION_NAME);
this.appScope = this.appSymbolicName + "_" + this.appVersion.toString();
if (this.appSymbolicName == null || this.appVersion == null) {
throw new IllegalArgumentException("Failed to create ApplicationMetadataImpl object from Manifest " + appManifest);
}
// configure appContents
// use parseImportString as we don't allow appContents to be duplicate
String applicationContents = appMap.get(AppConstants.APPLICATION_CONTENT);
Map<String, Map<String, String>> appContentsMap = ManifestHeaderProcessor.parseImportString(applicationContents);
for (Map.Entry<String, Map<String, String>> e : appContentsMap.entrySet()) {
this.appContents.add(new ContentImpl(e.getKey(), e.getValue()));
}
String useBundleStr = appMap.get(AppConstants.APPLICATION_USE_BUNDLE);
if (useBundleStr != null) {
Map<String, Map<String, String>> useBundleMap = ManifestHeaderProcessor.parseImportString(useBundleStr);
for (Map.Entry<String, Map<String, String>> e : useBundleMap.entrySet()) {
this.useBundle.add(new ContentImpl(e.getKey(), e.getValue()));
}
}
String allServiceImports = appMap.get(AppConstants.APPLICATION_IMPORT_SERVICE);
List<String> serviceImports = ManifestHeaderProcessor.split(allServiceImports, ",");
for (String s : serviceImports) {
try {
ServiceDeclaration dec = new ServiceDeclarationImpl(s);
importServices.add(dec);
} catch (InvalidSyntaxException ise) {
_logger.warn("APPUTILS0013E", new Object[] { s, appSymbolicName });
}
}
String allServiceExports = appMap.get(AppConstants.APPLICATION_EXPORT_SERVICE);
List<String> serviceExports = ManifestHeaderProcessor.split(allServiceExports, ",");
for (String s : serviceExports) {
try {
ServiceDeclaration dec = new ServiceDeclarationImpl(s);
exportServices.add(dec);
} catch (InvalidSyntaxException ise) {
_logger.warn("APPUTILS0014E", new Object[] { s, appSymbolicName });
}
}
}
Aggregations