use of org.osgi.framework.BundleActivator in project jackrabbit-oak by apache.
the class OakOSGiRepositoryFactory method getRepository.
@SuppressWarnings("unchecked")
public Repository getRepository(Map parameters) throws RepositoryException {
if (parameters == null || !parameters.containsKey(REPOSITORY_HOME)) {
//Required param missing so repository cannot be created
return null;
}
Map config = new HashMap();
config.putAll(parameters);
PojoServiceRegistry registry = initializeServiceRegistry(config);
BundleActivator activator = getApplicationActivator(config);
try {
activator.start(registry.getBundleContext());
} catch (Exception e) {
log.warn("Error occurred while starting activator {}", activator.getClass(), e);
}
//Future which would be used to notify when repository is ready
// to be used
SettableFuture<Repository> repoFuture = SettableFuture.create();
new RunnableJobTracker(registry.getBundleContext());
int timeoutInSecs = PropertiesUtil.toInteger(config.get(REPOSITORY_TIMEOUT_IN_SECS), DEFAULT_TIMEOUT);
//Start the tracker for repository creation
new RepositoryTracker(registry, activator, repoFuture, timeoutInSecs);
// where OSGi runtime fails to start due to bugs (like cycles)
try {
return repoFuture.get(timeoutInSecs, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RepositoryException("Repository initialization was interrupted");
} catch (ExecutionException e) {
throw new RepositoryException(e);
} catch (TimeoutException e) {
try {
if (PropertiesUtil.toBoolean(config.get(REPOSITORY_SHUTDOWN_ON_TIMEOUT), true)) {
shutdown(registry, timeoutInSecs);
log.info("OSGi container shutdown after waiting for repository initialization for {} sec", timeoutInSecs);
} else {
log.warn("[{}] found to be false. Container is not stopped", REPOSITORY_SHUTDOWN_ON_TIMEOUT);
}
} catch (BundleException be) {
log.warn("Error occurred while shutting down the service registry (due to " + "startup timeout) backing the Repository ", be);
}
throw new RepositoryException("Repository could not be started in " + timeoutInSecs + " seconds", e);
}
}
use of org.osgi.framework.BundleActivator in project bnd by bndtools.
the class DSTestWiringTest method testSimple.
public void testSimple() throws Exception {
BundleActivator act = mock(BundleActivator.class);
TestingLog testlog = new TestingLog().direct().stacktrace();
DSTestWiring ds = new DSTestWiring();
// by instance
ds.add(this);
ds.add(act);
// instance
ds.add(testlog).$("filters", Arrays.asList("skip"));
// by name
ds.add(String.class.getName());
// by class
ds.add(A.class).$("a", 1);
ds.add(1);
ds.add(2);
ds.add(3);
ds.add(4);
ds.wire();
assertNotNull(log);
assertNotNull(string);
assertNotNull(a);
assertNotNull(a.map);
assertEquals(1, a.map.get("a"));
assertEquals(Arrays.asList(1, 2, 3, 4), integers);
assertEquals(1, integer);
ds.get(BundleActivator.class).start(null);
verify(act).start(null);
verifyNoMoreInteractions(act);
log.log(LogService.LOG_ERROR, "skip");
log.log(LogService.LOG_ERROR, "include");
assertEquals(1, testlog.getEntries().size());
assertFalse(testlog.check("include"));
}
use of org.osgi.framework.BundleActivator in project jetty.project by eclipse.
the class PackageAdminServiceTracker method invokeFragmentActivators.
private void invokeFragmentActivators(ServiceReference sr) {
PackageAdmin admin = (PackageAdmin) _context.getService(sr);
Bundle[] fragments = admin.getFragments(_context.getBundle());
if (fragments == null) {
return;
}
for (Bundle frag : fragments) {
// find a convention to look for a class inside the fragment.
try {
String fragmentActivator = frag.getSymbolicName() + ".FragmentActivator";
Class<?> c = Class.forName(fragmentActivator);
if (c != null) {
BundleActivator bActivator = (BundleActivator) c.newInstance();
bActivator.start(_context);
_activatedFragments.add(bActivator);
}
} catch (NullPointerException e) {
// e.printStackTrace();
} catch (InstantiationException e) {
// e.printStackTrace();
} catch (IllegalAccessException e) {
// e.printStackTrace();
} catch (ClassNotFoundException e) {
// e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
use of org.osgi.framework.BundleActivator in project karaf by apache.
the class KarafActivatorManager method startKarafActivators.
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) {
}
}
}
}
}
use of org.osgi.framework.BundleActivator in project bnd by bndtools.
the class AgentDispatcher method createFramework.
/**
* Create a new framework. This is reflectively called from the Envoy
*/
public static Descriptor createFramework(String name, Map<String, Object> configuration, final File storage, final File shacache) throws Exception {
//
// Use the service loader for loading a framework
//
ClassLoader loader = AgentServer.class.getClassLoader();
ServiceLoader<FrameworkFactory> sl = ServiceLoader.load(FrameworkFactory.class, loader);
FrameworkFactory ff = null;
for (FrameworkFactory fff : sl) {
ff = fff;
// break;
}
if (ff == null)
throw new IllegalArgumentException("No framework on runpath");
//
// Create the framework
//
@SuppressWarnings({ "unchecked", "rawtypes" }) Framework framework = ff.newFramework((Map) configuration);
framework.init();
framework.getBundleContext().addFrameworkListener(new FrameworkListener() {
@Override
public void frameworkEvent(FrameworkEvent event) {
// System.err.println("FW Event " + event);
}
});
framework.start();
Descriptor d = new Descriptor();
//
// create a new descriptor. This is returned
// to the envoy side as an Object and we will
// get this back later in toAgent. The envoy
// maintains a list of name -> framework
//
d.framework = framework;
d.shaCache = shacache;
d.storage = storage;
d.configuration = configuration;
d.name = name;
String embedded = (String) configuration.get("biz.aQute.remote.embedded");
if (embedded != null && !(embedded = embedded.trim()).isEmpty()) {
String[] activators = embedded.trim().split("\\s*,\\s*");
for (String activator : activators) try {
Class<?> activatorClass = loader.loadClass(activator);
if (BundleActivator.class.isAssignableFrom(activatorClass)) {
// TODO check immediate
BundleActivator ba = (BundleActivator) activatorClass.getConstructor().newInstance();
ba.start(framework.getBundleContext());
d.activators.add(ba);
}
} catch (Exception e) {
// TODO
System.out.println("IGNORED");
e.printStackTrace();
}
}
return d;
}
Aggregations