Search in sources :

Example 11 with StartLevel

use of org.osgi.service.startlevel.StartLevel in project rt.equinox.framework by eclipse.

the class ClassLoadingBundleTests method testBug300692_02.

public void testBug300692_02() throws BundleException {
    // $NON-NLS-1$
    Bundle chainTest = installer.installBundle("chain.test");
    // $NON-NLS-1$
    Bundle chainTestA = installer.installBundle("chain.test.a");
    // $NON-NLS-1$
    Bundle chainTestB = installer.installBundle("chain.test.b");
    // $NON-NLS-1$
    Bundle chainTestC = installer.installBundle("chain.test.c");
    // $NON-NLS-1$
    Bundle chainTestD = installer.installBundle("chain.test.d");
    syncListenerResults.getResults(0);
    StartLevel sl = installer.getStartLevel();
    int currentSL = sl.getStartLevel();
    int testSL = currentSL + 1;
    sl.setBundleStartLevel(chainTest, testSL);
    sl.setBundleStartLevel(chainTestA, testSL);
    sl.setBundleStartLevel(chainTestB, testSL);
    sl.setBundleStartLevel(chainTestC, testSL);
    sl.setBundleStartLevel(chainTestD, testSL);
    installer.resolveBundles(new Bundle[] { chainTest, chainTestA, chainTestB, chainTestC, chainTestD });
    Object[] expectedEvents = new Object[5];
    expectedEvents[0] = new BundleEvent(BundleEvent.RESOLVED, chainTestD);
    expectedEvents[1] = new BundleEvent(BundleEvent.RESOLVED, chainTestC);
    expectedEvents[2] = new BundleEvent(BundleEvent.RESOLVED, chainTestB);
    expectedEvents[3] = new BundleEvent(BundleEvent.RESOLVED, chainTestA);
    expectedEvents[4] = new BundleEvent(BundleEvent.RESOLVED, chainTest);
    Object[] actualEvents = syncListenerResults.getResults(5);
    compareResults(expectedEvents, actualEvents);
    try {
        System.setProperty("test.bug300692", "true");
        System.setProperty("test.bug300692.listener", "true");
        chainTest.start();
        sl.setStartLevel(testSL);
        Object[] expectedFrameworkEvents = new Object[1];
        expectedFrameworkEvents[0] = new FrameworkEvent(FrameworkEvent.STARTLEVEL_CHANGED, OSGiTestsActivator.getContext().getBundle(0), null);
        Object[] actualFrameworkEvents = frameworkListenerResults.getResults(1);
        compareResults(expectedFrameworkEvents, actualFrameworkEvents);
        expectedEvents = new Object[14];
        int i = 0;
        expectedEvents[i++] = new BundleEvent(BundleEvent.LAZY_ACTIVATION, chainTestA);
        expectedEvents[i++] = new BundleEvent(BundleEvent.LAZY_ACTIVATION, chainTestB);
        expectedEvents[i++] = new BundleEvent(BundleEvent.LAZY_ACTIVATION, chainTestC);
        expectedEvents[i++] = new BundleEvent(BundleEvent.LAZY_ACTIVATION, chainTestD);
        expectedEvents[i++] = new BundleEvent(BundleEvent.STARTING, chainTest);
        expectedEvents[i++] = new BundleEvent(BundleEvent.STARTING, chainTestD);
        expectedEvents[i++] = new BundleEvent(BundleEvent.STARTED, chainTestD);
        expectedEvents[i++] = new BundleEvent(BundleEvent.STARTING, chainTestB);
        expectedEvents[i++] = new BundleEvent(BundleEvent.STARTED, chainTestB);
        expectedEvents[i++] = new BundleEvent(BundleEvent.STARTING, chainTestC);
        expectedEvents[i++] = new BundleEvent(BundleEvent.STARTED, chainTestC);
        expectedEvents[i++] = new BundleEvent(BundleEvent.STARTING, chainTestA);
        expectedEvents[i++] = new BundleEvent(BundleEvent.STARTED, chainTestA);
        expectedEvents[i++] = new BundleEvent(BundleEvent.STARTED, chainTest);
        actualFrameworkEvents = syncListenerResults.getResults(14);
        compareResults(expectedEvents, actualFrameworkEvents);
    } finally {
        System.getProperties().remove("test.bug300692");
        System.getProperties().remove("test.bug300692.listener");
        sl.setStartLevel(currentSL);
        Object[] expectedFrameworkEvents = new Object[1];
        expectedFrameworkEvents[0] = new FrameworkEvent(FrameworkEvent.STARTLEVEL_CHANGED, OSGiTestsActivator.getContext().getBundle(0), null);
        Object[] actualFrameworkEvents = frameworkListenerResults.getResults(1);
        compareResults(expectedFrameworkEvents, actualFrameworkEvents);
    }
}
Also used : FrameworkEvent(org.osgi.framework.FrameworkEvent) Bundle(org.osgi.framework.Bundle) StartLevel(org.osgi.service.startlevel.StartLevel) BundleEvent(org.osgi.framework.BundleEvent) Endpoint(javax.xml.ws.Endpoint)

Example 12 with StartLevel

use of org.osgi.service.startlevel.StartLevel in project rt.equinox.framework by eclipse.

the class Activator method start.

public void start(BundleContext context) throws Exception {
    StartLevel sl = (StartLevel) context.getService(context.getServiceReference(StartLevel.class.getName()));
    sl.setStartLevel(10);
}
Also used : StartLevel(org.osgi.service.startlevel.StartLevel)

Example 13 with StartLevel

use of org.osgi.service.startlevel.StartLevel in project Payara by payara.

the class OSGiGlassFishImpl method start.

@Override
public void start() throws GlassFishException {
    super.start();
    StartLevel sl = StartLevel.class.cast(bundleContext.getService(bundleContext.getServiceReference(StartLevel.class.getName())));
    sl.setStartLevel(finalStartLevel);
}
Also used : StartLevel(org.osgi.service.startlevel.StartLevel)

Example 14 with StartLevel

use of org.osgi.service.startlevel.StartLevel in project aries by apache.

the class BundleDataTest method testToCompositeData.

@Test
public void testToCompositeData() throws Exception {
    Bundle bundle = mock(Bundle.class);
    BundleContext context = mock(BundleContext.class);
    PackageAdmin packageAdmin = mock(PackageAdmin.class);
    StartLevel startLevel = mock(StartLevel.class);
    Bundle b1 = mock(Bundle.class);
    when(b1.getSymbolicName()).thenReturn("b1");
    when(b1.getBundleId()).thenReturn(new Long(44));
    Bundle b2 = mock(Bundle.class);
    when(b2.getSymbolicName()).thenReturn("b2");
    when(b2.getBundleId()).thenReturn(new Long(55));
    Bundle b3 = mock(Bundle.class);
    when(b3.getSymbolicName()).thenReturn("b3");
    when(b3.getBundleId()).thenReturn(new Long(66));
    when(context.getBundles()).thenReturn(new Bundle[] { bundle, b1, b2, b3 });
    when(bundle.getSymbolicName()).thenReturn("test");
    when(bundle.getVersion()).thenReturn(Version.emptyVersion);
    when(bundle.getBundleId()).thenReturn(new Long(1));
    when(bundle.getLastModified()).thenReturn(new Long(12345));
    when(bundle.getLocation()).thenReturn("location");
    //headers
    Dictionary<String, String> headers = new Hashtable<String, String>();
    headers.put(Constants.BUNDLE_SYMBOLICNAME, "test");
    headers.put(Constants.BUNDLE_VERSION, "0.0.0");
    when(bundle.getHeaders()).thenReturn(headers);
    //exported packages
    ExportedPackage exported = mock(ExportedPackage.class);
    when(exported.getName()).thenReturn("org.apache.aries.jmx");
    when(exported.getVersion()).thenReturn(new Version("1.0.0"));
    when(exported.getExportingBundle()).thenReturn(bundle);
    when(packageAdmin.getExportedPackages(bundle)).thenReturn(new ExportedPackage[] { exported });
    //imported packages
    ExportedPackage ep1 = mock(ExportedPackage.class);
    when(ep1.getImportingBundles()).thenReturn(new Bundle[] { bundle, b2, b3 });
    when(ep1.getName()).thenReturn("org.apache.aries.jmx.b1");
    when(ep1.getVersion()).thenReturn(Version.emptyVersion);
    when(ep1.getExportingBundle()).thenReturn(b1);
    ExportedPackage ep2 = mock(ExportedPackage.class);
    when(ep2.getImportingBundles()).thenReturn(new Bundle[] { bundle, b3 });
    when(ep2.getName()).thenReturn("org.apache.aries.jmx.b2");
    when(ep2.getVersion()).thenReturn(Version.parseVersion("2.0.1"));
    when(ep2.getExportingBundle()).thenReturn(b2);
    headers.put(Constants.DYNAMICIMPORT_PACKAGE, "*");
    when(packageAdmin.getExportedPackages(b1)).thenReturn(new ExportedPackage[] { ep1 });
    when(packageAdmin.getExportedPackages(b2)).thenReturn(new ExportedPackage[] { ep2 });
    when(packageAdmin.getExportedPackages(b3)).thenReturn(null);
    //required bundles
    RequiredBundle rb1 = mock(RequiredBundle.class);
    when(rb1.getBundle()).thenReturn(b1);
    when(rb1.getRequiringBundles()).thenReturn(new Bundle[] { bundle, b2 });
    RequiredBundle rb2 = mock(RequiredBundle.class);
    when(rb2.getBundle()).thenReturn(b2);
    when(rb2.getRequiringBundles()).thenReturn(new Bundle[] { b1 });
    RequiredBundle rb3 = mock(RequiredBundle.class);
    when(rb3.getBundle()).thenReturn(b3);
    when(rb3.getRequiringBundles()).thenReturn(new Bundle[] { bundle, b1, b2 });
    headers.put(Constants.REQUIRE_BUNDLE, "b1;bundle-version=\"1.0.0\",b3;bundle-version=\"2.0.0\"");
    when(packageAdmin.getRequiredBundles("b1")).thenReturn(new RequiredBundle[] { rb1 });
    when(packageAdmin.getRequiredBundles("b2")).thenReturn(new RequiredBundle[] { rb2 });
    when(packageAdmin.getRequiredBundles("b3")).thenReturn(new RequiredBundle[] { rb3 });
    //services in use
    ServiceReference s1 = mock(ServiceReference.class);
    when(s1.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(15));
    ServiceReference s2 = mock(ServiceReference.class);
    when(s2.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(16));
    ServiceReference s3 = mock(ServiceReference.class);
    when(s3.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(17));
    when(bundle.getServicesInUse()).thenReturn(new ServiceReference[] { s1, s2, s3 });
    BundleData b = new BundleData(context, bundle, packageAdmin, startLevel);
    CompositeData compositeData = b.toCompositeData();
    assertEquals("test", compositeData.get(SYMBOLIC_NAME));
    assertEquals("0.0.0", compositeData.get(VERSION));
    TabularData headerTable = (TabularData) compositeData.get(HEADERS);
    assertEquals(4, headerTable.values().size());
    CompositeData header = headerTable.get(new Object[] { Constants.BUNDLE_SYMBOLICNAME });
    assertNotNull(header);
    String value = (String) header.get(VALUE);
    assertEquals("test", value);
    String key = (String) header.get(KEY);
    assertEquals(Constants.BUNDLE_SYMBOLICNAME, key);
    TabularData bundleTable = new TabularDataSupport(BUNDLES_TYPE);
    bundleTable.put(b.toCompositeData());
    CompositeData bundleData = bundleTable.get(new Object[] { Long.valueOf(1) });
    assertNotNull(bundleData);
    String location = (String) bundleData.get(LOCATION);
    assertEquals("location", location);
    assertArrayEquals(new String[] { "org.apache.aries.jmx;1.0.0" }, (String[]) compositeData.get(EXPORTED_PACKAGES));
    assertArrayEquals(new String[] { "org.apache.aries.jmx.b1;0.0.0", "org.apache.aries.jmx.b2;2.0.1" }, (String[]) compositeData.get(IMPORTED_PACKAGES));
    assertEquals(toSet(new long[] { 44, 55, 66 }), toSet((Long[]) compositeData.get(REQUIRED_BUNDLES)));
    assertArrayEquals(new Long[] { new Long(15), new Long(16), new Long(17) }, (Long[]) compositeData.get(SERVICES_IN_USE));
    //default no return stub
    assertEquals("UNKNOWN", compositeData.get(STATE));
    assertEquals(0, ((Long[]) compositeData.get(HOSTS)).length);
    assertEquals(0, ((Long[]) compositeData.get(FRAGMENTS)).length);
}
Also used : Bundle(org.osgi.framework.Bundle) RequiredBundle(org.osgi.service.packageadmin.RequiredBundle) Hashtable(java.util.Hashtable) CompositeData(javax.management.openmbean.CompositeData) RequiredBundle(org.osgi.service.packageadmin.RequiredBundle) ServiceReference(org.osgi.framework.ServiceReference) TabularData(javax.management.openmbean.TabularData) PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) Version(org.osgi.framework.Version) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) TabularDataSupport(javax.management.openmbean.TabularDataSupport) StartLevel(org.osgi.service.startlevel.StartLevel) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 15 with StartLevel

use of org.osgi.service.startlevel.StartLevel in project aries by apache.

the class BundleFrameworkImpl method increaseStartLevel.

private void increaseStartLevel(BundleContext context) {
    /*
       * Algorithm for doing this
       * 
       * 1. Set up a framework listener that will tell us when the start level has been set.
       * 
       * 2. Change the start level. This is asynchronous so by the time the method returned the event 
       *    could have been sent. This is why we set up the listener in step 1.
       * 
       * 3. Wait until the start level has been set appropriately. At this stage all the bundles are startable
       *    and some have been started (most notably lazy activated bundles it appears). Other bundles are still
       *    in resolved state.
       */
    ServiceReference ref = context.getServiceReference(StartLevel.class.getName());
    if (ref != null) {
        StartLevel sl = (StartLevel) context.getService(ref);
        if (sl != null) {
            final Semaphore waitForStartLevelChangedEventToOccur = new Semaphore(0);
            // step 1
            FrameworkListener listener = new FrameworkListener() {

                public void frameworkEvent(FrameworkEvent event) {
                    if (event.getType() == FrameworkEvent.STARTLEVEL_CHANGED) {
                        waitForStartLevelChangedEventToOccur.release();
                    }
                }
            };
            context.addFrameworkListener(listener);
            // step 2
            sl.setStartLevel(sl.getStartLevel() + 1);
            // step 3
            try {
                if (!!!waitForStartLevelChangedEventToOccur.tryAcquire(60, TimeUnit.SECONDS)) {
                    LOGGER.debug("Starting CBA child bundles took longer than 60 seconds");
                }
            } catch (InterruptedException e) {
                // restore the interrupted status
                Thread.currentThread().interrupt();
            }
            context.removeFrameworkListener(listener);
        }
        context.ungetService(ref);
    }
}
Also used : FrameworkEvent(org.osgi.framework.FrameworkEvent) StartLevel(org.osgi.service.startlevel.StartLevel) Semaphore(java.util.concurrent.Semaphore) FrameworkListener(org.osgi.framework.FrameworkListener) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

StartLevel (org.osgi.service.startlevel.StartLevel)22 Bundle (org.osgi.framework.Bundle)12 FrameworkEvent (org.osgi.framework.FrameworkEvent)10 BundleEvent (org.osgi.framework.BundleEvent)7 ServiceReference (org.osgi.framework.ServiceReference)7 PackageAdmin (org.osgi.service.packageadmin.PackageAdmin)6 Endpoint (javax.xml.ws.Endpoint)4 BundleContext (org.osgi.framework.BundleContext)4 Logger (org.apache.aries.jmx.Logger)3 Test (org.junit.Test)3 BundleException (org.osgi.framework.BundleException)3 File (java.io.File)2 HashMap (java.util.HashMap)2 ExecutorService (java.util.concurrent.ExecutorService)2 MBeanServer (javax.management.MBeanServer)2 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)2 ObjectName (javax.management.ObjectName)2 Equinox (org.eclipse.osgi.launch.Equinox)2 BundleListener (org.osgi.framework.BundleListener)2 Field (java.lang.reflect.Field)1