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

The following examples show how to use org.osgi.framework.BundleContext#addFrameworkListener() . 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)
    throws Exception
{
  this.bc = bc;

  isLog = bc.createFilter(LOG_FILTER);
  isDevice = bc.createFilter(DEVICE_FILTER);
  isDriver = bc.createFilter(DRIVER_FILTER);
  isSelector = bc.createFilter(SELECTOR_FILTER);
  isLocator = bc.createFilter(LOCATOR_FILTER);

  startService(LOG_FILTER);

  start();

  bc.addFrameworkListener(this);
  final Bundle b = bc.getBundle(0);
  if (b.getState() == Bundle.ACTIVE) {
    activate();
  } else {
    info("Passive start");
  }
}
 
Example 2
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 3
Source File: NetigsoStartLevelTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testActivation() throws Exception {
    ModuleSystem ms = Main.getModuleSystem();
    mgr = ms.getManager();
    mgr.mutexPrivileged().enterWriteAccess();
    try {
        m1 = mgr.createBundle(simpleModule, null, false, false, false, 10);
        mgr.enable(m1);
    } finally {
        mgr.mutexPrivileged().exitWriteAccess();
    }

    Class<?> main = m1.getClassLoader().loadClass("org.activate.Main");
    Object s = main.getField("start").get(null);
    assertNull("Not started yet", s);

    Framework f = NetigsoServicesTest.findFramework();
    final BundleContext fc = f.getBundleContext();
    fc.addFrameworkListener(this);
    ServiceReference sr = fc.getServiceReference(StartLevel.class.getName());
    assertNotNull("Start level service found", sr);
    StartLevel level = (StartLevel) fc.getService(sr);
    assertNotNull("Start level found", level);
    level.setStartLevel(10);
    waitLevelChanged();
        
    s = main.getField("start").get(null);
    assertNotNull("Bundle started, its context provided", s);

    mgr.mutexPrivileged().enterWriteAccess();
    try {
        mgr.disable(m1);

        Object e = main.getField("stop").get(null);
        assertNotNull("Bundle stopped, its context provided", e);
    } finally {
        mgr.mutexPrivileged().exitWriteAccess();
    }
}
 
Example 4
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 5
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 6
Source File: NetbinoxHooks.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void frameworkStart(BundleContext bc) throws BundleException {
    bc.addFrameworkListener(this);
}
 
Example 7
Source File: Util.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Calls package admin refresh packages and waits for the operation
 * to complete.
 *
 * @param bc context owning both resources and to install bundle from
 * @param bundles the inital list of bundles to refresh.
 * @return null on sucess, string with error message on failure.
 */
public static String refreshPackages(BundleContext bc,
                                     Bundle[] bundles)
{
  System.out.println("PackageAdmin.refreshPackages("
                     +Arrays.asList(bundles) +")");
  ServiceReference paSR
    = bc.getServiceReference(PackageAdmin.class.getName());
  if (null==paSR)
    return "No package admin service reference.";

  PackageAdmin pa = (PackageAdmin) bc.getService(paSR);
  if (null==pa)
    return "No package admin service.";

  final Object lock = new Object();

  FrameworkListener fListen = new FrameworkListener(){
      public void frameworkEvent(FrameworkEvent event)
      {
        System.out.println("Got framework event of type "+event.getType());
        if (event.getType()==FrameworkEvent.PACKAGES_REFRESHED) {
          synchronized(lock) {
            lock.notifyAll();
          }
        }
      }
    };
  bc.addFrameworkListener(fListen);

  try {
    pa.refreshPackages(bundles);
  } catch (Exception e) {
    e.printStackTrace();
    return "Failed to refresh packages, got exception " +e;
  }

  synchronized (lock) {
    try {
      lock.wait(30000L);
    } catch (InterruptedException ie) {
      System.err.println("Waiting or packages refreshed was interrupted.");
    }
  }
  System.out.println("PackageAdmin.refreshPackages("
                     +Arrays.asList(bundles) +") done.");

  bc.removeFrameworkListener(fListen);

  return null;
}