Java Code Examples for org.osgi.framework.BundleContext#addBundleListener()

The following examples show how to use org.osgi.framework.BundleContext#addBundleListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Activator.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void start(BundleContext bc) {

    lc = new LogConfigImpl(bc);
    lrsf = new LogReaderServiceFactory(bc, lc);
    lsf = new LogServiceFactory(lrsf);

    // Catch all framework error and place in the log.
    LogFrameworkListener lfl = new LogFrameworkListener(lrsf);
    bc.addFrameworkListener(lfl);
    bc.addBundleListener(lfl);
    bc.addServiceListener(lfl);

    // Register our services
    bc.registerService(LOG_READER_SERVICE_CLASS, lrsf, null);
    bc.registerService(LOG_SERVICE_CLASSES, lsf, null);
  }
 
Example 2
Source File: OSGIActivator.java    From incubator-tamaya with Apache License 2.0 6 votes vote down vote up
@Override
public void start(BundleContext context) {
    // Register marker service
    this.serviceLoader = new OSGIServiceLoader(context);
    context.addBundleListener(serviceLoader);
    ServiceContextManager.setToStaticClassLoader(new OSGIServiceContext(serviceLoader));
    LOG.info("Registered Tamaya OSGI ServiceContext...");
    Configuration.setCurrent(
                   new CoreConfigurationBuilder()
                    .addDefaultPropertyConverters()
                    .addDefaultPropertyFilters()
                    .addDefaultPropertySources()
                    .sortPropertyFilter(PropertyFilterComparator.getInstance())
                    .sortPropertySources(PropertySourceComparator.getInstance())
                    .build()
    );
    LOG.info("Loaded default configuration from OSGI.");
}
 
Example 3
Source File: EmacsPlusActivator.java    From e4macs with Eclipse Public License 1.0 6 votes vote down vote up
private void addBundlerListener(BundleContext context) {
	// Listen for our STARTED event
	// Alternatively, we can get the bundle from org.osgi.framework.FrameworkUtil
	context.addBundleListener(new BundleListener() {
		public void bundleChanged(BundleEvent event) {
			String name = event.getBundle().getSymbolicName();
			if (name != null && name.equals(PLUGIN_ID)) {
				switch (event.getType()) {
					case BundleEvent.STARTED:
						bundleStarted();
						break;
					case BundleEvent.UNINSTALLED:
						emacsIds = null;
						break;
					default:
						break;
				}
			}
		}
	});
}
 
Example 4
Source File: Activator.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Override
public void start(final BundleContext bundleContext) throws Exception {
    ProviderUtil.STARTUP_LOCK.lock();
    lockingProviderUtil = true;
    final BundleWiring self = bundleContext.getBundle().adapt(BundleWiring.class);
    final List<BundleWire> required = self.getRequiredWires(LoggerContextFactory.class.getName());
    for (final BundleWire wire : required) {
        loadProvider(bundleContext, wire.getProviderWiring());
    }
    bundleContext.addBundleListener(this);
    final Bundle[] bundles = bundleContext.getBundles();
    for (final Bundle bundle : bundles) {
        loadProvider(bundle);
    }
    unlockIfReady();
}
 
Example 5
Source File: Activator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public @Override void start(final BundleContext context) throws Exception {
        if (System.getProperty("netbeans.home") != null) {
            throw new IllegalStateException("Should not be run from inside regular NetBeans module system");
        }
        String storage = context.getProperty(Constants.FRAMEWORK_STORAGE);
        if (storage != null) {
            System.setProperty("netbeans.user", storage);
        }
        System.setProperty("TopSecurityManager.disable", "true");
        NbBundle.setBranding(System.getProperty("branding.token"));
        OSGiMainLookup.initialize(context);
        queue = new DependencyQueue<String,Bundle>();
        this.context = context;
        framework = ((Framework) context.getBundle(0));
        if (framework.getState() == Bundle.STARTING) {
            LOG.fine("framework still starting");
            final AtomicReference<FrameworkListener> frameworkListener = new AtomicReference<FrameworkListener>();
            frameworkListener.set(new FrameworkListener() {
                public @Override void frameworkEvent(FrameworkEvent event) {
                    if (event.getType() == FrameworkEvent.STARTED) {
//                        System.err.println("framework started");
                        context.removeFrameworkListener(frameworkListener.get());
                        context.addBundleListener(Activator.this);
                        processLoadedBundles();
                    }
                }
            });
            context.addFrameworkListener(frameworkListener.get());
        } else {
            LOG.fine("framework already started");
            context.addBundleListener(this);
            processLoadedBundles();
        }
    }
 
Example 6
Source File: Activator.java    From sling-whiteboard with Apache License 2.0 5 votes vote down vote up
@Override
public void start(BundleContext context) throws Exception {
    
    bstc = new BundleStartTimeCalculator(context.getBundle().getBundleId());
    context.addBundleListener(bstc);

    srcc = new ServiceRestartCountCalculator();
    context.addServiceListener(srcc);

    stc = new StartupTimeCalculator(context, bstc, srcc);
}
 
Example 7
Source File: EclipseClassProvider.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Constructor.
 * 
 * @param context
 *            the {@link BundleContext}
 * @param classLoader
 *            the fall back {@link ClassLoader}
 */
public EclipseClassProvider(BundleContext context, ClassLoader classLoader) {
    super(classLoader);
    this.context = context;
    for (Bundle bundle : context.getBundles()) {
        bundles.put(bundle.getSymbolicName(), bundle);
    }
    listener = new BundleListener() {

        @Override
        public void bundleChanged(BundleEvent event) {
            switch (event.getType()) {
                case BundleEvent.INSTALLED:
                    bundles.put(event.getBundle().getSymbolicName(), event.getBundle());
                    break;

                case BundleEvent.UNINSTALLED:
                    bundles.remove(event.getBundle().getSymbolicName());
                    break;

                default:
                    // nothing to do here
                    break;
            }
        }
    };
    context.addBundleListener(listener);
}
 
Example 8
Source File: EventAdminActivator.java    From concierge with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 
 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
 */
public void start(BundleContext context) throws Exception {
	EventAdminActivator.context = context;
	eventAdmin = new EventAdminImpl();
	context.addBundleListener(eventAdmin);
	context.addServiceListener(eventAdmin);
	context.addFrameworkListener(eventAdmin);
	context.registerService(EventAdmin.class.getName(), eventAdmin, null);
}
 
Example 9
Source File: LogConfigImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public LogConfigImpl(BundleContext bc) {
  LogConfigImpl.bc = bc;

  // Initialize with default values.
  applyConfig(getDefault());

  bc.addBundleListener(this);
  start();
}
 
Example 10
Source File: SiddhiExtensionLoader.java    From siddhi with Apache License 2.0 5 votes vote down vote up
/**
 * Load Extensions in OSGi environment.
 *
 * @param bundleContext       OSGi bundleContext
 * @param siddhiExtensionsMap reference map for the Siddhi extension
 * @param extensionHolderMap  reference map for the Siddhi extension holder
 */
private static void loadExtensionOSGI(BundleContext bundleContext,
                                      Map<String, Class> siddhiExtensionsMap,
                                      ConcurrentHashMap<Class, AbstractExtensionHolder> extensionHolderMap) {
    ExtensionBundleListener extensionBundleListener
            = new ExtensionBundleListener(siddhiExtensionsMap, extensionHolderMap);
    bundleContext.addBundleListener(extensionBundleListener);
    extensionBundleListener.loadAllExtensions(bundleContext);
}
 
Example 11
Source File: CXFActivator.java    From cxf with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc}*/
public void start(BundleContext context) throws Exception {
    workQueues = new ManagedWorkQueueList();
    cxfBundleListener = new CXFExtensionBundleListener(context.getBundle().getBundleId());
    context.addBundleListener(cxfBundleListener);
    cxfBundleListener.registerExistingBundles(context);

    configAdminTracker = new ServiceTracker<>(context, ConfigurationAdmin.class, null);
    configAdminTracker.open();
    workQueues.setConfigAdminTracker(configAdminTracker);
    workQueueServiceRegistration = registerManagedServiceFactory(context,
                                                                 ManagedServiceFactory.class,
                                                                 workQueues,
                                                                 ManagedWorkQueueList.FACTORY_PID);

    extensions = new ArrayList<>();
    extensions.add(createOsgiBusListenerExtension(context));
    extensions.add(createManagedWorkQueueListExtension(workQueues));
    ExtensionRegistry.addExtensions(extensions);

    BlueprintNameSpaceHandlerFactory factory = new BlueprintNameSpaceHandlerFactory() {

        @Override
        public Object createNamespaceHandler() {
            return new CXFAPINamespaceHandler();
        }
    };
    NamespaceHandlerRegisterer.register(context, factory,
                                        "http://cxf.apache.org/blueprint/core",
                                        "http://cxf.apache.org/configuration/beans",
                                        "http://cxf.apache.org/configuration/parameterized-types",
                                        "http://cxf.apache.org/configuration/security",
                                        "http://schemas.xmlsoap.org/wsdl/",
                                        "http://www.w3.org/2005/08/addressing",
                                        "http://schemas.xmlsoap.org/ws/2004/08/addressing");

}
 
Example 12
Source File: BundleCheckActivator.java    From micro-integrator with Apache License 2.0 4 votes vote down vote up
public void start(final BundleContext bundleContext) throws Exception {

		/*
         * Check for the services that should be deployed before this if there
		 * are any.
		 * if there are such services we wait for such bundles to deploy and
		 * gets the notification
		 * through a service listener.
		 */

        // first check whether there are such services and they have already
        // deployed.
        String pendingBundleName = null;
        final List<Bundle> pendingBundles = new ArrayList<Bundle>();
        for (Bundle bundle : bundleContext.getBundles()) {
            pendingBundleName = (String) bundle.getHeaders().get(DEPLOY_BEFORE);
            if ((pendingBundleName != null) && (pendingBundleName.equals(getName())) &&
                    (bundle.getState() != Bundle.ACTIVE)) {
                // i.e this bundle should be started before the user manager but
                // yet has not started
                pendingBundles.add(bundle);
            }
        }

        if (pendingBundles.isEmpty()) {
            startDeploy(bundleContext);
        } else {
            BundleListener bundleListener = new BundleListener() {
                public void bundleChanged(BundleEvent bundleEvent) {
                    synchronized (pendingBundles) {
                        if (bundleEvent.getType() == BundleEvent.STARTED) {

                            pendingBundles.remove(bundleEvent.getBundle());
                            if (pendingBundles.isEmpty()) {
                                // now start the user manager deployment
                                bundleContext.removeBundleListener(this);
                                try {
                                    startDeploy(bundleContext);
                                } catch (Exception e) {
                                    log.error("Can not start the bundle ", e);
                                }
                            }
                        }
                    }
                }
            };
            bundleContext.addBundleListener(bundleListener);
        }
    }
 
Example 13
Source File: OSGiActivator.java    From sis with Apache License 2.0 4 votes vote down vote up
/**
 * Invoked when this bundle is started.
 *
 * @param  context  the execution context of the bundle being started.
 */
@Override
public void start(final BundleContext context) {
    context.addBundleListener(this);
    Shutdown.setContainer("OSGi");
}