Java源码示例:org.osgi.framework.BundleActivator

示例1
/**
 * If the extension has an extension activator header process it.
 *
 * @param extension the extension bundle to process.
 */
private BundleActivator handleExtensionActivator(final BundleGeneration extension) throws BundleException {
  String extActivatorName =
    extension.archive.getAttribute(Constants.EXTENSION_BUNDLE_ACTIVATOR);
  extActivatorName = null!=extActivatorName ? extActivatorName.trim() : null;

  if (null != extActivatorName && extActivatorName.length() > 0) {
    fwCtx.log("Create bundle activator for extension: " + extension.symbolicName
              + ":" +extension.version + " using: " +extActivatorName);
    try {
      final Class<BundleActivator> c = (Class<BundleActivator>)Class.forName(extActivatorName);
      return c.newInstance();
    } catch (Throwable t) {
      final String msg = "Failed to instanciate extension activator " + extActivatorName + ", " + extension.bundle;
      fwCtx.log(msg, t);
      throw new BundleException(msg, BundleException.ACTIVATOR_ERROR, t);
    }
  }
  return null;
}
 
示例2
private void extensionCallStop() {
  BundleImpl[] bs = extensions.keySet().toArray(new BundleImpl[extensions.size()]);
  for (int i = bs.length - 1; i >= 0; i--) {
    if (fwCtx.debug.framework) {
      fwCtx.debug.println("Call extension bundle stop: " + bs[i]);
    }
    BundleActivator ba = extensions.get(bs[i]);
    if (ba != null) {
      try {
        ba.stop(bundleContext);
      } catch (final Throwable t) {
        final String msg = "Failed to stop framework extension, " + bs[i];
        fwCtx.frameworkError(bs[i], new BundleException(msg, BundleException.ACTIVATOR_ERROR, t));
      }
    }
  }
  extensions.clear();
}
 
示例3
private void callBundleActivatorStart(BundleImpl b) {
  BundleActivator ba = extensions.get(b);
  if (ba != null) {
    if (fwCtx.debug.framework) {
      fwCtx.debug.println("Call extension bundle start: " + b);
    }
    try {
      ba.start(bundleContext);
    } catch (final Throwable t) {
      extensions.put(b, null);
      final String msg = "Failed to start framework extension, " + b;
      fwCtx.frameworkError(b, new BundleException(msg, BundleException.ACTIVATOR_ERROR, t));
    }
  }
}
 
示例4
@Test
public void testStartupExtension() {
  Extension extension = getEarlyStartup();

  IStartup startup = extension.createExecutableExtension( "class", IStartup.class );
  assertThat( startup ).isInstanceOf( DynamicWorkingSetStartup.class );
  assertThat( startup ).isNotInstanceOf( BundleActivator.class );
}
 
示例5
@Test
public void testStartupExtension() {
  Extension extension = getEarlyStartup();

  IStartup startup = extension.createExecutableExtension( "class", IStartup.class );
  assertThat( startup ).isInstanceOf( LaunchExtrasStartup.class );
  assertThat( startup ).isNotInstanceOf( BundleActivator.class );
}
 
示例6
public synchronized void startBundle() throws BundleException {
    if (this.state == 1) {
        throw new IllegalStateException("Cannot start uninstalled bundle " + toString());
    } else if (this.state != 32) {
        if (this.state == 2) {
            resolveBundle(true);
        }
        this.state = 8;
        try {
            this.context.isValid = true;
            if (!(this.classloader.activatorClassName == null || StringUtils.isBlank(this.classloader.activatorClassName))) {
                Class loadClass = this.classloader.loadClass(this.classloader.activatorClassName);
                if (loadClass == null) {
                    throw new ClassNotFoundException(this.classloader.activatorClassName);
                }
                this.classloader.activator = (BundleActivator) loadClass.newInstance();
                this.classloader.activator.start(this.context);
            }
            this.state = 32;
            Framework.notifyBundleListeners(2, this);
            if (Framework.DEBUG_BUNDLES && log.isInfoEnabled()) {
                log.info("Framework: Bundle " + toString() + " started.");
            }
        } catch (Throwable th) {
            Throwable th2 = th;
            Framework.clearBundleTrace(this);
            this.state = 4;
            String str = "Error starting bundle " + toString();
            if (th2.getCause() != null) {
                th2 = th2.getCause();
            }
            BundleException bundleException = new BundleException(str, th2);
        }
    }
}
 
示例7
public HostApplication() throws IOException {
    // Create a configuration property map.
    Map<String, Object> config = new HashMap<String, Object>();
    // Create host activator;
    m_activator = new HostActivator();
    List<BundleActivator> list = new ArrayList<BundleActivator>();
    list.add(m_activator);
    config.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
    config.put(FelixConstants.FRAMEWORK_STORAGE, "target/felix");
    config.put(FelixConstants.LOG_LEVEL_PROP, "5");

    cleanUp("target/felix");

    try
    {
        // Now create an instance of the framework with
        // our configuration properties.
        m_felix = new Felix(config);
        // Now start Felix instance.
        m_felix.start();
    }
    catch (Exception ex)
    {
        System.err.println("Could not create framework: " + ex);
        ex.printStackTrace();
    }
}
 
示例8
private void install() throws BundleException {

		// register bundle with framework:
		synchronized (framework) {
			framework.bundles.add(this);
			framework.bundleID_bundles.put(new Long(getBundleId()), this);
			framework.symbolicName_bundles
					.insert(currentRevision.getSymbolicName(), this);
			framework.location_bundles.put(location, this);
		}

		this.state = Bundle.INSTALLED;
		
		// resolve if it is a framework extension
		if (currentRevision.isExtensionBundle()) {
			if(currentRevision.resolve(false)){
				// call start for extension activator before notifying framework of resolve
				if(currentRevision.extActivatorClassName != null){
					try {
						@SuppressWarnings("unchecked")
						final Class<BundleActivator> activatorClass = (Class<BundleActivator>) framework.systemBundleClassLoader.loadClass(currentRevision.extActivatorClassName);
						if (activatorClass == null) {
							throw new ClassNotFoundException(currentRevision.extActivatorClassName);
						}
						currentRevision.extActivatorInstance = activatorClass.newInstance();
						currentRevision.extActivatorInstance.start(framework.context);
					} catch(Throwable err){
						currentRevision.extActivatorInstance = null;
						framework.notifyFrameworkListeners(FrameworkEvent.ERROR, BundleImpl.this,
								new BundleException("Error calling extension bundle start(): " + this.toString(),
										BundleException.ACTIVATOR_ERROR, err));
					}
				}
			}
		}
		
		// we are just installing the bundle, if it is
		// possible, resolve it, if not, wait until the
		// exports are really needed (i.e., they become critical)
		// if (!currentRevision.isFragment()) {
		// currentRevision.resolve(false);
		// }
	}