下面列出了org.osgi.framework.BundleException#printStackTrace ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void prepare() {
try {
b1 = Util.installBundle(bc, "bundleEnd101_test-1.0.0.jar");
b2 = Util.installBundle(bc, "bundleEnd102_test-1.0.0.jar");
b1.start();
b2.start();
tracker1 = new ServiceTracker(bc, Control101.class.getName(), null);
tracker2 = new ServiceTracker(bc, Control102.class.getName(), null);
tracker1.open();
tracker2.open();
tracker = new ServiceTracker(bc, Control.class.getName(), null);
tracker.open();
} catch (BundleException e) {
e.printStackTrace();
}
}
public boolean runTest() {
try {
Bundle bundle = Util.installBundle(bc, "bundleEnd151_test-1.0.0.jar");
bundle.start();
for (int i = 0; i < loops; i++) {
for (int o = 0; o < locales.length; o++) {
bundle.getHeaders(locales[o]);
}
}
bundle.uninstall();
} catch (BundleException e) {
e.printStackTrace();
}
return true;
}
public boolean runTest() {
try {
Bundle fragment = Util.installBundle(bc, "bundleEnd152_test-1.0.0.jar");
Bundle bundle = Util.installBundle(bc, "bundleEnd151_test-1.0.0.jar");
bundle.start();
for (int i = 0; i < loops; i++) {
for (int o = 0; o < locales.length; o++) {
bundle.getHeaders(locales[o]);
}
}
bundle.uninstall();
fragment.uninstall();
} catch (BundleException e) {
e.printStackTrace();
}
return true;
}
public void run() {
Thread currentThread = Thread.currentThread();
try {
while (currentThread == thread) {
try {
Bundle bundle = context.installBundle(TEST_BUNDLE_LOCATION);
Thread.sleep(100);
bundle.start();
Thread.sleep(100);
bundle.stop();
Thread.sleep(100);
bundle.uninstall();
Thread.sleep(100);
} catch (InterruptedException ignore) { }
}
} catch (BundleException be) {
be.printStackTrace(System.err);
}
}
private static void uninstall ( final PrintWriter pw, final List<Bundle> bundles )
{
for ( final Bundle b : bundles )
{
try
{
pw.format ( "Uninstalling bundle: %s%n", b.getBundleId () );
b.uninstall ();
}
catch ( final BundleException e1 )
{
pw.format ( "Failed uninstalling bundle: %s%n", b.getBundleId () );
e1.printStackTrace ( pw );
// ignore
}
}
bundles.clear ();
}
/**
* @see org.osgi.framework.Bundle#getResources(String)
* @category Bundle
*/
public Enumeration<URL> getResources(final String name) throws IOException {
if (state == UNINSTALLED) {
throw new IllegalStateException("Bundle is uninstalled");
}
if (currentRevision.isFragment()) {
// bundle is fragment, return null
return null;
}
if (state == INSTALLED) {
try {
if (!currentRevision.resolve(false)) {
final Vector<URL> result = new Vector<URL>();
for (int i = 0; i < currentRevision.classpath.length; i++) {
final URL url = currentRevision
.lookupFile(currentRevision.classpath[i], name);
if (url != null) {
result.add(url);
}
}
return result.isEmpty() ? null : result.elements();
}
} catch (final BundleException e) {
// TODO: to log
e.printStackTrace();
return null;
}
}
return currentRevision.classloader.findResources0(name);
}
private void failed() {
System.out.println(FAILED_TEST);
try {
bc.getBundle(0).stop();
} catch (BundleException e) {
e.printStackTrace();
}
while (true) {}
}
public void cleanup() {
tracker1.close();
tracker2.close();
try {
b1.uninstall();
b2.uninstall();
tracker.getServiceReference().getBundle().uninstall();
} catch (BundleException e) {
e.printStackTrace();
}
tracker.close();
}
public void prepare() {
try {
bundle = Util.installBundle(bc, "bundleEnd5_test-1.0.0.jar");
bundle.start();
} catch (BundleException e) {
e.printStackTrace();
}
}
public void cleanup() {
try {
bundle.uninstall();
} catch (BundleException e) {
e.printStackTrace();
}
}
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;
}
@Override
public void run() {
// Create a display
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Grid Editor");
// The lone ReactorEditor needs to fill this window.
shell.setLayout(new FillLayout());
// Create Viewers for three different views of LW/SFRs.
createLWRCore(shell);
createSFRCore(shell);
createSFRAssembly(shell);
// Pack and open everything
shell.setSize(1200, 900);
shell.open();
// Wait until the shell is closed
while (!shell.isDisposed() && active) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
// Dispose the display
display.dispose();
final BundleContext bundleContext = FrameworkUtil.getBundle(
this.getClass()).getBundleContext();
if (bundleContext != null) {
try {
bundleContext.getBundle(0).stop();
} catch (BundleException e) {
e.printStackTrace();
}
}
return;
}
/**
* set the current startlevel but does not update the metadata.
*
* @param targetLevel
* the startlevel.
*
*/
protected void setLevel(final Bundle[] bundleArray, final int targetLevel,
final boolean all) {
if (startlevel == targetLevel) {
return;
}
final boolean up = targetLevel > startlevel;
final int levels = up ? targetLevel - startlevel
: startlevel - targetLevel;
final MultiMap<Integer, AbstractBundle> startLevels = new MultiMap<Integer, AbstractBundle>(
0);
// prepare startlevels
for (int i = 0; i < bundleArray.length; i++) {
final AbstractBundle bundle = (AbstractBundle) bundleArray[i];
if (bundle == Concierge.this || bundle.state == Bundle.UNINSTALLED
|| up && bundle.autostart == AUTOSTART_STOPPED
|| !up && bundle.state == Bundle.RESOLVED) {
continue;
}
final int offset;
if (up) {
offset = bundle.startlevel - startlevel - 1;
} else {
offset = startlevel - bundle.startlevel;
}
if (offset >= 0 && offset < levels) {
startLevels.insert(new Integer(offset), bundle);
}
}
for (int i = 0; i < levels; i++) {
if (up) {
startlevel++;
} else {
startlevel--;
}
final List<AbstractBundle> list = startLevels.get(new Integer(i));
if (list == null) {
continue;
}
final BundleImpl[] toProcess = list
.toArray(new BundleImpl[list.size()]);
for (int j = 0; j < toProcess.length; j++) {
try {
if (up) {
// transient is implicit
toProcess[j]
.activate(toProcess[j].isActivationPolicyUsed()
? Bundle.START_ACTIVATION_POLICY : 0);
} else {
if (toProcess[toProcess.length - j - 1]
.getState() == Bundle.UNINSTALLED) {
continue;
}
// transient is implicit
toProcess[toProcess.length - j - 1].stopBundle();
}
} catch (final BundleException be) {
if (be.getNestedException() != null) {
be.getNestedException().printStackTrace();
}
be.printStackTrace();
notifyFrameworkListeners(FrameworkEvent.ERROR,
up ? toProcess[j]
: toProcess[toProcess.length - j - 1],
be);
} catch (final Throwable t) {
t.printStackTrace();
notifyFrameworkListeners(FrameworkEvent.ERROR,
up ? toProcess[j]
: toProcess[toProcess.length - j - 1],
t);
}
}
}
startlevel = targetLevel;
}
public void run() {
EnduranceTest[] tests = new EnduranceTest[] {
new Test1(bc),
new Test2(bc),
new Test3(bc),
new Test4(bc),
new Test5(bc),
new Test6(bc),
new Test7(bc),
new Test8(bc),
new Test9(bc, 500, 1, new String[] { "sv" }, "Localization test many getHeaders/install"),
new Test9(bc, 1, 500, new String[] { "sv" }, "Localization test few getHeaders/install"),
new Test9(bc, 500, 1, new String[] { "" }, "Raw localization test many getHeaders/install"),
new Test9(bc, 1, 500, new String[] { "" }, "Raw localization test few getHeaders/install"),
new Test10(bc, 500, 1, new String[] { "sv" }, "Localization test with fragments many getHeaders/install"),
new Test10(bc, 1, 500, new String[] { "sv" }, "Localization test with fragments few getHeaders/install"),
new Test10(bc, 500, 1, new String[] { "" }, "Raw localization test with fragments many getHeaders/install"),
new Test10(bc, 1, 500, new String[] { "" }, "Raw localization test with fragments few getHeaders/install"),
// add new tests here.
};
for (int i = 0; i < tests.length; i++) {
int n = tests[i].getNoRuns();
int bestRun = -1;
long bestTime = Long.MAX_VALUE;
int worstRun = -1;
long worstTime = Long.MIN_VALUE;
System.out.println("Starting test \"" + tests[i].testName() + "\"");
tests[i].prepare();
long discUsage = discUsage(cacheDir);
System.gc();
long totalTime = System.currentTimeMillis();
long freeBefore = Runtime.getRuntime().freeMemory();
long totalBefore = Runtime.getRuntime().totalMemory();
for (int o = 0; o < n; o++) {
long tmp = System.currentTimeMillis();
if (!tests[i].runTest()) {
out.println("FAILED TO RUN TEST " + tests[i].getClass().getName());
break;
}
long tmp2 = System.currentTimeMillis();
if (bestTime > tmp2 - tmp) {
bestTime = tmp2 - tmp;
bestRun = o;
}
if (worstTime < tmp2 - tmp) {
worstTime = tmp2 - tmp;
worstRun = o;
}
}
totalTime = System.currentTimeMillis() - totalTime;
System.gc();
long freeAfter = Runtime.getRuntime().freeMemory();
long totalAfter = Runtime.getRuntime().totalMemory();
out.println("Results from test \"" + tests[i].testName() + "\" (executed " + tests[i].getNoRuns() + " times)");
out.println("Memory\t\tfree\t\ttotal\t\tused");
out.println(" before:\t" + freeBefore / 1000 + "kB\t\t" + totalBefore / 1000 + "kB\t\t" + (totalBefore - freeBefore) / 1000 + "kB");
out.println(" after:\t" + freeAfter / 1000 + "kB\t\t" + totalAfter / 1000 + "kB\t\t" + (totalAfter - freeAfter) / 1000 + "kB");
out.println("Disc usage\tused");
out.println(" before:\t" + discUsage/1000 + "kB\t");
out.println(" after:\t" + discUsage(cacheDir)/1000 + "kB");
out.println("Time ");
out.println(" best run:\t " + bestTime + "ms\trun:" + bestRun);
out.println(" worst run:\t " + worstTime + "ms\trun:" + worstRun);
out.println("Total time:\t " + totalTime + "ms");
out.println();
tests[i].cleanup();
}
if (System.getProperty("org.knopflerfish.bundle.endurance_test.halt_after_test",
"false").equals("true")) {
try {
System.out.println("Shutting down framework.");
bc.getBundle(0).stop();
} catch (BundleException e) {
e.printStackTrace();
System.exit(0);
}
}
}
public void runTest() {
Bundle c1 = null;
ServiceReference<?> sr = null;
ServiceRegistration<?> reg = null;
ServiceRegistration<?> reg2 = null;
try {
counter = 0;
gotCircularError = false;
sr = bc.getServiceReference(LogReaderService.class.getName());
LogReaderService lrs = (LogReaderService)bc.getService(sr);
lrs.addLogListener(this);
c1 = Util.installBundle(bc, "componentA_test-1.0.1.jar");
c1.start();
Thread.sleep(SLEEP_TIME);
assertNull("Should be null (1)", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentA"));
assertNull("Should be null (2)", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentB"));
assertNull("Should be null (3)", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentC"));
assertEquals("Should not have been bumped", 0, counter);
assertTrue("Should have got circular error message", gotCircularError);
lrs.removeLogListener(this);
reg2 = bc.registerService(TestService2.class.getName(), new TestService2(), new Hashtable<String, Object>());
reg = bc.registerService(TestService.class.getName(), new TestService(), new Hashtable<String, Object>());
Thread.sleep(SLEEP_TIME);
ServiceReference<?> ref = bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentA");
assertNotNull("Should get service A", bc.getService(ref));
ref = bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentB");
assertNotNull("Should get service B", bc.getService(ref));
ref = bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentC");
assertNotNull("Should get service C", bc.getService(ref));
assertEquals("Should have been activate/bind bumped", 103, counter);
reg.unregister();
reg = null;
Thread.sleep(SLEEP_TIME);
assertNull("Should be null (1(2))", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentA"));
assertNull("Should be null (2(2))", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentB"));
assertNull("Should be null (3(2))", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentC"));
assertEquals("Should have been bind/2*unbind and deactive bumped", 2233, counter);
counter = 0;
} catch (Exception e) {
e.printStackTrace();
fail("Test2b: got unexpected exception " + e);
} finally {
if (c1 != null) {
try {
c1.uninstall();
} catch (BundleException be) {
be.printStackTrace();
fail("Test2b: got uninstall exception " + be);
}
}
if (sr != null) {
bc.ungetService(sr);
}
if (reg != null) {
reg.unregister();
}
if (reg2 != null) {
reg2.unregister();
}
}
}
/**
* Test setup: ComponentA references ComponentB,
* ComponentB references ComponentC,TestService2
* ComponentC references TestService
* ComponentD provides TestService and reference ComponentA
* before: no components are started.
* action: TestService and TestService2 is registered
* after: all components are activated
*
* then:
*
* before: all components are activated
* action: modify TestService2 to block ComponentB
* after: only ComponentC is active
*
* then:
*
* before: all components are activated
* action: unregister TestService and TestService2
* after: all components are deactivated
*
* (the components call bump when they are (de-)actived)
*/
public void runTest() {
Bundle c1 = null;
ServiceRegistration<?> reg = null;
ServiceRegistration<?> reg2 = null;
try {
reg = bc.registerService(TestService.class.getName(), new TestService(), new Hashtable<String,Object>());
reg2 = bc.registerService(TestService2.class.getName(), new TestService2(), new Hashtable<String,Object>());
counter = 0;
c1 = Util.installBundle(bc, "componentA_test-1.0.1.jar");
c1.start();
Thread.sleep(SLEEP_TIME);
ServiceReference<?> ref = bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentB");
assertNotNull("Should get serviceRef B", ref);
assertNotNull("Should get service B", bc.getService(ref));
ref = bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentC");
assertNotNull("Should get serviceRef C", ref);
assertNotNull("Should get service C", bc.getService(ref));
assertEquals("Should have been activate(B&C)/bind(C) bumped", 102, counter);
Hashtable<String, Object> p = new Hashtable<String,Object>();
p.put("block","yes");
reg2.setProperties(p);
Thread.sleep(SLEEP_TIME);
assertNull("Should not get B", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentB"));
assertNotNull("Should still get C", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentC"));
assertEquals("Should have been deactivate B", 112, counter);
reg.unregister();
reg = null;
Thread.sleep(SLEEP_TIME);
assertNull("Should not get C", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentC"));
assertEquals("Should have been deactivate/unbind C bumped", 1122, counter);
counter = 0;
} catch (Exception e) {
e.printStackTrace();
fail("Test4: got unexpected exception " + e);
} finally {
if (c1 != null) {
try {
c1.uninstall();
} catch (BundleException be) {
be.printStackTrace();
fail("Test4: got uninstall exception " + be);
}
}
if (reg != null) {
reg.unregister();
}
if (reg2 != null) {
reg2.unregister();
}
}
}
/**
* Test setup: ComponentA references ComponentB,
* ComponentB references ComponentC,TestService2
* ComponentC references TestService
* ComponentD provides TestService and reference ComponentA
* before: no components are started.
* action: TestService and TestService2 is registered
* after: all components are activated
*
* then:
*
* before: all components are activated
* action: register second TestService, then set high service ranking and
* then unregister first TestService
* after: all components are still activated
*
* then:
*
* before: all components are activated
* action: unregister second TestService
* after: all components are deactivated
*
* (the components call bump when they are (de-)actived)
*/
public void runTest() {
Bundle c1 = null;
ServiceRegistration<?> reg = null;
ServiceRegistration<?> regSecond = null;
ServiceRegistration<?> reg2 = null;
try {
reg = bc.registerService(TestService.class.getName(), new TestService(), new Hashtable<String,Object>());
reg2 = bc.registerService(TestService2.class.getName(), new TestService2(), new Hashtable<String,Object>());
counter = 0;
c1 = Util.installBundle(bc, "componentA_test-1.0.1.jar");
c1.start();
Thread.sleep(SLEEP_TIME);
ServiceReference<?> ref = bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentB");
assertNotNull("Should get serviceRef B", ref);
assertNotNull("Should get service B", bc.getService(ref));
ref = bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentC");
assertNotNull("Should get serviceRef C", ref);
assertNotNull("Should get service C", bc.getService(ref));
assertEquals("Should have been activate(B&C)/bind(C) bumped", 102, counter);
regSecond = bc.registerService(TestService.class.getName(), new TestService(), new Hashtable<String,Object>());
Hashtable<String,Object> p = new Hashtable<String,Object>();
p.put(Constants.SERVICE_RANKING, new Integer(7));
regSecond.setProperties(p);
reg.unregister();
reg = null;
Thread.sleep(SLEEP_TIME);
assertNotNull("Should still get B", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentB"));
assertNotNull("Should still get C", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentC"));
assertEquals("Should have been deactivate B", 1202, counter);
regSecond.unregister();
regSecond = null;
Thread.sleep(SLEEP_TIME);
assertNull("Should not get B", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentB"));
assertNull("Should not get C", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentC"));
assertEquals("Should have been deactivate/unbind C bumped", 3322, counter);
counter = 0;
} catch (Exception e) {
e.printStackTrace();
fail("Test5: got unexpected exception " + e);
} finally {
if (c1 != null) {
try {
c1.uninstall();
} catch (BundleException be) {
be.printStackTrace();
fail("Test5: got uninstall exception " + be);
}
}
if (reg != null) {
reg.unregister();
}
if (regSecond != null) {
regSecond.unregister();
}
if (reg2 != null) {
reg2.unregister();
}
}
}