Java Code Examples for org.osgi.framework.BundleActivator

The following code examples are extracted from open source projects. You can click to vote up the examples that are useful to you.

Example 1

From project cxf-dosgi, under directory /distribution/single-bundle/src/main/java/org/apache/cxf/dosgi/singlebundle/.

Source file: AggregatedActivator.java

  18 
vote

void startEmbeddedActivators(BundleContext ctx) throws Exception {
  ClassLoader oldClassLoader=Thread.currentThread().getContextClassLoader();
  try {
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
    for (    String s : getActivators()) {
      try {
        Class<?> clazz=getClass().getClassLoader().loadClass(s);
        Object o=clazz.newInstance();
        if (o instanceof BundleActivator) {
          BundleActivator ba=(BundleActivator)o;
          activators.add(ba);
          ba.start(ctx);
        }
      }
 catch (      Throwable th) {
        LOG.log(Level.SEVERE,"Failed to start Activator " + s,th);
      }
    }
    SPIActivator sba=new SPIActivator();
    sba.start(ctx);
    activators.add(sba);
    DSWActivator dsw=new DSWActivator();
    dsw.start(ctx);
    activators.add(dsw);
  }
  finally {
    Thread.currentThread().setContextClassLoader(oldClassLoader);
  }
}
 

Example 2

From project karaf, under directory /main/src/main/java/org/apache/karaf/main/.

Source file: KarafActivatorManager.java

  18 
vote

void startKarafActivators() throws IOException {
  Enumeration<URL> urls=classLoader.getResources("META-INF/MANIFEST.MF");
  while (urls != null && urls.hasMoreElements()) {
    URL url=urls.nextElement();
    String className=null;
    InputStream is=url.openStream();
    try {
      Manifest mf=new Manifest(is);
      className=mf.getMainAttributes().getValue(KARAF_ACTIVATOR);
      if (className != null) {
        BundleActivator activator=(BundleActivator)classLoader.loadClass(className).newInstance();
        activator.start(framework.getBundleContext());
        karafActivators.add(activator);
      }
    }
 catch (    Throwable e) {
      if (className != null) {
        System.err.println("Error starting karaf activator " + className + ": "+ e.getMessage());
        LOG.log(Level.WARNING,"Error starting karaf activator " + className + " from url "+ url,e);
      }
    }
 finally {
      if (is != null) {
        try {
          is.close();
        }
 catch (        IOException e) {
        }
      }
    }
  }
}
 

Example 3

From project arquillian-container-osgi, under directory /container-embedded/src/test/java/org/jboss/test/arquillian/container/osgi/.

Source file: ARQ194TestCase.java

  15 
vote

@Deployment(name=BUNDLE_NAME,managed=false,testable=false) public static JavaArchive getTestArchive(){
  final JavaArchive archive=ShrinkWrap.create(JavaArchive.class);
  archive.setManifest(new Asset(){
    public InputStream openStream(){
      OSGiManifestBuilder builder=OSGiManifestBuilder.newInstance();
      builder.addBundleSymbolicName(BUNDLE_NAME);
      builder.addBundleManifestVersion(2);
      builder.addBundleActivator(ARQ194Activator.class.getName());
      builder.addExportPackages(ARQ194Service.class);
      builder.addImportPackages(BundleActivator.class);
      return builder.openStream();
    }
  }
);
  archive.addClasses(ARQ194Activator.class,ARQ194Service.class);
  return archive;
}
 

Example 4

From project arquillian-container-osgi, under directory /container-embedded/src/test/java/org/jboss/test/arquillian/container/osgi/.

Source file: SimpleBundleTestCase.java

  15 
vote

@Deployment public static JavaArchive createdeployment(){
  final JavaArchive archive=ShrinkWrap.create(JavaArchive.class,"test.jar");
  archive.addClasses(SimpleActivator.class,SimpleService.class);
  archive.setManifest(new Asset(){
    public InputStream openStream(){
      OSGiManifestBuilder builder=OSGiManifestBuilder.newInstance();
      builder.addBundleSymbolicName(archive.getName());
      builder.addBundleManifestVersion(2);
      builder.addBundleActivator(SimpleActivator.class.getName());
      builder.addImportPackages(BundleActivator.class);
      return builder.openStream();
    }
  }
);
  return archive;
}
 

Example 5

From project bndtools, under directory /bndtools.core/src/bndtools/editor/contents/.

Source file: GeneralInfoPart.java

  15 
vote

@Override protected List<IContentProposal> doGenerateProposals(String contents,int position){
  final String prefix=contents.substring(0,position);
  final IJavaProject javaProject=getJavaProject();
  if (javaProject == null)   return Collections.emptyList();
  try {
    final List<IContentProposal> result=new ArrayList<IContentProposal>();
    final IRunnableWithProgress runnable=new IRunnableWithProgress(){
      public void run(      IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
        try {
          IType activatorType=javaProject.findType(BundleActivator.class.getName());
          if (activatorType != null) {
            ITypeHierarchy hierarchy=activatorType.newTypeHierarchy(javaProject,monitor);
            for (            IType subType : hierarchy.getAllSubtypes(activatorType)) {
              if (!Flags.isAbstract(subType.getFlags()) && subType.getElementName().toLowerCase().contains(prefix.toLowerCase())) {
                result.add(new JavaTypeContentProposal(subType));
              }
            }
          }
        }
 catch (        JavaModelException e) {
          throw new InvocationTargetException(e);
        }
      }
    }
;
    IWorkbenchWindow window=((IFormPage)getManagedForm().getContainer()).getEditorSite().getWorkbenchWindow();
    window.run(false,false,runnable);
    return result;
  }
 catch (  InvocationTargetException e) {
    logger.logError("Error searching for BundleActivator types",e.getTargetException());
    return Collections.emptyList();
  }
catch (  InterruptedException e) {
    Thread.currentThread().interrupt();
    return Collections.emptyList();
  }
}
 

Example 6

From project bundlemaker, under directory /main/org.bundlemaker.core.osgi/src/org/bundlemaker/core/osgi/utils/.

Source file: SystemBundleCopier.java

  15 
vote

/** 
 * <p> </p>
 * @param destinationDirectory
 * @throws FileNotFoundException
 * @throws IOException
 */
public static void copySystemBundle(File destinationDirectory) throws FileNotFoundException, IOException {
  Assert.isNotNull(destinationDirectory);
  Assert.isTrue(destinationDirectory.isDirectory(),"destinationDirectory.isDirectory()");
  URL url=BundleActivator.class.getProtectionDomain().getCodeSource().getLocation();
  String file=BundleActivator.class.getProtectionDomain().getCodeSource().getLocation().getFile();
  String fileName=file.substring(file.lastIndexOf("/") + 1);
  FileUtils.copy(url.openStream(),new FileOutputStream(new File(destinationDirectory,fileName)),new byte[1024]);
}
 

Example 7

From project cxf-dosgi, under directory /distribution/single-bundle/src/main/java/org/apache/cxf/dosgi/singlebundle/.

Source file: AggregatedActivator.java

  15 
vote

void stopEmbeddedActivators(BundleContext ctx) throws Exception {
  for (  BundleActivator ba : activators) {
    try {
      ba.stop(ctx);
    }
 catch (    Throwable ex) {
      LOG.warning("BundleActivator " + ba.getClass().getName() + " can not be stopped");
    }
  }
}
 

Example 8

From project Gemini-Blueprint, under directory /extender/src/main/java/org/eclipse/gemini/blueprint/extender/internal/boot/.

Source file: ChainActivator.java

  15 
vote

public ChainActivator(){
  if (OsgiPlatformDetector.isR42()) {
    if (BLUEPRINT_AVAILABLE) {
      log.info("Blueprint API detected; enabling Blueprint Container functionality");
      CHAIN=new BundleActivator[]{new ContextLoaderListener(),new BlueprintLoaderListener()};
    }
 else {
      log.warn("Blueprint API not found; disabling Blueprint Container functionality");
      CHAIN=new BundleActivator[]{new ContextLoaderListener()};
    }
  }
 else {
    log.warn("Pre-4.2 OSGi platform detected; disabling Blueprint Container functionality");
    CHAIN=new BundleActivator[]{new ContextLoaderListener()};
  }
}
 

Example 9

From project jbosgi, under directory /testsuite/example/src/test/java/org/jboss/test/osgi/example/core/.

Source file: BundleLifecycleTestCase.java

  15 
vote

@Deployment public static JavaArchive createdeployment(){
  final JavaArchive archive=ShrinkWrap.create(JavaArchive.class,"example-bundle");
  archive.addClasses(SimpleActivator.class,SimpleService.class);
  archive.setManifest(new Asset(){
    @Override public InputStream openStream(){
      OSGiManifestBuilder builder=OSGiManifestBuilder.newInstance();
      builder.addBundleSymbolicName(archive.getName());
      builder.addBundleManifestVersion(2);
      builder.addBundleActivator(SimpleActivator.class);
      builder.addImportPackages(BundleActivator.class);
      return builder.openStream();
    }
  }
);
  return archive;
}
 

Example 10

From project jbosgi, under directory /testsuite/example/src/test/java/org/jboss/test/osgi/example/deployment/.

Source file: ArquillianDeployerTestCase.java

  15 
vote

@Deployment(name=DEPLOYMENT_NAME,managed=false,testable=false) public static JavaArchive getTestArchive(){
  final JavaArchive archive=ShrinkWrap.create(JavaArchive.class,DEPLOYMENT_NAME);
  archive.addClasses(SimpleActivator.class,SimpleService.class);
  archive.setManifest(new Asset(){
    @Override public InputStream openStream(){
      OSGiManifestBuilder builder=OSGiManifestBuilder.newInstance();
      builder.addBundleSymbolicName(archive.getName());
      builder.addBundleManifestVersion(2);
      builder.addBundleActivator(SimpleActivator.class);
      builder.addImportPackages(BundleActivator.class);
      return builder.openStream();
    }
  }
);
  return archive;
}
 

Example 11

From project jbosgi, under directory /testsuite/example/src/test/java/org/jboss/test/osgi/example/ejb3/.

Source file: StatelessBeanTestCase.java

  15 
vote

@Deployment(order=1) public static JavaArchive deployment(){
  final JavaArchive archive=ShrinkWrap.create(JavaArchive.class,"ejb3-osgi-target");
  archive.addClasses(Echo.class,TargetBundleActivator.class);
  archive.setManifest(new Asset(){
    public InputStream openStream(){
      OSGiManifestBuilder builder=OSGiManifestBuilder.newInstance();
      builder.addBundleSymbolicName(archive.getName());
      builder.addBundleManifestVersion(2);
      builder.addBundleActivator(TargetBundleActivator.class);
      builder.addImportPackages(BundleActivator.class,Logger.class,Module.class,InitialContext.class);
      builder.addExportPackages(Echo.class);
      return builder.openStream();
    }
  }
);
  return archive;
}
 

Example 12

From project jbosgi-framework, under directory /core/src/test/java/org/jboss/osgi/framework/internal/.

Source file: BundleStorageTestCase.java

  15 
vote

private JavaArchive getArchive(){
  final JavaArchive archive=ShrinkWrap.create(JavaArchive.class,"simple-bundle");
  archive.addClasses(SimpleService.class,SimpleActivator.class);
  archive.setManifest(new Asset(){
    public InputStream openStream(){
      OSGiManifestBuilder builder=OSGiManifestBuilder.newInstance();
      builder.addBundleManifestVersion(2);
      builder.addBundleSymbolicName(archive.getName());
      builder.addBundleVersion("1.0.0");
      builder.addBundleActivator(SimpleActivator.class);
      builder.addImportPackages(BundleActivator.class);
      return builder.openStream();
    }
  }
);
  return archive;
}
 

Example 13

From project jbosgi-framework, under directory /itest/src/test/java/org/jboss/test/osgi/framework/bundle/activation/.

Source file: BundleActivationTestCase.java

  15 
vote

private static JavaArchive getLazyServiceProvider(){
  final JavaArchive archive=ShrinkWrap.create(JavaArchive.class,"lazy-service-provider");
  archive.addClasses(SimpleActivator.class,SimpleService.class,BeanB.class);
  archive.setManifest(new Asset(){
    public InputStream openStream(){
      OSGiManifestBuilder builder=OSGiManifestBuilder.newInstance();
      builder.addBundleSymbolicName(archive.getName());
      builder.addBundleManifestVersion(2);
      builder.addBundleActivator(SimpleActivator.class.getName());
      builder.addBundleActivationPolicy(Constants.ACTIVATION_LAZY);
      builder.addExportPackages(SimpleService.class);
      builder.addImportPackages(BundleActivator.class);
      return builder.openStream();
    }
  }
);
  return archive;
}
 

Example 14

From project jbosgi-framework, under directory /itest/src/test/java/org/jboss/test/osgi/framework/bundle/activation/.

Source file: BundleActivationTestCase.java

  15 
vote

private static JavaArchive getLazyServiceProviderWithInclude(){
  final JavaArchive archive=ShrinkWrap.create(JavaArchive.class,"lazy-service-provider-include");
  archive.addClasses(SimpleActivator.class,SimpleService.class,BeanB.class);
  archive.setManifest(new Asset(){
    public InputStream openStream(){
      OSGiManifestBuilder builder=OSGiManifestBuilder.newInstance();
      builder.addBundleSymbolicName(archive.getName());
      builder.addBundleManifestVersion(2);
      builder.addBundleActivator(SimpleActivator.class.getName());
      builder.addBundleActivationPolicy(Constants.ACTIVATION_LAZY + ";include:='" + SimpleService.class.getPackage().getName()+ "'");
      builder.addExportPackages(SimpleService.class);
      builder.addImportPackages(BundleActivator.class);
      return builder.openStream();
    }
  }
);
  return archive;
}
 

Example 15

From project karaf, under directory /main/src/main/java/org/apache/karaf/main/.

Source file: KarafActivatorManager.java

  15 
vote

void stopKarafActivators(){
  for (  BundleActivator activator : karafActivators) {
    try {
      activator.stop(framework.getBundleContext());
    }
 catch (    Throwable e) {
      LOG.log(Level.WARNING,"Error stopping karaf activator " + activator.getClass().getName(),e);
    }
  }
}