Search in sources :

Example 1 with InvalidSyntaxException

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);
    }
}
Also used : ServiceNotFoundException(cucumber.api.osgi.ServiceNotFoundException) Collection(java.util.Collection) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) TimeoutException(cucumber.api.osgi.TimeoutException)

Example 2 with InvalidSyntaxException

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);
        }
    }
}
Also used : Dictionary(java.util.Dictionary) LanguageResolver(org.apache.camel.spi.LanguageResolver) Hashtable(java.util.Hashtable) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Example 3 with InvalidSyntaxException

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);
    }
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) Filter(org.osgi.framework.Filter) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Example 4 with InvalidSyntaxException

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);
    }
}
Also used : Dictionary(java.util.Dictionary) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Filter(org.osgi.framework.Filter) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Example 5 with InvalidSyntaxException

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 });
        }
    }
}
Also used : ServiceDeclaration(org.apache.aries.application.ServiceDeclaration) Version(org.osgi.framework.Version) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)105 ServiceReference (org.osgi.framework.ServiceReference)54 Filter (org.osgi.framework.Filter)26 ArrayList (java.util.ArrayList)22 IOException (java.io.IOException)20 BundleContext (org.osgi.framework.BundleContext)16 ServiceTracker (org.osgi.util.tracker.ServiceTracker)14 HashMap (java.util.HashMap)12 Configuration (org.osgi.service.cm.Configuration)12 Map (java.util.Map)10 Test (org.junit.Test)9 Dictionary (java.util.Dictionary)8 Hashtable (java.util.Hashtable)8 List (java.util.List)6 ConfigurationException (org.osgi.service.cm.ConfigurationException)6 Metacard (ddf.catalog.data.Metacard)4 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)4 CatalogFramework (ddf.catalog.CatalogFramework)3 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)3 InputTransformer (ddf.catalog.transform.InputTransformer)3