private void installAndStartBundles(BundleContext context) throws BundleException { m_framework.start(); StartLevel sl = getStartLevelService(context); for (ProvisionOption bundle : m_bundles) { Bundle b = null; b = context.installBundle(bundle.getURL()); int startLevel = getStartLevel(bundle); sl.setBundleStartLevel(b, startLevel); b.start(); LOG.info("+ Install (start@" + startLevel + ") " + bundle); } LOG.info("++++ Jump to startlevel: " + Constants.START_LEVEL_TEST_BUNDLE); sl.setStartLevel(Constants.START_LEVEL_TEST_BUNDLE); // Work around for FELIX-2942 final CountDownLatch latch = new CountDownLatch(1); context.addFrameworkListener( new FrameworkListener() { public void frameworkEvent(FrameworkEvent frameworkEvent) { switch (frameworkEvent.getType()) { case FrameworkEvent.STARTLEVEL_CHANGED: latch.countDown(); } } }); try { latch.await(m_timeout, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { throw new RuntimeException(e); } }
/** * Install all bundles contained in the specified crates from the given crate location. * * @param context The bundle context to use for interacting with OSGi. * @param location The location containing the crates. * @param crates The set of crates to activate. * @return an ordered list of bundles whose 'start' flag is set to true in their containing crate * @throws CrateException on read error. */ public static List<Bundle> installBundles( BundleContext context, InstallLocation location, Crate... crates) throws CrateException { Logger logger = Logger.getLogger(CrateStarterElf.class); List<Crate> toLoad = expandDependencies(location, crates); Map<String, Bundle> installed = new HashMap<String, Bundle>(); for (Bundle bundle : context.getBundles()) { installed.put(bundle.getSymbolicName(), bundle); } // prevent osgi from trying to be installed. installed.put("org.eclipse.osgi", null); ServiceReference reference = context.getServiceReference(StartLevel.class.getName()); StartLevel start = (StartLevel) context.getService(reference); List<Bundle> bundles = new ArrayList<Bundle>(); List<Bundle> toStart = new LinkedList<Bundle>(); for (Crate crate : toLoad) { for (CrateBundle bundle : crate.getBundles()) { if (installed.containsKey(bundle.getId())) { if (bundle.isStart()) { Bundle alreadyInstalledBundle = installed.get(bundle.getId()); if (alreadyInstalledBundle != null) { int state = alreadyInstalledBundle.getState(); if (state != Bundle.ACTIVE && state != Bundle.STARTING) { toStart.add(alreadyInstalledBundle); } } } continue; } installed.put(bundle.getId(), null); // exclude fragments not applicable for the platform. if (!location.isApplicable(bundle)) { continue; } File file = getFileForBundle(location.getRoot(), bundle); try { Bundle b = context.installBundle("reference:file:" + file.toString()); bundles.add(b); if (bundle.getStartLevel() > 0) { start.setBundleStartLevel(b, bundle.getStartLevel()); } if (bundle.isStart()) { toStart.add(b); } logger.debug("Installed bundle: " + bundle.getId()); } catch (BundleException e) { logger.warn("Error installing bundle: " + bundle.getId(), e); } } } context.ungetService(reference); reference = context.getServiceReference(PackageAdmin.class.getName()); PackageAdmin packageAdmin = (PackageAdmin) context.getService(reference); packageAdmin.resolveBundles(bundles.toArray(new Bundle[bundles.size()])); context.ungetService(reference); return toStart; }
public void setBundleStartLevel(long bundleId, int startLevel) throws TestContainerException { StartLevel sl = getStartLevelService(m_framework.getBundleContext()); sl.setBundleStartLevel(m_framework.getBundleContext().getBundle(bundleId), startLevel); }
public void execute(String s, PrintStream out, PrintStream err) { // Get start level service. ServiceReference ref = m_context.getServiceReference(org.osgi.service.startlevel.StartLevel.class.getName()); if (ref == null) { out.println("StartLevel service is unavailable."); return; } StartLevel sl = (StartLevel) m_context.getService(ref); if (sl == null) { out.println("StartLevel service is unavailable."); return; } // Parse command line. StringTokenizer st = new StringTokenizer(s, " "); // Ignore the command name. st.nextToken(); // If there is only one token, then assume it is // a bundle ID for which we must retrieve the bundle // level. if (st.countTokens() == 1) { // Get the bundle and display start level. Bundle bundle = null; String token = null; try { token = st.nextToken(); long id = Long.parseLong(token); bundle = m_context.getBundle(id); if (bundle != null) { out.println("Bundle " + token + " is level " + sl.getBundleStartLevel(bundle)); } else { err.println("Bundle ID " + token + " is invalid."); } } catch (NumberFormatException ex) { err.println("Unable to parse integer '" + token + "'."); } catch (Exception ex) { err.println(ex.toString()); } } // If there is more than one token, assume the first // token is the new start level and the remaining // tokens are the bundle IDs whose start levels should // be changed. else if (st.countTokens() > 1) { // Get the bundle. Bundle bundle = null; String token = null; int startLevel = -1; try { token = st.nextToken(); startLevel = Integer.parseInt(token); } catch (NumberFormatException ex) { err.println("Unable to parse start level '" + token + "'."); } // Ignore invalid start levels. if (startLevel > 0) { // Set the start level for each specified bundle. while (st.hasMoreTokens()) { try { token = st.nextToken(); long id = Long.parseLong(token); bundle = m_context.getBundle(id); if (bundle != null) { sl.setBundleStartLevel(bundle, startLevel); } else { err.println("Bundle ID '" + token + "' is invalid."); } } catch (NumberFormatException ex) { err.println("Unable to parse bundle ID '" + token + "'."); } catch (Exception ex) { err.println(ex.toString()); } } } else { err.println("Invalid start level."); } } else { err.println("Incorrect number of arguments."); } }