Search in sources :

Example 26 with InvalidSyntaxException

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

the class HealthCheckExecutorImpl method activate.

@Activate
protected final void activate(final Map<String, Object> properties, final BundleContext bundleContext) {
    this.bundleContext = bundleContext;
    final ModifiableThreadPoolConfig hcThreadPoolConfig = new ModifiableThreadPoolConfig();
    hcThreadPoolConfig.setMaxPoolSize(25);
    hcThreadPool = threadPoolManager.create(hcThreadPoolConfig, "Health Check Thread Pool");
    this.modified(properties);
    try {
        this.bundleContext.addServiceListener(this, "(" + Constants.OBJECTCLASS + "=" + HealthCheck.class.getName() + ")");
    } catch (final InvalidSyntaxException ise) {
        // this should really never happen as the expression above is constant
        throw new RuntimeException("Unexpected exception occured.", ise);
    }
}
Also used : HealthCheck(org.apache.sling.hc.api.HealthCheck) ModifiableThreadPoolConfig(org.apache.sling.commons.threads.ModifiableThreadPoolConfig) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Activate(org.apache.felix.scr.annotations.Activate)

Example 27 with InvalidSyntaxException

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

the class OSGiServiceInjector method getService.

@SuppressWarnings("unchecked")
private <T> Object getService(Object adaptable, Class<T> type, String filter, DisposalCallbackRegistry callbackRegistry) {
    // cannot use SlingScriptHelper since it does not support ordering by service ranking due to https://issues.apache.org/jira/browse/SLING-5665
    try {
        ServiceReference[] refs = bundleContext.getServiceReferences(type.getName(), filter);
        if (refs == null || refs.length == 0) {
            return null;
        } else {
            // sort by service ranking (lowest first) (see ServiceReference.compareTo)
            List<ServiceReference> references = Arrays.asList(refs);
            Collections.sort(references);
            callbackRegistry.addDisposalCallback(new Callback(refs, bundleContext));
            return bundleContext.getService(references.get(references.size() - 1));
        }
    } catch (InvalidSyntaxException e) {
        log.error("invalid filter expression", e);
        return null;
    }
}
Also used : DisposalCallback(org.apache.sling.models.spi.DisposalCallback) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Example 28 with InvalidSyntaxException

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

the class AbstractServiceReferenceRecipe method start.

public void start(SatisfactionListener listener) {
    if (listener == null)
        throw new NullPointerException("satisfactionListener is null");
    if (started.compareAndSet(false, true)) {
        try {
            satisfactionListener = listener;
            satisfied.set(optional);
            // though this may not be sufficient because we don't control ordering of those events
            synchronized (tracked) {
                getBundleContextForServiceLookup().addServiceListener(this, getOsgiFilter());
                ServiceReference[] references = getBundleContextForServiceLookup().getServiceReferences((String) null, getOsgiFilter());
                tracked.setInitial(references != null ? references : new ServiceReference[0]);
            }
            tracked.trackInitial();
            satisfied.set(optional || !tracked.isEmpty());
            retrack();
            LOGGER.debug("Found initial references {} for OSGi service {}", getServiceReferences(), getOsgiFilter());
        } catch (InvalidSyntaxException e) {
            throw new ComponentDefinitionException(e);
        }
    }
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Example 29 with InvalidSyntaxException

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

the class DSLTest method testConfigurationsAndRegistrations.

@Test
public void testConfigurationsAndRegistrations() throws InvalidSyntaxException, IOException, InterruptedException {
    ServiceReference<ConfigurationAdmin> serviceReference = bundleContext.getServiceReference(ConfigurationAdmin.class);
    ConfigurationAdmin configurationAdmin = bundleContext.getService(serviceReference);
    /*  For each factory configuration register a service with the property
            key set to the value of the property key that comes with the
            configuration */
    OSGi<ServiceRegistration<Service>> program = configurations("test.configuration").map(d -> d.get("key")).flatMap(key -> register(Service.class, new Service(), new HashMap<String, Object>() {

        {
            put("key", key);
        }
    }));
    OSGiResult<ServiceRegistration<Service>> result = program.run(bundleContext);
    assertEquals(0, bundleContext.getServiceReferences(Service.class, "(test.configuration=*)").size());
    CountDownLatch addedLatch = new CountDownLatch(3);
    ServiceRegistration<?> addedServiceRegistration = bundleContext.registerService(ManagedServiceFactory.class, new ManagedServiceFactory() {

        @Override
        public String getName() {
            return "";
        }

        @Override
        public void updated(String s, Dictionary<String, ?> dictionary) throws ConfigurationException {
            addedLatch.countDown();
        }

        @Override
        public void deleted(String s) {
        }
    }, new Hashtable<String, Object>() {

        {
            put("service.pid", "test.configuration");
        }
    });
    CountDownLatch deletedLatch = new CountDownLatch(3);
    ServiceRegistration<?> deletedServiceRegistration = bundleContext.registerService(ManagedServiceFactory.class, new ManagedServiceFactory() {

        @Override
        public String getName() {
            return "";
        }

        @Override
        public void updated(String s, Dictionary<String, ?> dictionary) throws ConfigurationException {
        }

        @Override
        public void deleted(String s) {
            deletedLatch.countDown();
        }
    }, new Hashtable<String, Object>() {

        {
            put("service.pid", "test.configuration");
        }
    });
    Configuration configuration = configurationAdmin.createFactoryConfiguration("test.configuration");
    configuration.update(new Hashtable<String, Object>() {

        {
            put("key", "service one");
        }
    });
    Configuration configuration2 = configurationAdmin.createFactoryConfiguration("test.configuration");
    configuration2.update(new Hashtable<String, Object>() {

        {
            put("key", "service two");
        }
    });
    Configuration configuration3 = configurationAdmin.createFactoryConfiguration("test.configuration");
    configuration3.update(new Hashtable<String, Object>() {

        {
            put("key", "service three");
        }
    });
    assertTrue(addedLatch.await(10, TimeUnit.SECONDS));
    assertEquals(1, bundleContext.getServiceReferences(Service.class, "(key=service one)").size());
    assertEquals(1, bundleContext.getServiceReferences(Service.class, "(key=service two)").size());
    assertEquals(1, bundleContext.getServiceReferences(Service.class, "(key=service three)").size());
    configuration3.delete();
    configuration2.delete();
    configuration.delete();
    assertTrue(deletedLatch.await(10, TimeUnit.SECONDS));
    assertEquals(0, bundleContext.getServiceReferences(Service.class, "(test.configuration=*)").size());
    addedServiceRegistration.unregister();
    deletedServiceRegistration.unregister();
    result.close();
    bundleContext.ungetService(serviceReference);
}
Also used : OSGi.configurations(org.apache.aries.osgi.functional.OSGi.configurations) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) OSGi.onClose(org.apache.aries.osgi.functional.OSGi.onClose) OSGi.register(org.apache.aries.osgi.functional.OSGi.register) HighestRankingRouter.highest(org.apache.aries.osgi.functional.test.HighestRankingRouter.highest) Configuration(org.osgi.service.cm.Configuration) ConfigurationException(org.osgi.service.cm.ConfigurationException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OSGi.configuration(org.apache.aries.osgi.functional.OSGi.configuration) ServiceReference(org.osgi.framework.ServiceReference) Hashtable(java.util.Hashtable) ServiceRegistration(org.osgi.framework.ServiceRegistration) ManagedServiceFactory(org.osgi.service.cm.ManagedServiceFactory) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) OSGi.serviceReferences(org.apache.aries.osgi.functional.OSGi.serviceReferences) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) BundleContext(org.osgi.framework.BundleContext) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) OSGi.just(org.apache.aries.osgi.functional.OSGi.just) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) OSGi.services(org.apache.aries.osgi.functional.OSGi.services) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) OSGi(org.apache.aries.osgi.functional.OSGi) FrameworkUtil(org.osgi.framework.FrameworkUtil) OSGiResult(org.apache.aries.osgi.functional.OSGiResult) Dictionary(java.util.Dictionary) Assert.assertEquals(org.junit.Assert.assertEquals) Configuration(org.osgi.service.cm.Configuration) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedServiceFactory(org.osgi.service.cm.ManagedServiceFactory) ConfigurationException(org.osgi.service.cm.ConfigurationException) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 30 with InvalidSyntaxException

use of org.osgi.framework.InvalidSyntaxException in project ddf by codice.

the class CatalogBundle method waitForSource.

private <T extends Source> T waitForSource(String id, Class<T> type) throws InterruptedException, InvalidSyntaxException {
    T source = null;
    long timeoutLimit = System.currentTimeMillis() + CATALOG_PROVIDER_TIMEOUT;
    boolean available = false;
    while (!available) {
        ServiceReference<CatalogFramework> frameworkRef = serviceManager.getServiceReference(CatalogFramework.class);
        CatalogFramework framework = null;
        if (frameworkRef != null) {
            framework = serviceManager.getService(frameworkRef);
        }
        if (source == null) {
            source = serviceManager.getServiceReferences(type, null).stream().map(serviceManager::getService).filter(src -> id.equals(src.getId())).findFirst().orElse(null);
        }
        if (source != null && framework != null) {
            SourceInfoRequestEnterprise sourceInfoRequestEnterprise = new SourceInfoRequestEnterprise(true);
            try {
                SourceInfoResponse sources = framework.getSourceInfo(sourceInfoRequestEnterprise);
                Set<SourceDescriptor> sourceInfo = sources.getSourceInfo();
                for (SourceDescriptor sourceDescriptor : sourceInfo) {
                    if (sourceDescriptor.getSourceId().equals(source.getId())) {
                        available = sourceDescriptor.isAvailable() && source.isAvailable();
                        LOGGER.info("Source.isAvailable = {} Framework.isAvailable = {}", source.isAvailable(), sourceDescriptor.isAvailable());
                    }
                }
            } catch (SourceUnavailableException e) {
                available = false;
            }
        } else {
            LOGGER.info("Currently no source of type {} and name {} could be found", type.getName(), id);
        }
        if (!available) {
            if (System.currentTimeMillis() > timeoutLimit) {
                fail("Source (" + id + ") was not created in a timely manner.");
            }
            Thread.sleep(1000);
        }
    }
    LOGGER.info("Source {} is available.", id);
    return source;
}
Also used : SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) Logger(org.slf4j.Logger) SourceInfoRequestLocal(ddf.catalog.operation.impl.SourceInfoRequestLocal) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) CatalogStore(ddf.catalog.source.CatalogStore) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) CatalogFramework(ddf.catalog.CatalogFramework) FederatedSource(ddf.catalog.source.FederatedSource) LoggerFactory(org.slf4j.LoggerFactory) SourceInfoRequestEnterprise(ddf.catalog.operation.impl.SourceInfoRequestEnterprise) Set(java.util.Set) IOException(java.io.IOException) TimeUnit(java.util.concurrent.TimeUnit) Source(ddf.catalog.source.Source) SourceDescriptor(ddf.catalog.source.SourceDescriptor) List(java.util.List) Configuration(org.osgi.service.cm.Configuration) CatalogProvider(ddf.catalog.source.CatalogProvider) Map(java.util.Map) Optional(java.util.Optional) Assert.fail(org.junit.Assert.fail) Hashtable(java.util.Hashtable) ServiceReference(org.osgi.framework.ServiceReference) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) SourceDescriptor(ddf.catalog.source.SourceDescriptor) CatalogFramework(ddf.catalog.CatalogFramework) SourceInfoRequestEnterprise(ddf.catalog.operation.impl.SourceInfoRequestEnterprise) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse)

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