use of org.osgi.framework.launch.FrameworkFactory in project webservices-axiom by apache.
the class UsesConstraintsTest method test.
@Test
public void test() throws Exception {
System.setProperty("java.protocol.handler.pkgs", "org.ops4j.pax.url");
Map<String, String> p = new HashMap<String, String>();
p.put(FRAMEWORK_STORAGE, new File("target/felix").getAbsolutePath());
p.put(FRAMEWORK_STORAGE_CLEAN, FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
FrameworkFactory frameworkFactory = new org.apache.felix.framework.FrameworkFactory();
Framework framework = frameworkFactory.newFramework(p);
framework.init();
BundleContext context = framework.getBundleContext();
Listener listener = new Listener();
context.addFrameworkListener(listener);
List<Bundle> bundles = new ArrayList<Bundle>();
bundles.add(context.installBundle("link:classpath:META-INF/links/org.ops4j.pax.logging.api.link"));
bundles.add(context.installBundle("link:classpath:org.apache.servicemix.specs.stax-api-1.0.link"));
bundles.add(context.installBundle("link:classpath:stax2-api.link"));
bundles.add(context.installBundle("link:classpath:woodstox-core-asl.link"));
bundles.add(context.installBundle("link:classpath:org.apache.james.apache-mime4j-core.link"));
bundles.add(context.installBundle("link:classpath:org.apache.ws.commons.axiom.axiom-api.link"));
// This bundle will be wired to the javax.xml.stream package exported by the ServiceMix stax-api bundle.
bundles.add(context.installBundle(streamBundle(bundle().set(BUNDLE_SYMBOLICNAME, "testbundle1").set(IMPORT_PACKAGE, "org.apache.axiom.om, javax.xml.stream; version=1.0").build()).getURL()));
// This bundle will be wired to the javax.xml.stream package exported by the framework bundle
bundles.add(context.installBundle(streamBundle(bundle().set(BUNDLE_SYMBOLICNAME, "testbundle2").set(IMPORT_PACKAGE, "org.apache.axiom.om, javax.xml.stream; version=\"[0.0.0,1.0)\"").build()).getURL()));
for (Bundle bundle : bundles) {
bundle.start();
}
framework.start();
try {
listener.awaitStart();
assertTrue("Uses constraint violation expected", listener.gotExpectedError());
} finally {
framework.stop();
}
}
use of org.osgi.framework.launch.FrameworkFactory in project sling by apache.
the class FrameworkSetup method call.
public Object call() throws Exception {
final Model model = require(Launcher.MODEL_KEY, Model.class);
final LauncherListener listener = (LauncherListener) get(Launcher.LISTENER_KEY);
log.info("Setting OSGi framework properties");
final Map<String, String> fprops = new FrameworkProperties(model).getProperties(null);
log.info("Starting the OSGi framework");
final FrameworkFactory factory = (FrameworkFactory) getClass().getClassLoader().loadClass("org.apache.felix.framework.FrameworkFactory").newInstance();
final Framework framework = factory.newFramework(fprops);
framework.start();
final RunModeFilter rmFilter = new RunModeFilter();
final Configurations cfg = new Configurations(framework.getBundleContext(), model, rmFilter);
setShutdownHook(framework, new Closeable[] { cfg });
log.info("OSGi framework started");
log.info("Installing bundles from provisioning model");
final BundlesInstaller bi = new BundlesInstaller(model, rmFilter);
final BundleContext bc = framework.getBundleContext();
bi.installBundles(bc, Launcher.NOT_CRANKSTART_FILTER);
cfg.maybeConfigure();
// TODO shall we gradually increase start levels like the launchpad does?? Reuse that DefaultStartupHandler code?
final Bundle[] bundles = bc.getBundles();
log.info("Starting all bundles ({} bundles installed)", bundles.length);
int started = 0;
int failed = 0;
for (Bundle b : bundles) {
if (isFragment(b)) {
started++;
} else {
try {
b.start();
started++;
} catch (BundleException be) {
failed++;
log.warn("Error starting bundle " + b.getSymbolicName(), be);
}
}
cfg.maybeConfigure();
}
if (failed == 0) {
log.info("All {} bundles started.", started);
} else {
log.info("{} bundles started, {} failed to start, total {}", started, failed, bundles.length);
}
log.info("OSGi setup done, waiting for framework to stop");
if (listener != null) {
listener.onStartup(started, failed, bundles.length);
}
framework.waitForStop(0);
if (listener != null) {
listener.onShutdown();
}
return null;
}
use of org.osgi.framework.launch.FrameworkFactory in project bnd by bndtools.
the class DistroCommandTest method setUp.
@Override
protected void setUp() throws Exception {
tmp = IO.getFile("generated/tmp");
tmp.mkdirs();
ServiceLoader<FrameworkFactory> sl = ServiceLoader.load(FrameworkFactory.class, this.getClass().getClassLoader());
FrameworkFactory ff = sl.iterator().next();
Map<String, String> configuration = new HashMap<String, String>();
configuration.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
configuration.put(Constants.FRAMEWORK_STORAGE, new File(tmp, "fwstorage").getAbsolutePath());
framework = ff.newFramework(configuration);
framework.init();
framework.start();
BundleContext context = framework.getBundleContext();
String[] bundles = { "../biz.aQute.remote/generated/biz.aQute.remote.agent.jar", "testdata/bundles/com.liferay.dynamic.data.mapping.taglib.jar", "testdata/bundles/com.liferay.item.selector.taglib.jar" };
for (String bundle : bundles) {
String location = "reference:" + IO.getFile(bundle).toURI().toString();
Bundle b = context.installBundle(location);
b.start();
}
super.setUp();
}
use of org.osgi.framework.launch.FrameworkFactory in project jPOS by jpos.
the class Q2 method startOSGIFramework.
private void startOSGIFramework() throws BundleException {
Iterator<FrameworkFactory> iter = ServiceLoader.load(FrameworkFactory.class, loader).iterator();
if (iter.hasNext()) {
FrameworkFactory frameworkFactory = iter.next();
Map<String, String> config = new HashMap<String, String>();
osgiFramework = frameworkFactory.newFramework(config);
osgiFramework.start();
} else {
getLog().warn("OSGI framework not found");
}
}
use of org.osgi.framework.launch.FrameworkFactory in project bnd by bndtools.
the class Launcher method createFramework.
private Framework createFramework() throws Exception {
Properties p = new Properties();
p.putAll(properties);
File workingdir = null;
if (parms.storageDir != null)
workingdir = parms.storageDir;
else if (parms.keep && parms.name != null) {
workingdir = new File(bnd, parms.name);
}
if (workingdir == null) {
workingdir = File.createTempFile("osgi.", ".fw");
final File wd = workingdir;
Runtime.getRuntime().addShutdownHook(new Thread("launcher::delete temp working dir") {
public void run() {
deleteFiles(wd);
}
});
}
trace("using working dir: %s with keeping=%s", workingdir, parms.keep);
if (!parms.keep && workingdir.exists()) {
trace("deleting working dir %s because not kept", workingdir);
delete(workingdir);
p.setProperty(Constants.FRAMEWORK_STORAGE_CLEAN, "true");
}
IO.mkdirs(workingdir);
if (!workingdir.isDirectory())
throw new IllegalArgumentException("Cannot create a working dir: " + workingdir);
if (System.getProperty(Constants.FRAMEWORK_STORAGE) == null)
p.setProperty(Constants.FRAMEWORK_STORAGE, workingdir.getAbsolutePath());
else
p.setProperty(Constants.FRAMEWORK_STORAGE, System.getProperty(Constants.FRAMEWORK_STORAGE));
if (parms.systemPackages != null) {
p.setProperty(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, parms.systemPackages);
trace("system packages used: %s", parms.systemPackages);
}
if (parms.systemCapabilities != null) {
p.setProperty(FRAMEWORK_SYSTEM_CAPABILITIES_EXTRA, parms.systemCapabilities);
trace("system capabilities used: %s", parms.systemCapabilities);
}
Framework systemBundle;
if (parms.services) {
trace("using META-INF/services");
// 3) framework = null, lookup in META-INF/services
ClassLoader loader = getClass().getClassLoader();
// 3) Lookup in META-INF/services
List<String> implementations = getMetaInfServices(loader, FrameworkFactory.class.getName());
if (implementations.size() == 0)
error("Found no fw implementation");
if (implementations.size() > 1)
error("Found more than one framework implementations: %s", implementations);
String implementation = implementations.get(0);
Class<?> clazz = loader.loadClass(implementation);
FrameworkFactory factory = (FrameworkFactory) clazz.getConstructor().newInstance();
trace("Framework factory %s", factory);
@SuppressWarnings({ "unchecked", "rawtypes" }) Map<String, String> configuration = (Map) p;
systemBundle = factory.newFramework(configuration);
trace("framework instance %s", systemBundle);
} else {
trace("using embedded mini framework because we were told not to use META-INF/services");
// we have to use our own dummy framework
systemBundle = new MiniFramework(p);
}
systemBundle.init();
try {
systemBundle.getBundleContext().addFrameworkListener(new FrameworkListener() {
public void frameworkEvent(FrameworkEvent event) {
switch(event.getType()) {
case FrameworkEvent.ERROR:
case FrameworkEvent.WAIT_TIMEDOUT:
trace("Refresh will end due to error or timeout %s", event.toString());
case FrameworkEvent.PACKAGES_REFRESHED:
inrefresh = false;
trace("refresh ended");
break;
}
}
});
} catch (Exception e) {
trace("could not register a framework listener: %s", e);
}
trace("inited system bundle %s", systemBundle);
return systemBundle;
}
Aggregations