Search in sources :

Example 66 with BundleException

use of org.osgi.framework.BundleException in project aries by apache.

the class FrameworkTest method testInstallBundlesFromURL.

@Test
public void testInstallBundlesFromURL() throws Exception {
    Bundle bundle = Mockito.mock(Bundle.class);
    Mockito.when(context.installBundle(Mockito.anyString(), Mockito.any(InputStream.class))).thenReturn(bundle);
    Mockito.when(bundle.getBundleId()).thenReturn(Long.valueOf(2));
    Framework spiedMBean = Mockito.spy(mbean);
    InputStream stream = Mockito.mock(InputStream.class);
    Mockito.doReturn(stream).when(spiedMBean).createStream(Mockito.anyString());
    CompositeData data = spiedMBean.installBundlesFromURL(new String[] { "file:test.jar" }, new String[] { "test.jar" });
    Assert.assertNotNull(data);
    BatchInstallResult batch = BatchInstallResult.from(data);
    Assert.assertEquals(2, batch.getCompleted()[0]);
    Assert.assertTrue(batch.isSuccess());
    Assert.assertNull(batch.getError());
    Assert.assertNull(batch.getRemainingLocationItems());
    Mockito.reset(context);
    Mockito.when(spiedMBean.createStream(Mockito.anyString())).thenReturn(stream);
    Mockito.when(context.installBundle(Mockito.anyString(), Mockito.any(InputStream.class))).thenThrow(new BundleException("location doesn't exist"));
    CompositeData data2 = spiedMBean.installBundlesFromURL(new String[] { "file:test.jar" }, new String[] { "test.jar" });
    BatchInstallResult batch2 = BatchInstallResult.from(data2);
    Assert.assertNotNull(batch2);
    Assert.assertNotNull(batch2.getCompleted());
    Assert.assertEquals(0, batch2.getCompleted().length);
    Assert.assertFalse(batch2.isSuccess());
    Assert.assertNotNull(batch2.getError());
    Assert.assertEquals("file:test.jar", batch2.getBundleInError());
    Assert.assertNotNull(batch2.getRemainingLocationItems());
    Assert.assertEquals(0, batch2.getRemainingLocationItems().length);
}
Also used : BatchInstallResult(org.apache.aries.jmx.codec.BatchInstallResult) Bundle(org.osgi.framework.Bundle) InputStream(java.io.InputStream) CompositeData(javax.management.openmbean.CompositeData) BundleException(org.osgi.framework.BundleException) Test(org.junit.Test)

Example 67 with BundleException

use of org.osgi.framework.BundleException in project aries by apache.

the class FrameworkTest method testStartBundle.

@Test
public void testStartBundle() throws Exception {
    Bundle bundle = Mockito.mock(Bundle.class);
    Mockito.when(context.getBundle(5)).thenReturn(bundle);
    mbean.startBundle(5);
    Mockito.verify(bundle).start();
    Mockito.reset(context);
    Mockito.when(context.getBundle(6)).thenReturn(bundle);
    Mockito.doThrow(new BundleException("")).when(bundle).start();
    try {
        mbean.startBundle(6);
        Assert.fail("Shouldn't go to this stage, BundleException was thrown");
    } catch (IOException ioe) {
    // expected
    }
    Mockito.when(context.getBundle(6)).thenReturn(null);
    try {
        mbean.startBundle(6);
        Assert.fail("IOException should be thrown");
    } catch (IOException e) {
    //expected
    }
}
Also used : Bundle(org.osgi.framework.Bundle) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException) Test(org.junit.Test)

Example 68 with BundleException

use of org.osgi.framework.BundleException in project aries by apache.

the class Utils method getBundleContext.

private static BundleContext getBundleContext(ClassLoader cl2) {
    ClassLoader cl = cl2;
    BundleContext result = null;
    while (result == null && cl != null) {
        if (cl instanceof BundleReference) {
            Bundle b = ((BundleReference) cl).getBundle();
            result = b.getBundleContext();
            if (result == null) {
                try {
                    b.start();
                    result = b.getBundleContext();
                } catch (BundleException e) {
                }
                break;
            }
        } else if (cl != null) {
            cl = cl.getParent();
        }
    }
    return result;
}
Also used : Bundle(org.osgi.framework.Bundle) BundleReference(org.osgi.framework.BundleReference) BundleException(org.osgi.framework.BundleException) BundleContext(org.osgi.framework.BundleContext)

Example 69 with BundleException

use of org.osgi.framework.BundleException in project aries by apache.

the class StartAction method startBundleResource.

private void startBundleResource(Resource resource, Coordination coordination) throws BundleException {
    if (target.isRoot())
        // root region.
        return;
    if (Utils.isRegionContextBundle(resource))
        // The region context bundle was persistently started elsewhere.
        return;
    final Bundle bundle = ((BundleRevision) resource).getBundle();
    if ((bundle.getState() & (Bundle.STARTING | Bundle.ACTIVE)) != 0)
        return;
    if (logger.isDebugEnabled()) {
        int bundleStartLevel = bundle.adapt(BundleStartLevel.class).getStartLevel();
        Bundle systemBundle = Activator.getInstance().getBundleContext().getBundle(0);
        int fwStartLevel = systemBundle.adapt(FrameworkStartLevel.class).getStartLevel();
        logger.debug("StartAction: starting bundle " + bundle.getSymbolicName() + " " + bundle.getVersion().toString() + " bundleStartLevel=" + bundleStartLevel + " frameworkStartLevel=" + fwStartLevel);
    }
    bundle.start(Bundle.START_TRANSIENT | Bundle.START_ACTIVATION_POLICY);
    if (logger.isDebugEnabled()) {
        logger.debug("StartAction: bundle " + bundle.getSymbolicName() + " " + bundle.getVersion().toString() + " started correctly");
    }
    if (coordination == null)
        return;
    coordination.addParticipant(new Participant() {

        public void ended(Coordination coordination) throws Exception {
        // noop
        }

        public void failed(Coordination coordination) throws Exception {
            bundle.stop();
        }
    });
}
Also used : BundleStartLevel(org.osgi.framework.startlevel.BundleStartLevel) Participant(org.osgi.service.coordinator.Participant) Coordination(org.osgi.service.coordinator.Coordination) Bundle(org.osgi.framework.Bundle) BundleRevision(org.osgi.framework.wiring.BundleRevision) FrameworkStartLevel(org.osgi.framework.startlevel.FrameworkStartLevel) BundleException(org.osgi.framework.BundleException) SubsystemException(org.osgi.service.subsystem.SubsystemException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) CoordinationException(org.osgi.service.coordinator.CoordinationException)

Example 70 with BundleException

use of org.osgi.framework.BundleException in project aries by apache.

the class StartAction method setExportIsolationPolicy.

private static void setExportIsolationPolicy(final BasicSubsystem subsystem, Coordination coordination) throws InvalidSyntaxException {
    if (!subsystem.isComposite())
        return;
    final Region from = ((BasicSubsystem) subsystem.getParents().iterator().next()).getRegion();
    final Region to = subsystem.getRegion();
    RegionFilterBuilder builder = from.getRegionDigraph().createRegionFilterBuilder();
    setExportIsolationPolicy(builder, subsystem.getDeploymentManifest().getExportPackageHeader(), subsystem);
    setExportIsolationPolicy(builder, subsystem.getDeploymentManifest().getProvideCapabilityHeader(), subsystem);
    setExportIsolationPolicy(builder, subsystem.getDeploymentManifest().getSubsystemExportServiceHeader(), subsystem);
    RegionFilter regionFilter = builder.build();
    if (regionFilter.getSharingPolicy().isEmpty())
        return;
    if (logger.isDebugEnabled())
        logger.debug("Establishing region connection: from=" + from + ", to=" + to + ", filter=" + regionFilter);
    try {
        from.connectRegion(to, regionFilter);
    } catch (BundleException e) {
        // been set. Bad assumption?
        return;
    }
    coordination.addParticipant(new Participant() {

        @Override
        public void ended(Coordination coordination) throws Exception {
            // It may be necessary to rollback the export sharing policy
            // even when the coordination did not fail. For example, this
            // might have been a subsystem whose export sharing policy was
            // set just in case it offered dependencies for some other
            // subsystem.
            unsetExportIsolationPolicyIfNecessary();
        }

        @Override
        public void failed(Coordination coordination) throws Exception {
        // Nothing to do because a coordination is always ended.
        }

        private void unsetExportIsolationPolicyIfNecessary() throws BundleException, InvalidSyntaxException {
            if (!EnumSet.of(State.INSTALLING, State.INSTALLED).contains(subsystem.getState())) {
                // does not require a rollback.
                return;
            }
            // The subsystem is either INSTALLING or INSTALLED and therefore
            // requires a rollback since the export sharing policy must only
            // be set upon entering the RESOLVED state.
            RegionUpdater updater = new RegionUpdater(from, to);
            updater.addRequirements(null);
        }
    });
}
Also used : RegionFilter(org.eclipse.equinox.region.RegionFilter) Participant(org.osgi.service.coordinator.Participant) Coordination(org.osgi.service.coordinator.Coordination) Region(org.eclipse.equinox.region.Region) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BundleException(org.osgi.framework.BundleException) RegionFilterBuilder(org.eclipse.equinox.region.RegionFilterBuilder) BundleException(org.osgi.framework.BundleException) SubsystemException(org.osgi.service.subsystem.SubsystemException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) CoordinationException(org.osgi.service.coordinator.CoordinationException)

Aggregations

BundleException (org.osgi.framework.BundleException)99 Bundle (org.osgi.framework.Bundle)54 IOException (java.io.IOException)31 Test (org.junit.Test)19 File (java.io.File)15 ArrayList (java.util.ArrayList)13 InputStream (java.io.InputStream)10 FileInputStream (java.io.FileInputStream)9 BundleContext (org.osgi.framework.BundleContext)9 HashMap (java.util.HashMap)8 Map (java.util.Map)7 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)7 Hashtable (java.util.Hashtable)5 Manifest (java.util.jar.Manifest)5 ServiceReference (org.osgi.framework.ServiceReference)5 Version (org.osgi.framework.Version)5 BundleStartLevel (org.osgi.framework.startlevel.BundleStartLevel)5 LowDiskException (android.taobao.atlas.runtime.LowDiskException)4 TimeoutException (java.util.concurrent.TimeoutException)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4