Search in sources :

Example 86 with InvalidSyntaxException

use of org.osgi.framework.InvalidSyntaxException in project aries by apache.

the class AriesApplicationManagerImpl method install.

public AriesApplicationContext install(AriesApplication app) throws BundleException, ManagementException, ResolverException {
    if (!app.isResolved()) {
        app = resolve(app);
    }
    // Register an Application Repository for this application if none exists
    String appScope = app.getApplicationMetadata().getApplicationScope();
    ServiceReference[] ref = null;
    try {
        String filter = "(" + BundleRepository.REPOSITORY_SCOPE + "=" + appScope + ")";
        ref = _bundleContext.getServiceReferences(BundleRepository.class.getName(), filter);
    } catch (InvalidSyntaxException e) {
    // Something went wrong attempting to find a service so we will act as if 
    // there is no existing service.
    }
    if (ref == null || ref.length == 0) {
        Dictionary dict = new Hashtable();
        dict.put(BundleRepository.REPOSITORY_SCOPE, appScope);
        ServiceRegistration serviceReg = _bundleContext.registerService(BundleRepository.class.getName(), new ApplicationRepository(app), dict);
        serviceRegistrations.put(app, serviceReg);
    }
    AriesApplicationContext result = _applicationContextManager.getApplicationContext(app);
    // When installing bundles in the .eba file we use the jar url scheme. This results in a
    // JarFile being held open, which is bad as on windows we cannot delete the .eba file
    // so as a work around we open a url connection to one of the bundles in the eba and
    // if it is a jar url we close the associated JarFile.
    Iterator<BundleInfo> bi = app.getBundleInfo().iterator();
    if (bi.hasNext()) {
        String location = bi.next().getLocation();
        if (location.startsWith("jar")) {
            try {
                URL url = new URL(location);
                JarURLConnection urlc = (JarURLConnection) url.openConnection();
                // Make sure that we pick up the cached version rather than creating a new one
                urlc.setUseCaches(true);
                urlc.getJarFile().close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    return result;
}
Also used : Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable) JarURLConnection(java.net.JarURLConnection) ApplicationRepository(org.apache.aries.application.management.repository.ApplicationRepository) IOException(java.io.IOException) BundleRepository(org.apache.aries.application.management.spi.repository.BundleRepository) URL(java.net.URL) ServiceReference(org.osgi.framework.ServiceReference) BundleInfo(org.apache.aries.application.management.BundleInfo) SimpleBundleInfo(org.apache.aries.application.utils.management.SimpleBundleInfo) AriesApplicationContext(org.apache.aries.application.management.AriesApplicationContext) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 87 with InvalidSyntaxException

use of org.osgi.framework.InvalidSyntaxException in project aries by apache.

the class OBRAriesResolver method getBundleInfo.

@Override
public BundleInfo getBundleInfo(String bundleSymbolicName, Version bundleVersion) {
    Map<String, String> attribs = new HashMap<String, String>();
    // bundleVersion is an exact version - so ensure right version filter is generated
    VersionRange range = ManifestHeaderProcessor.parseVersionRange(bundleVersion.toString(), true);
    attribs.put(Resource.VERSION, range.toString());
    String filterString = ManifestHeaderProcessor.generateFilter(Resource.SYMBOLIC_NAME, bundleSymbolicName, attribs);
    Resource[] resources;
    try {
        resources = repositoryAdmin.discoverResources(filterString);
        if (resources != null && resources.length > 0) {
            return toBundleInfo(resources[0], false);
        } else {
            return null;
        }
    } catch (InvalidSyntaxException e) {
        log.error("Invalid filter", e);
        return null;
    }
}
Also used : HashMap(java.util.HashMap) ModelledBundleResource(org.apache.aries.application.resolver.obr.ext.ModelledBundleResource) ModelledResource(org.apache.aries.application.modelling.ModelledResource) Resource(org.apache.felix.bundlerepository.Resource) VersionRange(org.apache.aries.util.VersionRange) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException)

Example 88 with InvalidSyntaxException

use of org.osgi.framework.InvalidSyntaxException in project aries by apache.

the class CmUtils method callPlugins.

private static void callPlugins(final BundleContext bundleContext, final Dictionary<String, Object> props, final ServiceReference sr, final String configPid, final String factoryPid) {
    ServiceReference[] plugins = null;
    try {
        final String targetPid = (factoryPid == null) ? configPid : factoryPid;
        String filter = "(|(!(cm.target=*))(cm.target=" + targetPid + "))";
        plugins = bundleContext.getServiceReferences(ConfigurationPlugin.class.getName(), filter);
    } catch (InvalidSyntaxException ise) {
    // no filter, no exception ...
    }
    // abort early if there are no plugins
    if (plugins == null || plugins.length == 0) {
        return;
    }
    // sort the plugins by their service.cmRanking
    if (plugins.length > 1) {
        Arrays.sort(plugins, CM_RANKING);
    }
    // call the plugins in order
    for (ServiceReference pluginRef : plugins) {
        ConfigurationPlugin plugin = (ConfigurationPlugin) bundleContext.getService(pluginRef);
        if (plugin != null) {
            try {
                plugin.modifyConfiguration(sr, props);
            } catch (Throwable t) {
            // Ignore
            } finally {
                // ensure ungetting the plugin
                bundleContext.ungetService(pluginRef);
            }
            setAutoProperties(props, configPid, factoryPid);
        }
    }
}
Also used : ConfigurationPlugin(org.osgi.service.cm.ConfigurationPlugin) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Example 89 with InvalidSyntaxException

use of org.osgi.framework.InvalidSyntaxException in project aries by apache.

the class Helper method getOsgiService.

public static <T> T getOsgiService(BundleContext bundleContext, 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 = 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 + ", bundle: " + ref.getBundle() + ", symbolicName: " + ref.getBundle().getSymbolicName());
            }
            for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, flt))) {
                System.err.println("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 90 with InvalidSyntaxException

use of org.osgi.framework.InvalidSyntaxException in project aries by apache.

the class Helper method getOsgiServiceReference.

public static <T> ServiceReference getOsgiServiceReference(BundleContext bundleContext, 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 = 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 + ", bundle: " + ref.getBundle() + ", symbolicName: " + ref.getBundle().getSymbolicName());
            }
            for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, flt))) {
                System.err.println("Filtered ServiceReference: " + ref + ", bundle: " + ref.getBundle() + ", symbolicName: " + ref.getBundle().getSymbolicName());
            }
            throw new RuntimeException("Gave up waiting for service " + flt);
        }
        return tracker.getServiceReference();
    } 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)

Aggregations

InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)124 ServiceReference (org.osgi.framework.ServiceReference)59 Filter (org.osgi.framework.Filter)29 ArrayList (java.util.ArrayList)27 IOException (java.io.IOException)23 BundleContext (org.osgi.framework.BundleContext)21 HashMap (java.util.HashMap)17 ServiceTracker (org.osgi.util.tracker.ServiceTracker)14 BundleException (org.osgi.framework.BundleException)12 Configuration (org.osgi.service.cm.Configuration)12 Map (java.util.Map)11 Dictionary (java.util.Dictionary)9 Hashtable (java.util.Hashtable)9 Test (org.junit.Test)9 List (java.util.List)8 File (java.io.File)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)7 URL (java.net.URL)6 LinkedHashMap (java.util.LinkedHashMap)6 FilterImpl (org.eclipse.osgi.internal.framework.FilterImpl)6