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);
}
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
}
}
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;
}
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();
}
});
}
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);
}
});
}
Aggregations