Search in sources :

Example 86 with ServiceReference

use of org.osgi.framework.ServiceReference in project felix by apache.

the class JettyService method start.

public void start() throws Exception {
    // FELIX-4422: start Jetty synchronously...
    startJetty();
    if (this.registerManagedService) {
        final Dictionary<String, Object> props = new Hashtable<>();
        props.put(Constants.SERVICE_PID, PID);
        this.configServiceReg = this.context.registerService("org.osgi.service.cm.ManagedService", new JettyManagedService(this), props);
    }
    this.eventAdmintTracker = new ServiceTracker<>(this.context, EventAdmin.class, new ServiceTrackerCustomizer<EventAdmin, EventAdmin>() {

        @Override
        public EventAdmin addingService(final ServiceReference<EventAdmin> reference) {
            EventAdmin service = context.getService(reference);
            modifiedService(reference, service);
            return service;
        }

        @Override
        public void modifiedService(final ServiceReference<EventAdmin> reference, final EventAdmin service) {
            eventAdmin = service;
        }

        @Override
        public void removedService(final ServiceReference<EventAdmin> reference, final EventAdmin service) {
            context.ungetService(reference);
            eventAdmin = null;
        }
    });
    this.eventAdmintTracker.open();
    this.bundleTracker = new BundleTracker<>(this.context, Bundle.ACTIVE | Bundle.STARTING, new BundleTrackerCustomizer<Deployment>() {

        @Override
        public Deployment addingBundle(Bundle bundle, BundleEvent event) {
            return detectWebAppBundle(bundle);
        }

        @Override
        public void modifiedBundle(Bundle bundle, BundleEvent event, Deployment object) {
            detectWebAppBundle(bundle);
        }

        private Deployment detectWebAppBundle(Bundle bundle) {
            if (bundle.getState() == Bundle.ACTIVE || (bundle.getState() == Bundle.STARTING && "Lazy".equals(bundle.getHeaders().get(HEADER_ACTIVATION_POLICY)))) {
                String contextPath = bundle.getHeaders().get(HEADER_WEB_CONTEXT_PATH);
                if (contextPath != null) {
                    return startWebAppBundle(bundle, contextPath);
                }
            }
            return null;
        }

        @Override
        public void removedBundle(Bundle bundle, BundleEvent event, Deployment object) {
            String contextPath = bundle.getHeaders().get(HEADER_WEB_CONTEXT_PATH);
            if (contextPath == null) {
                return;
            }
            Deployment deployment = deployments.remove(contextPath);
            if (deployment != null && deployment.getContext() != null) {
                // remove registration, since bundle is already stopping
                deployment.setRegistration(null);
                undeploy(deployment, deployment.getContext());
            }
        }
    });
    this.bundleTracker.open();
}
Also used : EventAdmin(org.osgi.service.event.EventAdmin) Hashtable(java.util.Hashtable) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) Bundle(org.osgi.framework.Bundle) BundleTrackerCustomizer(org.osgi.util.tracker.BundleTrackerCustomizer) ServiceReference(org.osgi.framework.ServiceReference) BundleEvent(org.osgi.framework.BundleEvent)

Example 87 with ServiceReference

use of org.osgi.framework.ServiceReference in project felix by apache.

the class DefaultImplementationTest method testDefaultImplementationTimeoutWithProxy.

@Test
public void testDefaultImplementationTimeoutWithProxy() {
    String prov = "provider";
    ComponentInstance provider = ipojoHelper.createComponentInstance("TEMPORAL-FooProvider", prov);
    String un = "under-1";
    ComponentInstance under = ipojoHelper.createComponentInstance("TEMPORAL-DIProxiedCheckServiceProvider", un);
    ServiceReference ref_fs = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), prov);
    assertNotNull("Check foo availability", ref_fs);
    ServiceReference ref_cs = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), un);
    assertNotNull("Check cs availability", ref_cs);
    CheckService cs = (CheckService) osgiHelper.getServiceObject(ref_cs);
    assertTrue("Check invocation", cs.check());
    // Stop the provider.
    provider.stop();
    ref_cs = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), un);
    assertNotNull("Check cs availability - 2", ref_cs);
    cs = (CheckService) osgiHelper.getServiceObject(ref_cs);
    boolean res = false;
    try {
        res = cs.check();
    } catch (RuntimeException e) {
        fail("A nullable was expected ...");
    }
    assertFalse("Check nullable", res);
    provider.stop();
    provider.dispose();
    under.stop();
    under.dispose();
    return;
}
Also used : FooService(org.apache.felix.ipojo.handler.temporal.services.FooService) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) CheckService(org.apache.felix.ipojo.handler.temporal.services.CheckService) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 88 with ServiceReference

use of org.osgi.framework.ServiceReference in project felix by apache.

the class DefaultImplementationTest method testDelayTimeout.

@Test
public void testDelayTimeout() {
    String prov = "provider";
    ComponentInstance provider = ipojoHelper.createComponentInstance("TEMPORAL-FooProvider", prov);
    String un = "under-1";
    ComponentInstance under = ipojoHelper.createComponentInstance("TEMPORAL-DICheckServiceProviderTimeout", un);
    ServiceReference ref_fs = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), prov);
    assertNotNull("Check foo availability", ref_fs);
    ServiceReference ref_cs = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), un);
    assertNotNull("Check cs availability", ref_cs);
    CheckService cs = (CheckService) osgiHelper.getServiceObject(ref_cs);
    assertTrue("Check invocation", cs.check());
    // Stop the provider.
    provider.stop();
    ref_cs = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), un);
    assertNotNull("Check cs availability - 2", ref_cs);
    long begin = System.currentTimeMillis();
    DelayedProvider dp = new DelayedProvider(provider, 200);
    dp.start();
    cs = (CheckService) osgiHelper.getServiceObject(ref_cs);
    assertTrue("Check invocation - 2", cs.check());
    long end = System.currentTimeMillis();
    assertTrue("Assert delay", (end - begin) >= 200);
    ref_cs = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), un);
    assertNotNull("Check cs availability - 3", ref_cs);
    cs = (CheckService) osgiHelper.getServiceObject(ref_cs);
    assertTrue("Check invocation - 3", cs.check());
    provider.stop();
    provider.dispose();
    under.stop();
    under.dispose();
}
Also used : FooService(org.apache.felix.ipojo.handler.temporal.services.FooService) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) CheckService(org.apache.felix.ipojo.handler.temporal.services.CheckService) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 89 with ServiceReference

use of org.osgi.framework.ServiceReference in project felix by apache.

the class DefaultImplementationTest method testDelayOnMultipleDependency.

@Test
public void testDelayOnMultipleDependency() {
    String prov = "provider";
    ComponentInstance provider1 = ipojoHelper.createComponentInstance("TEMPORAL-FooProvider", prov);
    String prov2 = "provider2";
    ComponentInstance provider2 = ipojoHelper.createComponentInstance("TEMPORAL-FooProvider", prov2);
    String un = "under-1";
    ComponentInstance under = ipojoHelper.createComponentInstance("TEMPORAL-NullableMultipleCheckServiceProviderTimeout", un);
    ServiceReference ref_fs = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), prov);
    assertNotNull("Check foo availability", ref_fs);
    ServiceReference ref_cs = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), un);
    assertNotNull("Check cs availability", ref_cs);
    CheckService cs = (CheckService) osgiHelper.getServiceObject(ref_cs);
    assertTrue("Check invocation", cs.check());
    // Stop the providers.
    provider1.stop();
    provider2.stop();
    ref_cs = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), un);
    assertNotNull("Check cs availability - 2", ref_cs);
    long begin = System.currentTimeMillis();
    DelayedProvider dp = new DelayedProvider(provider1, 1500);
    DelayedProvider dp2 = new DelayedProvider(provider2, 100);
    dp.start();
    dp2.start();
    cs = (CheckService) osgiHelper.getServiceObject(ref_cs);
    assertTrue("Check invocation - 2", cs.check());
    long end = System.currentTimeMillis();
    System.out.println("delay = " + (end - begin));
    assertTrue("Assert min delay", (end - begin) >= 100);
    assertTrue("Assert max delay", (end - begin) <= 1000);
    dp.stop();
    dp2.stop();
    provider1.stop();
    provider2.stop();
    ref_cs = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), un);
    assertNotNull("Check cs availability - 3", ref_cs);
    cs = (CheckService) osgiHelper.getServiceObject(ref_cs);
    // Will return false as the contained DI will return false to the foo method.
    assertFalse("Check invocation - 3", cs.check());
    provider1.dispose();
    provider2.dispose();
    under.stop();
    under.dispose();
}
Also used : FooService(org.apache.felix.ipojo.handler.temporal.services.FooService) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) CheckService(org.apache.felix.ipojo.handler.temporal.services.CheckService) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 90 with ServiceReference

use of org.osgi.framework.ServiceReference in project felix by apache.

the class DefaultImplementationTest method testDefaultImplementation.

@Test
public void testDefaultImplementation() {
    String prov = "provider";
    ComponentInstance provider = ipojoHelper.createComponentInstance("TEMPORAL-FooProvider", prov);
    String un = "under-1";
    ComponentInstance under = ipojoHelper.createComponentInstance("TEMPORAL-DICheckServiceProvider", un);
    ServiceReference ref_fs = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), prov);
    assertNotNull("Check foo availability", ref_fs);
    ServiceReference ref_cs = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), un);
    assertNotNull("Check cs availability", ref_cs);
    CheckService cs = (CheckService) osgiHelper.getServiceObject(ref_cs);
    assertTrue("Check invocation", cs.check());
    // Stop the provider.
    provider.stop();
    ref_cs = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), un);
    assertNotNull("Check cs availability - 2", ref_cs);
    long begin = System.currentTimeMillis();
    DelayedProvider dp = new DelayedProvider(provider, 200);
    dp.start();
    cs = (CheckService) osgiHelper.getServiceObject(ref_cs);
    assertTrue("Check invocation - 2", cs.check());
    long end = System.currentTimeMillis();
    assertTrue("Assert delay", (end - begin) >= 200);
    ref_cs = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), un);
    assertNotNull("Check cs availability - 3", ref_cs);
    cs = (CheckService) osgiHelper.getServiceObject(ref_cs);
    assertTrue("Check invocation - 3", cs.check());
    provider.stop();
    provider.dispose();
    under.stop();
    under.dispose();
}
Also used : FooService(org.apache.felix.ipojo.handler.temporal.services.FooService) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) CheckService(org.apache.felix.ipojo.handler.temporal.services.CheckService) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

ServiceReference (org.osgi.framework.ServiceReference)1690 Test (org.junit.Test)926 Properties (java.util.Properties)396 Architecture (org.apache.felix.ipojo.architecture.Architecture)263 CheckService (org.apache.felix.ipojo.runtime.core.test.services.CheckService)233 BundleContext (org.osgi.framework.BundleContext)231 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)227 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)215 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)183 ArrayList (java.util.ArrayList)167 Bundle (org.osgi.framework.Bundle)144 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)141 Hashtable (java.util.Hashtable)124 IOException (java.io.IOException)107 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)92 Dictionary (java.util.Dictionary)82 Configuration (org.osgi.service.cm.Configuration)74 CheckService (org.apache.felix.ipojo.handler.temporal.services.CheckService)70 FooService (org.apache.felix.ipojo.handler.temporal.services.FooService)70 CheckService (org.apache.felix.ipojo.handler.transaction.services.CheckService)65