Search in sources :

Example 21 with InvalidSyntaxException

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

the class EncryptablePropertyPlaceholderTest method getOsgiService.

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 22 with InvalidSyntaxException

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

the class BulkRequestContext method newContext.

public static BulkRequestContext newContext(ConfigurationAdmin configAdmin) throws IOException {
    BulkRequestContext context = new BulkRequestContext();
    context.configAdmin = configAdmin;
    try {
        // check JAAS subject here
        AccessControlContext acc = AccessController.getContext();
        if (acc == null) {
            context.anonymous = true;
        } else {
            Subject subject = Subject.getSubject(acc);
            if (subject == null) {
                context.anonymous = true;
            } else {
                context.principals.addAll(subject.getPrincipals());
            }
        }
        // list available ACL configs - valid for this instance only
        for (Configuration config : configAdmin.listConfigurations("(service.pid=jmx.acl*)")) {
            context.allPids.add(config.getPid());
        }
        // list available ACT whitelist configs
        Configuration[] configs = configAdmin.listConfigurations("(service.pid=jmx.acl.whitelist)");
        if (configs != null) {
            for (Configuration config : configs) {
                context.whiteListProperties.add(config.getProperties());
            }
        }
    } catch (InvalidSyntaxException ise) {
        throw new RuntimeException(ise);
    }
    return context;
}
Also used : AccessControlContext(java.security.AccessControlContext) Configuration(org.osgi.service.cm.Configuration) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Subject(javax.security.auth.Subject)

Example 23 with InvalidSyntaxException

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

the class Wait method execute.

@Override
public Object execute() throws Exception {
    ServiceTracker<?, ?> tracker = null;
    try {
        String filter = service;
        if (!filter.startsWith("(")) {
            if (!filter.contains("=")) {
                filter = Constants.OBJECTCLASS + "=" + filter;
            }
            filter = "(" + filter + ")";
        }
        Filter osgiFilter = FrameworkUtil.createFilter(filter);
        tracker = new ServiceTracker<>(bundleContext, osgiFilter, null);
        tracker.open(true);
        Object svc = tracker.getService();
        if (timeout >= 0) {
            svc = tracker.waitForService(timeout);
        }
        if (exception && svc == null) {
            throw new TimeoutException("Can not find service '" + service + "' in the OSGi registry");
        }
        return svc != null;
    } catch (InvalidSyntaxException e) {
        throw new IllegalArgumentException("Invalid filter", e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    } finally {
        if (tracker != null) {
            tracker.close();
        }
    }
}
Also used : Filter(org.osgi.framework.Filter) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) TimeoutException(java.util.concurrent.TimeoutException)

Example 24 with InvalidSyntaxException

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

the class FeatureConfigInstaller method installFeatureConfigs.

public void installFeatureConfigs(Feature feature) throws IOException, InvalidSyntaxException {
    for (ConfigInfo config : feature.getConfigurations()) {
        TypedProperties props = new TypedProperties();
        // trim lines
        String val = config.getValue();
        if (config.isExternal()) {
            props.load(new URL(val));
        } else {
            props.load(new StringReader(val));
        }
        String[] pid = parsePid(config.getName());
        Configuration cfg = findExistingConfiguration(configAdmin, pid[0], pid[1]);
        if (cfg == null) {
            Dictionary<String, Object> cfgProps = convertToDict(props);
            cfg = createConfiguration(configAdmin, pid[0], pid[1]);
            String key = createConfigurationKey(pid[0], pid[1]);
            cfgProps.put(CONFIG_KEY, key);
            cfg.update(cfgProps);
            try {
                updateStorage(pid[0], pid[1], props, false);
            } catch (Exception e) {
                LOGGER.warn("Can't update cfg file", e);
            }
        } else if (config.isAppend()) {
            boolean update = false;
            Dictionary<String, Object> properties = cfg.getProperties();
            for (String key : props.keySet()) {
                if (properties.get(key) == null) {
                    properties.put(key, props.get(key));
                    update = true;
                }
            }
            if (update) {
                cfg.update(properties);
                try {
                    updateStorage(pid[0], pid[1], props, true);
                } catch (Exception e) {
                    LOGGER.warn("Can't update cfg file", e);
                }
            }
        }
    }
    for (ConfigFileInfo configFile : feature.getConfigurationFiles()) {
        installConfigurationFile(configFile.getLocation(), configFile.getFinalname(), configFile.isOverride());
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) StringReader(java.io.StringReader) ConfigFileInfo(org.apache.karaf.features.ConfigFileInfo) ConfigInfo(org.apache.karaf.features.ConfigInfo) TypedProperties(org.apache.felix.utils.properties.TypedProperties) URL(java.net.URL) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 25 with InvalidSyntaxException

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

the class AbstractJobHandlingTest method cleanup.

public void cleanup() {
    // clean job area
    final ServiceReference<ResourceResolverFactory> ref = this.bc.getServiceReference(ResourceResolverFactory.class);
    final ResourceResolverFactory factory = this.bc.getService(ref);
    ResourceResolver resolver = null;
    try {
        resolver = factory.getAdministrativeResourceResolver(null);
        final Resource rsrc = resolver.getResource("/var/eventing");
        if (rsrc != null) {
            delete(rsrc);
            resolver.commit();
        }
    } catch (final LoginException le) {
    // ignore
    } catch (final PersistenceException e) {
    // ignore
    } catch (final Exception e) {
    // sometimes an NPE is thrown from the repository, as we
    // are in the cleanup, we can ignore this
    } finally {
        if (resolver != null) {
            resolver.close();
        }
    }
    // unregister all services
    for (final ServiceRegistration<?> reg : this.registrations) {
        reg.unregister();
    }
    this.registrations.clear();
    // remove all configurations
    try {
        final org.osgi.service.cm.Configuration[] cfgs = this.configAdmin.listConfigurations(null);
        if (cfgs != null) {
            for (final org.osgi.service.cm.Configuration c : cfgs) {
                try {
                    c.delete();
                } catch (final IOException io) {
                // ignore
                }
            }
        }
    } catch (final IOException io) {
    // ignore
    } catch (final InvalidSyntaxException e) {
    // ignore
    }
    this.sleep(1000);
}
Also used : Configuration(org.ops4j.pax.exam.Configuration) JobManagerConfiguration(org.apache.sling.event.impl.jobs.config.JobManagerConfiguration) Resource(org.apache.sling.api.resource.Resource) IOException(java.io.IOException) PersistenceException(org.apache.sling.api.resource.PersistenceException) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) LoginException(org.apache.sling.api.resource.LoginException) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) PersistenceException(org.apache.sling.api.resource.PersistenceException) LoginException(org.apache.sling.api.resource.LoginException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException)

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