@Test public void testOsgiInitialBundleStartLevel() throws ResourceNotFoundException, IllegalActionOnResourceException { Resource osgi = get("/osgi"); // get initial bundle start level Integer bundleStartlevel = osgi.getMetadata().get("startlevel.bundle", Integer.class); System.out.println(bundleStartlevel); FrameworkStartLevel fwStartlevel = osgiHelper.getBundle(0).adapt(FrameworkStartLevel.class); assertThat(bundleStartlevel).isEqualTo(fwStartlevel.getInitialBundleStartLevel()); Resource bundles = get("/osgi/bundles"); for (Resource bundle : bundles.getResources()) { BundleResource bundleResource = bundle.adaptTo(BundleResource.class); //System.out.println(bundleResource.getBundleId() + " : " + bundleResource.getStartLevel()); } // set initial bundle start level HashMap<String, Object> params = new HashMap<String, Object>(); params.put("startlevel.bundle", 2); update(osgi.getPath(), params); osgi = get("/osgi"); assertThat(osgi.getMetadata().get("startlevel.bundle")).isEqualTo(2); }
@Override public Resource update(Request request) throws IllegalActionOnResourceException { FrameworkStartLevel frameworkStartLevel = m_frameworkBundle.adapt(FrameworkStartLevel.class); Integer bundleStartLevel = request.get(STARTLEVEL_BUNDLE_PARAMETER, Integer.class); if (bundleStartLevel != null) { frameworkStartLevel.setInitialBundleStartLevel(bundleStartLevel); } Integer startLevel = request.get(STARTLEVEL_PARAMETER, Integer.class); if (startLevel != null) { frameworkStartLevel.setStartLevel(startLevel); } Boolean restart = request.get(FRAMEWORK_RESTART_PARAMETER, Boolean.class); if (restart != null && restart) { try { //restarting framework m_frameworkBundle.update(); } catch (BundleException e) { throw new IllegalActionOnResourceException(request, e.getMessage()); } } return this; }
@Before public void setUp() { m_everest = new Everest(); // Create a fake bundle context. Bundle zero = mock(Bundle.class, RETURNS_MOCKS); BundleContext context = mock(BundleContext.class, RETURNS_MOCKS); when(zero.getBundleContext()).thenReturn(context); when(context.getBundle(0)).thenReturn(zero); when(context.getProperty(anyString())).thenReturn("Some Property"); when(zero.adapt(FrameworkWiring.class)).thenReturn(mock(FrameworkWiring.class)); when(zero.adapt(FrameworkStartLevel.class)).thenReturn(mock(FrameworkStartLevel.class)); m_osgi = new OsgiRootResource(context); m_everest.bindRootResource(m_osgi); }
public UpgradeProcess(final UpgradeServiceImpl bundleDeployerService, final BundleContext systemBundleContext) { this.bundleDeployerService = bundleDeployerService; this.systemBundleContext = systemBundleContext; // Refresh classes must be initialized first because they will be not available if the richconsole re-deploys // itself final AtomicBoolean refreshFinished = new AtomicBoolean(false); Lock refreshFinishLock = new ReentrantLock(); Condition refreshFinishCondition = refreshFinishLock.newCondition(); refreshListener = new FrameworkRefreshListener(refreshFinished, refreshFinishLock, refreshFinishCondition); Bundle systemBundle = systemBundleContext.getBundle(); frameworkWiring = systemBundle.adapt(FrameworkWiring.class); frameworkStartLevel = systemBundle.adapt(FrameworkStartLevel.class); originalFrameworkStartLevelValue = frameworkStartLevel.getStartLevel(); currentFrameworkStartLevelValue = originalFrameworkStartLevelValue; }
@Override public void start ( final BundleContext bundleContext ) throws Exception { this.context = bundleContext; final ServiceReference<FrameworkStartLevel> frameworkStartLevel = this.context.getServiceReference ( FrameworkStartLevel.class ); if ( frameworkStartLevel != null ) { final FrameworkStartLevel service = this.context.getService ( frameworkStartLevel ); if ( service != null ) { try { this.defaultStartLevel = service.getInitialBundleStartLevel (); } finally { this.context.ungetService ( frameworkStartLevel ); } } } loadStartLevels (); for ( final Map.Entry<String, Integer> entry : this.bundleStartList.entrySet () ) { setStartLevel ( entry.getKey (), entry.getValue () ); } }
/** * Install all features listed under "nexus-features". */ private void installNexusFeatures(final String featureNames) throws Exception { final Set<Feature> features = new LinkedHashSet<>(); for (final String name : Splitter.on(',').trimResults().omitEmptyStrings().split(featureNames)) { final Feature feature = featuresService.getFeature(name); if (feature != null) { features.add(feature); } else { log.warn("Missing: {}", name); } } log.info("Installing: {}", features); Set<String> featureIds = new HashSet<>(features.size()); for (final Feature f : features) { // feature might already be installed in the cache; if so then skip installation if (!featuresService.isInstalled(f)) { featureIds.add(f.getId()); } } if (!featureIds.isEmpty()) { // avoid auto-refreshing bundles as that could trigger unwanted restart/lifecycle events EnumSet<Option> options = EnumSet.of(NoAutoRefreshBundles, NoAutoRefreshManagedBundles); featuresService.installFeatures(featureIds, options); } log.info("Installed: {}", features); // feature bundles have all been installed, so raise framework start level to finish activation FrameworkStartLevel frameworkStartLevel = bundleContext.getBundle(0).adapt(FrameworkStartLevel.class); if (frameworkStartLevel.getStartLevel() < NEXUS_PLUGIN_START_LEVEL) { frameworkStartLevel.setStartLevel(NEXUS_PLUGIN_START_LEVEL, this); // activation continues asynchronously in frameworkEvent method... } }
@Test public void testOsgiStartLevel() throws ResourceNotFoundException, IllegalActionOnResourceException, InterruptedException { Resource osgi = get("/osgi"); // get bundle start level Integer startlevel = osgi.getMetadata().get("startlevel", Integer.class); System.out.println("startlevel: " + startlevel); FrameworkStartLevel fwStartlevel = osgiHelper.getBundle(0).adapt(FrameworkStartLevel.class); assertThat(startlevel).isEqualTo(fwStartlevel.getStartLevel()); updatedEvents.clear(); // set start level HashMap<String, Object> params = new HashMap<String, Object>(); params.put("startlevel", 3); update(osgi.getPath(), params); // wait for start level passage System.out.println("Waiting for start level passage"); try { Thread.sleep(TimeUtils.TIME_FACTOR * 1000); } catch (InterruptedException e) { // Interrupted } osgi = get("/osgi"); startlevel = osgi.getMetadata().get("startlevel", Integer.class); assertThat(startlevel).isEqualTo(3); testUpdatedEventFrom("/osgi"); Resource bundles = get("/osgi/bundles"); for (Resource bundle : bundles.getResources()) { BundleResource bundleResource = bundle.adaptTo(BundleResource.class); if (bundleResource.getStartLevel() > startlevel) { assertThat(bundleResource.getState()).isEqualTo(OsgiResourceUtils.bundleStateToString(Bundle.RESOLVED)); } //System.out.println(bundleResource.getBundleId() + " :"+bundleResource.getSymbolicName()+"-"+bundleResource.getState()+" : " + bundleResource.getStartLevel()); } }
@Override public ResourceMetadata getMetadata() { ImmutableResourceMetadata.Builder metadataBuilder = new ImmutableResourceMetadata.Builder(m_metadata); // add Start Level metadata FrameworkStartLevel frameworkStartLevel = m_frameworkBundle.adapt(FrameworkStartLevel.class); metadataBuilder.set(STARTLEVEL_BUNDLE_PARAMETER, frameworkStartLevel.getInitialBundleStartLevel()); metadataBuilder.set(STARTLEVEL_PARAMETER, frameworkStartLevel.getStartLevel()); return metadataBuilder.build(); }
@Override public <A> A adaptTo(Class<A> clazz) { if (Bundle.class.equals(clazz)) { return (A) m_frameworkBundle; } else if (FrameworkWiring.class.equals(clazz)) { return (A) m_frameworkBundle.adapt(FrameworkWiring.class); } else if (FrameworkStartLevel.class.equals(clazz)) { return (A) m_frameworkBundle.adapt(FrameworkStartLevel.class); } else { return null; } }