MainClassBundleActivator.java

00001 /*
00002  * Copyright (c) 2003, KNOPFLERFISH project
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following
00007  * conditions are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  *
00012  * - Redistributions in binary form must reproduce the above
00013  *   copyright notice, this list of conditions and the following
00014  *   disclaimer in the documentation and/or other materials
00015  *   provided with the distribution.
00016  *
00017  * - Neither the name of the KNOPFLERFISH project nor the names of its
00018  *   contributors may be used to endorse or promote products derived
00019  *   from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00027  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00028  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00030  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
00032  * OF THE POSSIBILITY OF SUCH DAMAGE.
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     // Check for optional stop method
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 }

Generated on Mon Jan 11 21:19:15 2010 for OpenMobileIS by  doxygen 1.5.4