00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 package org.knopflerfish.framework;
00036
00037 import java.lang.reflect.*;
00038
00039 import org.osgi.framework.*;
00040
00041
00057 public class MainClassBundleActivator implements BundleActivator, Runnable {
00058
00059 Class clazz;
00060 Method startMethod = null;
00061 Method stopMethod = null;
00062
00063 Thread runner = null;
00064
00065 String[] argv = new String[] { };
00066
00067 public MainClassBundleActivator(Class clazz) throws Exception {
00068 this.clazz = clazz;
00069
00070 startMethod = clazz.getMethod("start", new Class[] { argv.getClass() });
00071
00072
00073 try {
00074 stopMethod = clazz.getMethod("stop", new Class[] { });
00075 } catch (Exception ignored) {
00076 }
00077 }
00078
00079 public static MainClassBundleActivator create(ClassLoader cl, Class mainClazz) throws BundleException {
00080
00081 try {
00082 Class baClass = cl.loadClass(MainClassBundleActivator.class.getName());
00083 Constructor cons = baClass.getConstructor(new Class[]
00084 { Class.class });
00085
00086 MainClassBundleActivator ba = (MainClassBundleActivator)cons.newInstance(new Object[]
00087 { mainClazz });
00088
00089
00090 return ba;
00091 } catch (Exception e) {
00092 throw new BundleException("Failed to create main class activator", e);
00093 }
00094 }
00095
00096
00097
00098 public void start(BundleContext bc) throws BundleException {
00099
00100 try {
00101 runner = new Thread(this, "start thread for executable jar file, bundle id=" + bc.getBundle().getBundleId());
00102 runner.start();
00103 } catch (Exception e) {
00104 throw new BundleException("Failed to start main class", e);
00105 }
00106 }
00107
00108 public void stop(BundleContext bc) throws BundleException {
00109 if(stopMethod != null) {
00110 try {
00111 stopMethod.invoke(null, new Object[] { } );
00112 } catch (Exception e) {
00113 throw new BundleException("Failed to stop main class", e);
00114 }
00115 }
00116 }
00117
00118 public void run() {
00119 try {
00120 startMethod.invoke(null, new Object[] { argv } );
00121 } catch (Exception e) {
00122 System.err.println("Failed to start executable jar file: " + e);
00123 }
00124 }
00125 }