下面列出了org.osgi.framework.Bundle#stop ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Tests the loading of the 1.2 Compatibility API bundle, its classes should be loadable from the Core bundle,
* and the class loader should be the same between a class from core and a class from compat
*/
@Test
public void testLog4J12Fragement() throws BundleException, ReflectiveOperationException {
final Bundle api = getApiBundle();
final Bundle plugins = getPluginsBundle();
final Bundle core = getCoreBundle();
final Bundle compat = get12ApiBundle();
api.start();
plugins.start();
core.start();
final Class<?> coreClassFromCore = core.loadClass("org.apache.logging.log4j.core.Core");
final Class<?> levelClassFrom12API = core.loadClass("org.apache.log4j.Level");
final Class<?> levelClassFromAPI = core.loadClass("org.apache.logging.log4j.Level");
Assert.assertEquals("expected 1.2 API Level to have the same class loader as Core", levelClassFrom12API.getClassLoader(), coreClassFromCore.getClassLoader());
Assert.assertNotEquals("expected 1.2 API Level NOT to have the same class loader as API Level", levelClassFrom12API.getClassLoader(), levelClassFromAPI.getClassLoader());
core.stop();
api.stop();
uninstall(api, plugins, core, compat);
}
private void restartHttpServiceWhiteboard() throws Exception {
BundleWiring runtimeWiring = _runtimeServiceReference.getBundle().adapt(BundleWiring.class);
Bundle httpService = runtimeWiring.getRequiredWires("osgi.implementation").stream()
.filter(bw -> "osgi.http".equals(bw.getCapability().getAttributes().get("osgi.implementation")))
.map(bw -> bw.getProvider().getBundle())
.findFirst().get();
try {
httpService.stop();
} finally {
httpService.start();
}
_runtime = _runtimeTracker.waitForService(5000);
_runtimeServiceReference = _runtimeTracker.getServiceReference();
}
protected void stopFramework0()
{
final Object[] options = { Strings.get("yes"), Strings.get("cancel") };
final int n =
JOptionPane.showOptionDialog(frame, Strings.get("q_stopframework"),
Strings.get("msg_stopframework"),
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options,
options[1]);
if (n == 0) {
try {
final Bundle sysBundle = Activator.getBC().getBundle(0);
sysBundle.stop();
} catch (final Exception e) {
showErr("Failed to stop bundle.", e);
}
}
}
private int doStopBundle(final String[] args, int i, final int options)
{
assertFramework();
if (i+1 < args.length) {
final long id = getBundleID(framework, args[i+1]);
final Bundle b = framework.getBundleContext().getBundle(id);
try {
b.stop(options);
println("Stopped: ", b);
} catch (final Exception e) {
error("Failed to stop", e);
}
i++;
} else {
error("No ID for stop command");
}
return i;
}
@Test
public void assertThatARuleSwitchesFromIDLEtoUNINITIALIZEDifAModuleHandlerDisappearsAndBackToIDLEifItAppearsAgain()
throws BundleException {
logger.info(
"assert that a rule switches from IDLE to UNINITIALIZED if a moduleHanlder disappears and back to IDLE if it appears again");
Rule rule = createSimpleRule();
ruleRegistry.add(rule);
assertThat(ruleEngine.getStatusInfo(rule.getUID()).getStatus(), is(RuleStatus.IDLE));
Bundle moduleBundle = FrameworkUtil.getBundle(GenericEventTriggerHandler.class);
moduleBundle.stop();
waitForAssert(() -> {
logger.info("RuleStatus: {}", ruleEngine.getStatusInfo(rule.getUID()).getStatus());
assertThat(ruleEngine.getStatusInfo(rule.getUID()).getStatus(), is(RuleStatus.UNINITIALIZED));
}, 3000, 100);
moduleBundle.start();
ruleEngine.setEnabled(rule.getUID(), true);
waitForAssert(() -> {
logger.info("RuleStatus: {}", ruleEngine.getStatusInfo(rule.getUID()));
assertThat(ruleEngine.getStatusInfo(rule.getUID()).getStatus(), is(RuleStatus.IDLE));
}, 3000, 100);
}
@Override
protected void reload(Module m) throws IOException {
try {
Bundle b = findBundle(m.getCodeNameBase());
b.stop();
fakeOneModule(m, b);
} catch (BundleException ex) {
throw new IOException(ex);
}
}
public void stopBundle(long id) throws BundleException {
Bundle b = context.getBundle(id);
if(b == null)
return;
b.stop();
}
public void stopBundle(long id, int options) throws BundleException {
Bundle b = context.getBundle(id);
if(b == null)
return;
b.stop(options);
}
@Override
public Representation put(final Representation value,
final Variant variant) {
try {
final Bundle bundle = getBundleFromKeys(RestService.BUNDLE_ID_KEY);
if (bundle == null) {
setStatus(Status.CLIENT_ERROR_NOT_FOUND);
return null;
}
final BundleStatePojo targetState = fromRepresentation(value,
value.getMediaType());
if (bundle.getState() == Bundle.UNINSTALLED) {
return ERROR(Status.CLIENT_ERROR_PRECONDITION_FAILED, "target state "
+ targetState.getState() + " not reachable from the current state");
} else if (targetState.getState() == Bundle.ACTIVE) {
bundle.start(targetState.getOptions());
return getRepresentation(
new BundleStatePojo(bundle.getState()), variant);
} else if (targetState.getState() == Bundle.RESOLVED) {
bundle.stop(targetState.getOptions());
return getRepresentation(
new BundleStatePojo(bundle.getState()), variant);
} else {
return ERROR(Status.CLIENT_ERROR_BAD_REQUEST, "target state "
+ targetState.getState() + " not supported");
}
} catch (final Exception e) {
return ERROR(e, variant);
}
}
void shutdown()
{
try {
final Bundle systemBundle = Activator.bc.getBundle(0);
systemBundle.stop();
} catch (final Exception e) {
Activator.log.error("Failed to shutdown", e);
}
}
public boolean runTest() {
try {
Bundle bundle = Util.installBundle(bc, "bundleEnd1_test-1.0.0.jar");
bundle.start();
bundle.stop();
bundle.uninstall();
} catch (BundleException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* Tests LOG4J2-1637.
*/
@Test
public void testClassNotFoundErrorLogger() throws BundleException {
final Bundle api = getApiBundle();
final Bundle plugins = getPluginsBundle();
final Bundle core = getCoreBundle();
api.start();
plugins.start();
// fails if LOG4J2-1637 is not fixed
try {
core.start();
}
catch (final BundleException ex) {
boolean shouldRethrow = true;
final Throwable t = ex.getCause();
if (t != null) {
final Throwable t2 = t.getCause();
if (t2 != null) {
final String cause = t2.toString();
final boolean result = cause.equals("java.lang.ClassNotFoundException: org.apache.logging.log4j.Logger") // Equinox
|| cause.equals("java.lang.ClassNotFoundException: org.apache.logging.log4j.Logger not found by org.apache.logging.log4j.core [2]"); // Felix
Assert.assertFalse("org.apache.logging.log4j package is not properly imported in org.apache.logging.log4j.core bundle, check that the package is exported from api and is not split between api and core", result);
shouldRethrow = !result;
}
}
if (shouldRethrow) {
throw ex; // rethrow if the cause of the exception is something else
}
}
core.stop();
plugins.stop();
api.stop();
core.uninstall();
plugins.uninstall();
api.uninstall();
}
public static void systemExitWrapper(int code, long bid, Object context) {
Activator.println("CP.systemExit code=" + code + ", bid=" + bid + ", context=" + context);
try {
// NYI, we only handle framework start via Main.
Bundle b = getBundle(bid);
Activator.println("stopping " + b);
b.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
@RequestMapping ( value = "/bundles/{id}/stop", method = RequestMethod.POST )
public ModelAndView stopBundle ( @PathVariable ( "id" ) final long bundleId ) throws BundleException
{
final Bundle bundle = this.context.getBundle ( bundleId );
if ( bundle != null )
{
bundle.stop ();
}
return new ModelAndView ( "redirect:/system/info/framework/bundles" );
}
private void stopEventingBundle() throws BundleException {
for (Bundle bundle : bundleContext.getBundles()) {
if (bundle.getSymbolicName().equals(BUNDLE_SYMBOLIC_NAME)) {
bundle.stop();
break;
}
}
}
@Test
public void testManagementOfABundleContainingInternationalizationFiles() throws FileNotFoundException, BundleException {
Bundle bundle = osgi.installAndStart("local:/i18n",
bundle()
.add("i18n/app.properties", new FileInputStream("src/test/resources/integration/app.properties"))
.add("i18n/app_fr.properties", new FileInputStream("src/test/resources/integration/app_fr.properties"))
.build());
bundles.add(bundle);
Map<String, String> messages = service.getAllMessages(Locale.FRENCH);
assertThat(messages).contains(
MapEntry.entry("app.name", "mon application")
);
messages = service.getAllMessages(Locale.CHINA);
assertThat(messages).contains(
MapEntry.entry("app.name", "my application")
);
messages = service.getAllMessages(Locale.ENGLISH);
assertThat(messages).contains(
MapEntry.entry("app.name", "my application")
);
messages = service.getAllMessages(Locale.GERMAN);
assertThat(messages).contains(
MapEntry.entry("app.name", "my application")
);
bundle.stop();
messages = service.getAllMessages(Locale.FRENCH);
assertThat(messages).doesNotContainKey("app.name");
messages = service.getAllMessages(Locale.GERMAN);
assertThat(messages).doesNotContainKey("app.name");
}
/**
* Tests starting, then stopping, then restarting, then stopping, and finally uninstalling the API and Core bundles
*/
@Test
public void testApiCoreStartStopStartStop() throws BundleException {
final Bundle api = getApiBundle();
final Bundle plugins = getPluginsBundle();
final Bundle core = getCoreBundle();
Assert.assertEquals("api is not in INSTALLED state", Bundle.INSTALLED, api.getState());
Assert.assertEquals("plugins is not in INSTALLED state", Bundle.INSTALLED, plugins.getState());
Assert.assertEquals("core is not in INSTALLED state", Bundle.INSTALLED, core.getState());
api.start();
plugins.start();
core.start();
Assert.assertEquals("api is not in ACTIVE state", Bundle.ACTIVE, api.getState());
Assert.assertEquals("plugins is not in ACTIVE state", Bundle.ACTIVE, plugins.getState());
Assert.assertEquals("core is not in ACTIVE state", Bundle.ACTIVE, core.getState());
core.stop();
plugins.stop();
api.stop();
Assert.assertEquals("api is not in RESOLVED state", Bundle.RESOLVED, api.getState());
Assert.assertEquals("plugins is not in RESOLVED state", Bundle.RESOLVED, plugins.getState());
Assert.assertEquals("core is not in RESOLVED state", Bundle.RESOLVED, core.getState());
api.start();
plugins.start();
core.start();
Assert.assertEquals("api is not in ACTIVE state", Bundle.ACTIVE, api.getState());
Assert.assertEquals("plugins is not in ACTIVE state", Bundle.ACTIVE, plugins.getState());
Assert.assertEquals("core is not in ACTIVE state", Bundle.ACTIVE, core.getState());
core.stop();
plugins.stop();
api.stop();
Assert.assertEquals("api is not in RESOLVED state", Bundle.RESOLVED, api.getState());
Assert.assertEquals("plugins is not in RESOLVED state", Bundle.RESOLVED, plugins.getState());
Assert.assertEquals("core is not in RESOLVED state", Bundle.RESOLVED, core.getState());
core.uninstall();
plugins.uninstall();
api.uninstall();
Assert.assertEquals("api is not in UNINSTALLED state", Bundle.UNINSTALLED, api.getState());
Assert.assertEquals("plugins is not in UNINSTALLED state", Bundle.UNINSTALLED, plugins.getState());
Assert.assertEquals("core is not in UNINSTALLED state", Bundle.UNINSTALLED, core.getState());
}
/**
* In this test, a first bundle provides the default and french resources, while another one provides the german
* version.
*/
@Test
public void testWithTwoBundles() throws FileNotFoundException, BundleException {
Bundle bundle1 = osgi.installAndStart("local:/i18n",
bundle()
.add("i18n/app.properties", new FileInputStream("src/test/resources/integration/app.properties"))
.add("i18n/app_fr.properties", new FileInputStream("src/test/resources/integration/app_fr.properties"))
.build());
bundles.add(bundle1);
Bundle bundle2 = osgi.installAndStart("local:/i18n_de", bundle()
.add("i18n/app_de.properties", new FileInputStream("src/test/resources/integration/app_de.properties"))
.build());
bundles.add(bundle2);
Map<String, String> messages = service.getAllMessages(Locale.FRENCH);
assertThat(messages).contains(
MapEntry.entry("app.name", "mon application")
);
messages = service.getAllMessages(Locale.CHINA);
assertThat(messages).contains(
MapEntry.entry("app.name", "my application")
);
messages = service.getAllMessages(Locale.ENGLISH);
assertThat(messages).contains(
MapEntry.entry("app.name", "my application")
);
messages = service.getAllMessages(Locale.GERMAN);
assertThat(messages).contains(
MapEntry.entry("app.name", "Meine Software")
);
bundle1.stop();
messages = service.getAllMessages(Locale.FRENCH);
assertThat(messages).doesNotContainKey("app.name");
messages = service.getAllMessages(Locale.GERMAN);
assertThat(messages).containsEntry("app.name", "Meine Software");
bundle2.stop();
messages = service.getAllMessages(Locale.GERMAN);
assertThat(messages).doesNotContainEntry("app.name", "Meine Software");
}
private void stop(final Bundle api, final Bundle plugins, final Bundle core, final Bundle dummy) throws BundleException {
dummy.stop();
core.stop();
plugins.stop();
api.stop();
}
/**
* Undeploys a bundle.
*
* @param managedBundle
* the bundle to be undeployed.
* @throws BundleException
* bundle exception.
*/
public static void undeployBundle(final Bundle managedBundle) throws BundleException {
managedBundle.stop(Bundle.STOP_TRANSIENT);
managedBundle.uninstall();
}