Search in sources :

Example 1 with BundleTracker

use of org.osgi.util.tracker.BundleTracker in project aries by apache.

the class BaseActivator method start.

public synchronized void start(BundleContext context, final String consumerHeaderName) throws Exception {
    bundleContext = context;
    providerBundleTracker = new BundleTracker(context, Bundle.ACTIVE, new ProviderBundleTrackerCustomizer(this, context.getBundle()));
    providerBundleTracker.open();
    consumerBundleTracker = new BundleTracker(context, Bundle.INSTALLED | Bundle.RESOLVED | Bundle.STARTING | Bundle.ACTIVE, new ConsumerBundleTrackerCustomizer(this, consumerHeaderName));
    consumerBundleTracker.open();
    for (Bundle bundle : context.getBundles()) {
        addConsumerWeavingData(bundle, consumerHeaderName);
    }
    activator = this;
}
Also used : Bundle(org.osgi.framework.Bundle) BundleTracker(org.osgi.util.tracker.BundleTracker)

Example 2 with BundleTracker

use of org.osgi.util.tracker.BundleTracker in project aries by apache.

the class ScopeAdminTest method testBundleServiceIsolation.

@Test
public void testBundleServiceIsolation() throws Exception {
    // make sure we are using a framework that provides composite admin service
    assertNotNull("scope admin should not be null", scope);
    System.out.println("able to get scope admin service");
    bt = new BundleTracker(bundleContext, Bundle.INSTALLED | Bundle.UNINSTALLED | Bundle.ACTIVE, new BundleTrackerCustomizer() {

        public synchronized Object addingBundle(Bundle bundle, BundleEvent event) {
            if (event == null) {
                System.out.println("ScopeAdminTest - adding Bundle: " + bundle.getSymbolicName() + " event: null");
            } else {
                System.out.println("ScopeAdminTest - adding Bundle: " + bundle.getSymbolicName() + " event: " + event.getType());
                addEventCount++;
            }
            return bundle;
        }

        public synchronized void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
            if (event == null) {
                System.out.println("ScopeAdminTest - modifying Bundle: " + bundle.getSymbolicName() + " event: null");
            } else {
                System.out.println("ScopeAdminTest - modifying Bundle: " + bundle.getSymbolicName() + " event: " + event.getType());
                modifyEventCount++;
            }
        }

        public synchronized void removedBundle(Bundle bundle, BundleEvent event, Object object) {
            if (event == null) {
                System.out.println("ScopeAdminTest - removing Bundle: " + bundle.getSymbolicName() + " event: null");
            } else {
                System.out.println("ScopeAdminTest - removing Bundle: " + bundle.getSymbolicName() + " event: " + event.getType());
                removeEventCount++;
            }
        }
    });
    bt.open();
    ScopeUpdate su = scope.newScopeUpdate();
    ScopeUpdate childScopeUpdate = su.newChild("scope_test1");
    su.getChildren().add(childScopeUpdate);
    addPackageImportPolicy("org.osgi.framework", childScopeUpdate);
    addPackageImportPolicy("org.osgi.util.tracker", childScopeUpdate);
    // build up installInfo object for the scope
    InstallInfo info1 = new InstallInfo("helloIsolation", new URL("mvn:org.apache.aries.subsystem/org.apache.aries.subsystem.example.helloIsolation/0.1-SNAPSHOT"));
    InstallInfo info2 = new InstallInfo("helloIsolationRef", new URL("mvn:org.apache.aries.subsystem/org.apache.aries.subsystem.example.helloIsolationRef/0.1-SNAPSHOT"));
    List<InstallInfo> bundlesToInstall = childScopeUpdate.getBundlesToInstall();
    bundlesToInstall.add(info1);
    bundlesToInstall.add(info2);
    // add bundles to be installed, based on subsystem content
    su.commit();
    assertEquals("add event count should be 0 since 0 bundles are installed in root scope", 0, addEventCount);
    assertEquals("modify event count should be 0", 0, modifyEventCount);
    assertEquals("remove event count should be 0", 0, removeEventCount);
    // start all bundles in the scope scope_test1
    Collection<Bundle> bundlesToStart = childScopeUpdate.getScope().getBundles();
    for (Bundle b : bundlesToStart) {
        b.start();
    }
    assertEquals("add event count should be 0 since 0 bundles are installed in root scope", 0, addEventCount);
    assertEquals("modify event count should be 0", 0, modifyEventCount);
    assertEquals("remove event count should be 0", 0, removeEventCount);
    try {
        ServiceReference sr = bundleContext.getServiceReference(HelloIsolation.class.getName());
        fail("should not be able to get the sr for HelloIsolation service");
    } catch (Exception ex) {
    // expected 
    } catch (Error er) {
    // expected
    }
    // test bundle find hooks
    Bundle[] bundles = bundleContext.getBundles();
    for (Bundle b : bundles) {
        System.out.println("Bundle is " + b.getBundleId() + ": " + b.getSymbolicName());
        if (b.getSymbolicName().indexOf("org.apache.aries.subsystem.example.helloIsolation") > -1) {
            fail("bundles with name starts with org.apache.aries.subsystem.example.helloIsolation should be in a different scope");
        }
    }
    // test bundle service find hook
    //ServiceReference sr = bundleContext.getServiceReference(HelloIsolation.class.getName());
    //assertNull("sr should be null", sr);
    Collection<Scope> children = scope.getChildren();
    assertEquals(1, children.size());
    for (Scope child : children) {
        if (child.getName().equals("scope_test1")) {
            Collection<Bundle> buns = child.getBundles();
            assertEquals(2, buns.size());
            assertEquals(0, child.getChildren().size());
            for (Bundle b : buns) {
                assertTrue(b.getSymbolicName().indexOf("org.apache.aries.subsystem.example.helloIsolation") > -1);
            }
        }
    }
    // install a test bundle in the root scope
    URL url = new URL("mvn:org.apache.felix/org.apache.felix.fileinstall/2.0.8");
    bundleContext.installBundle("org.apache.felix.fileinstall-rootScope", url.openStream());
    assertEquals("add event count should be 1 since 1 bundles are installed", 1, addEventCount);
    assertEquals("modify event count should be 0", 0, modifyEventCount);
    assertEquals("remove event count should be 0", 0, removeEventCount);
    // remove child scope
    su = scope.newScopeUpdate();
    //        Collection<Scope> scopes = su.getToBeRemovedChildren();
    Collection<ScopeUpdate> scopes = su.getChildren();
    childScopeUpdate = scopes.iterator().next();
    // obtain child scope admin from service registry
    //        String filter = "ScopeName=scope_test1";
    //        Scope childscope = getOsgiService(Scope.class, filter, DEFAULT_TIMEOUT);
    Scope childScopeAdmin = childScopeUpdate.getScope();
    assertEquals(scope, childScopeAdmin.getParent());
    //        scopes.add(childScopeAdmin);
    scopes.remove(childScopeUpdate);
    su.commit();
    assertFalse(scope.getChildren().contains(childScopeAdmin));
    su = scope.newScopeUpdate();
    assertFalse(su.getChildren().contains(childScopeUpdate));
//        childScopeAdmin = null;
//        try {
//            childScopeAdmin = getOsgiService(Scope.class, filter, DEFAULT_TIMEOUT);
//        } catch (Exception ex) {
//            // ignore
//        }
//        assertNull("scope admin service for the scope should be unregistered", childScopeAdmin);
}
Also used : InstallInfo(org.apache.aries.subsystem.scope.InstallInfo) Bundle(org.osgi.framework.Bundle) BundleTracker(org.osgi.util.tracker.BundleTracker) ScopeUpdate(org.apache.aries.subsystem.scope.ScopeUpdate) BundleTrackerCustomizer(org.osgi.util.tracker.BundleTrackerCustomizer) URL(java.net.URL) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference) HelloIsolation(org.apache.aries.subsystem.example.helloIsolation.HelloIsolation) Scope(org.apache.aries.subsystem.scope.Scope) BundleEvent(org.osgi.framework.BundleEvent) Test(org.junit.Test)

Example 3 with BundleTracker

use of org.osgi.util.tracker.BundleTracker in project aries by apache.

the class BundleTrackerFactory method unregisterAndCloseBundleTracker.

/**
     * unregister and close the bundle tracker(s) associated with composite
     * bundle's - SymbolicName_Version
     * 
     * @param bundleScope
     *            composite bundle's - SymbolicName_Version
     */
public static void unregisterAndCloseBundleTracker(String bundleScope) {
    List<BundleTracker> list = btMap.get(bundleScope);
    if (list == null) {
        return;
    } else {
        for (BundleTracker bt : list) {
            bt.close();
        }
    }
    btMap.remove(bundleScope);
}
Also used : BundleTracker(org.osgi.util.tracker.BundleTracker)

Example 4 with BundleTracker

use of org.osgi.util.tracker.BundleTracker in project sling by apache.

the class Activator method start.

public void start(BundleContext context) throws Exception {
    commandMap = new OsgiMailcapCommandMap();
    for (Bundle bundle : context.getBundles()) registerBundleMailcapEntries(bundle);
    CommandMap.setDefaultCommandMap(commandMap);
    bundleTracker = new BundleTracker(context, Bundle.ACTIVE | Bundle.UNINSTALLED | Bundle.STOP_TRANSIENT, new BundleTrackerCustomizer() {

        public void removedBundle(Bundle bundle, BundleEvent event, Object object) {
            unregisterBundleMailcapEntries(bundle);
        }

        public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
            unregisterBundleMailcapEntries(bundle);
            registerBundleMailcapEntries(bundle);
        }

        public Object addingBundle(Bundle bundle, BundleEvent event) {
            registerBundleMailcapEntries(bundle);
            return bundle;
        }
    });
    bundleTracker.open();
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent) BundleTracker(org.osgi.util.tracker.BundleTracker) BundleTrackerCustomizer(org.osgi.util.tracker.BundleTrackerCustomizer)

Example 5 with BundleTracker

use of org.osgi.util.tracker.BundleTracker in project aries by apache.

the class BlueprintExtender method start.

public void start(BundleContext ctx) {
    LOGGER.debug("Starting blueprint extender...");
    this.context = ctx;
    boolean useSystemContext = Boolean.parseBoolean(ctx.getProperty("org.apache.aries.blueprint.use.system.context"));
    BundleContext trackingContext = useSystemContext ? ctx.getBundle(Constants.SYSTEM_BUNDLE_LOCATION).getBundleContext() : ctx;
    handlers = new NamespaceHandlerRegistryImpl(trackingContext);
    executors = new ScheduledExecutorServiceWrapper(ctx, "Blueprint Extender", new ScheduledExecutorServiceFactory() {

        public ScheduledExecutorService create(String name) {
            return Executors.newScheduledThreadPool(3, new BlueprintThreadFactory(name));
        }
    });
    eventDispatcher = new BlueprintEventDispatcher(ctx, executors);
    // Ideally we'd want to only track STARTING and ACTIVE bundle, but this is not supported
    // when using equinox composites.  This would ensure that no STOPPING event is lost while
    // tracking the initial bundles. To work around this issue, we need to register
    // a synchronous bundle listener that will ensure the stopping event will be correctly
    // handled.
    context.addBundleListener(this);
    int mask = Bundle.INSTALLED | Bundle.RESOLVED | Bundle.STARTING | Bundle.STOPPING | Bundle.ACTIVE;
    bt = useSystemContext ? new BundleTracker(trackingContext, mask, this) : new RecursiveBundleTracker(ctx, mask, this);
    proxyManager = new SingleServiceTracker<ProxyManager>(ctx, ProxyManager.class, new SingleServiceListener() {

        public void serviceFound() {
            LOGGER.debug("Found ProxyManager service, starting to process blueprint bundles");
            if (bt instanceof BundleTracker) {
                ((BundleTracker) bt).open();
            } else if (bt instanceof RecursiveBundleTracker) {
                ((RecursiveBundleTracker) bt).open();
            }
        }

        public void serviceLost() {
            while (!containers.isEmpty()) {
                for (Bundle bundle : getBundlesToDestroy()) {
                    destroyContainer(bundle);
                }
            }
            if (bt instanceof BundleTracker) {
                ((BundleTracker) bt).close();
            } else if (bt instanceof RecursiveBundleTracker) {
                ((RecursiveBundleTracker) bt).close();
            }
        }

        public void serviceReplaced() {
        }
    });
    proxyManager.open();
    // Determine if the ParserService should ignore unknown namespace handlers
    boolean ignoreUnknownNamespaceHandlers = Boolean.parseBoolean(ctx.getProperty("org.apache.aries.blueprint.parser.service.ignore.unknown.namespace.handlers"));
    // Create and publish a ParserService
    parserServiceReg = ctx.registerService(ParserService.class.getName(), new ParserServiceImpl(handlers, ignoreUnknownNamespaceHandlers), new Hashtable<String, Object>());
    // Create and publish a BlueprintContainerService
    blueprintServiceReg = ctx.registerService(BlueprintExtenderService.class.getName(), new BlueprintContainerServiceImpl(), new Hashtable<String, Object>());
    try {
        ctx.getBundle().loadClass(QUIESCE_PARTICIPANT_CLASS);
        //Class was loaded, register
        quiesceParticipantReg = ctx.registerService(QUIESCE_PARTICIPANT_CLASS, new BlueprintQuiesceParticipant(ctx, this), new Hashtable<String, Object>());
    } catch (ClassNotFoundException e) {
        LOGGER.info("No quiesce support is available, so blueprint components will not participate in quiesce operations");
    }
    LOGGER.debug("Blueprint extender started");
}
Also used : SingleServiceListener(org.apache.aries.util.tracker.SingleServiceTracker.SingleServiceListener) ScheduledExecutorServiceWrapper(org.apache.aries.blueprint.utils.threading.ScheduledExecutorServiceWrapper) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) ProxyManager(org.apache.aries.proxy.ProxyManager) BundleTracker(org.osgi.util.tracker.BundleTracker) RecursiveBundleTracker(org.apache.aries.util.tracker.RecursiveBundleTracker) ScheduledExecutorServiceFactory(org.apache.aries.blueprint.utils.threading.ScheduledExecutorServiceWrapper.ScheduledExecutorServiceFactory) RecursiveBundleTracker(org.apache.aries.util.tracker.RecursiveBundleTracker) BundleContext(org.osgi.framework.BundleContext) NamespaceHandlerRegistryImpl(org.apache.aries.blueprint.namespace.NamespaceHandlerRegistryImpl)

Aggregations

BundleTracker (org.osgi.util.tracker.BundleTracker)11 Bundle (org.osgi.framework.Bundle)7 BundleEvent (org.osgi.framework.BundleEvent)4 BundleTrackerCustomizer (org.osgi.util.tracker.BundleTrackerCustomizer)3 RecursiveBundleTracker (org.apache.aries.util.tracker.RecursiveBundleTracker)2 BundleContext (org.osgi.framework.BundleContext)2 ServiceReference (org.osgi.framework.ServiceReference)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Hashtable (java.util.Hashtable)1 List (java.util.List)1 NamespaceHandlerRegistryImpl (org.apache.aries.blueprint.namespace.NamespaceHandlerRegistryImpl)1 ScheduledExecutorServiceWrapper (org.apache.aries.blueprint.utils.threading.ScheduledExecutorServiceWrapper)1 ScheduledExecutorServiceFactory (org.apache.aries.blueprint.utils.threading.ScheduledExecutorServiceWrapper.ScheduledExecutorServiceFactory)1 ProxyManager (org.apache.aries.proxy.ProxyManager)1 HelloIsolation (org.apache.aries.subsystem.example.helloIsolation.HelloIsolation)1 InstallInfo (org.apache.aries.subsystem.scope.InstallInfo)1 Scope (org.apache.aries.subsystem.scope.Scope)1 ScopeUpdate (org.apache.aries.subsystem.scope.ScopeUpdate)1