From: Web H. <web...@us...> - 2004-08-10 10:49:29
|
Update of /cvsroot/babeldoc/babeldoc/modules/init/src/com/babeldoc/init In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14094/modules/init/src/com/babeldoc/init Modified Files: Main.java Log Message: Modified entry points and supporting files to allow programmatically starting and stopping Babeldoc from within the same JVM. Some problems still exist when restarting, the PipelineStage method getOptions(...) return null, under investigation. Regular method of invocation via babeldoc script files is unchanged. Added some optimisations to method of invocation of "main" methodsvia reflection, now istead of looping thru all available methods, directly retrieve the method of interest. Index: Main.java =================================================================== RCS file: /cvsroot/babeldoc/babeldoc/modules/init/src/com/babeldoc/init/Main.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** Main.java 23 Jul 2004 03:32:06 -0000 1.8 --- Main.java 10 Aug 2004 10:49:06 -0000 1.9 *************** *** 69,73 **** import java.io.FilenameFilter; ! import java.lang.reflect.Method; import java.util.Vector; --- 69,73 ---- import java.io.FilenameFilter; ! import java.lang.reflect.*; import java.util.Vector; *************** *** 87,287 **** public class Main { ! // private static ClassLoader loader; ! public static final String BABELDOC_HOME = "babeldoc.home"; ! public static final String BABELDOC_CP = "babeldoc.cp"; ! public static final String BABELDOC_SCANDIR = "babeldoc.scandir"; ! public static final String BABELDOC_MAIN = "com.babeldoc.core.Main"; ! public static final String BABELDOC_CLASSLOADER = "babeldoc.classloader"; ! public static final String ADAPTIVE = "adaptive"; ! public static final String LIB = "lib"; ! public static final String DOT_JAR = ".jar"; ! public static final String DOT_ZIP = ".zip"; ! public static final String MAIN = "main"; ! /** ! * The starting point for commandline Babeldoc. This then ! * checks the BABELDOC_HOME variable. Gets all the jar and ! * zip files. Proceed with this. ! * ! * @param args command line arguments. Set in script: babeldoc.sh/.bat ! */ ! public static void main(String[] args) { ! String home = System.getProperty(BABELDOC_HOME); ! if (home != null) { ! File fileHome = new File(home); ! if (fileHome.exists() && fileHome.isDirectory()) { ! runBabeldocMain(getLibraryFiles(home), args); ! return; ! } } ! System.err.println("Babeldoc home: " + home + " is invalid"); ! } ! /** ! * Get the urls for all the jars and zips in the library directory ! * ! * @param home path to the $BABELDOC_HOME directory ! * ! * @return list of zips and jars from 'lib' subdirectory ! */ ! private static File[] getLibraryFiles(String home) { ! File fileLib = new File(home, LIB); ! if (fileLib.exists() && fileLib.isDirectory()) { ! System.setProperty(BABELDOC_SCANDIR, fileLib.getAbsolutePath()); ! File[] libs = fileLib.listFiles(new FilenameFilter() { ! public boolean accept(File dir, String name) { ! if (name.endsWith(DOT_JAR) || name.endsWith(DOT_ZIP)) { ! return true; ! } else { ! return false; ! } ! } ! }); ! return libs; ! } else { ! System.err.println("The directory: " + fileLib + ! " does not exist or is not a directory. Babeldoc cannot find its library files and is quitting now."); } ! return null; ! } ! /** ! * Create a ClassLoader with the list of jars and zips and add them to the ! * classpath. Then call into the CORE Main method. ! * ! * @param files all the zip and the jar files ! * @param args command line arguments ! */ ! private static void runBabeldocMain(File[] files, String[] args) { ! if (files != null) { ! ClassLoader loader = setupClassLoader(files); ! try { ! Class main = loader.loadClass(BABELDOC_MAIN); ! Method[] methods = main.getDeclaredMethods(); ! for (int i = 0; i < methods.length; ++i) { ! if (MAIN.equals(methods[i].getName())) { ! Method method = methods[i]; ! Object[] methodArgs = new Object[] { args }; ! try { ! method.invoke(main, methodArgs); ! return; ! } catch (Exception e) { ! System.err.println(e); ! e.printStackTrace(); ! } ! break; ! } ! } ! System.err.println("Main does not have a main"); ! } catch (ClassNotFoundException e) { ! System.err.println(e); ! } } - } ! /** ! * Setup the particular classloader. There are two: Adaptive ! * and URLClassLoader. Configure either one ! * with the library files. ! * <br/> ! * <strong>NOTE</strong> At the end of the method, this new ! * classloader is applied to the current thread. ! * ! * @param inFiles files discovered in the lib directory ! * @return the classloader with all the library files added. ! */ ! private static ClassLoader setupClassLoader(File[] inFiles) { ! File [] files = incorporateClassPath(inFiles); ! ClassLoader loader = null; ! if(ADAPTIVE.equals(System.getProperty(BABELDOC_CLASSLOADER))) { ! Vector vec = new Vector(files.length); ! for (int i = 0; i < files.length; i++) { ! vec.add(files[i]); ! } ! loader = new AdaptiveClassLoader(vec, false); ! } else { ! URL [] urls = new URL[files.length]; ! for (int i = 0; i < files.length; i++) { ! try { ! urls[i] = files[i].toURL(); ! } catch (MalformedURLException e) { ! System.err.println(e); ! } ! } ! loader = new URLClassLoader(urls); } - Thread.currentThread().setContextClassLoader(loader); - return loader; - } ! /** ! * Load those classpath entries into the grand list of ! * files to place in the classpath ! * ! * @param files ! * @return ! */ ! private static File[] incorporateClassPath(File[] files) { ! File [] cpFiles = getClasspathFiles(); ! if(cpFiles!=null&&cpFiles.length>0) { ! File [] newFiles = new File[cpFiles.length+files.length]; ! int j = 0; ! for (int i = 0; i < files.length; i++) { ! newFiles[j++] = files[i]; ! } ! for(int i = 0; i < cpFiles.length; i++) { ! newFiles[j++] = cpFiles[i]; ! } ! files = newFiles; } - return files; - } ! /** ! * Get the files from the classpath - this is a hack to get around the ! * hidden classpath issue when running -jar (which is how babeldoc runs) ! * ! * @return array of files representing the classpath ! */ ! private static File[] getClasspathFiles() { ! String cp = System.getProperty(BABELDOC_CP); ! if(cp!=null) { ! StringTokenizer st = new StringTokenizer(cp, File.pathSeparator); ! Collection files = new ArrayList(); ! while(st.hasMoreTokens()) { ! String token = st.nextToken(); ! File file = new File(token); ! if (file.exists() && ( ! (token.endsWith(DOT_JAR)) || ! (token.endsWith(DOT_ZIP)) || ! (file.isDirectory()) ) ) { ! //System.out.println("Adding: "+token); ! files.add(file); ! } else { ! //System.out.println("Ignoring : "+token); ! } ! } ! return (File[])files.toArray(new File[0]); ! } else { ! return null; } - } } --- 87,333 ---- public class Main { ! // private static ClassLoader loader; ! public static final String BABELDOC_HOME = "babeldoc.home"; ! public static final String BABELDOC_CP = "babeldoc.cp"; ! public static final String BABELDOC_SCANDIR = "babeldoc.scandir"; ! public static final String BABELDOC_MAIN = "com.babeldoc.core.Main"; ! public static final String BABELDOC_CLASSLOADER = "babeldoc.classloader"; ! public static final String ADAPTIVE = "adaptive"; ! public static final String LIB = "lib"; ! public static final String DOT_JAR = ".jar"; ! public static final String DOT_ZIP = ".zip"; ! public static final String MAIN = "main"; ! public static final String INTERRUPT = "interrupt"; ! private Object babelDocThread; ! /** ! * Creates a new <code>Main</code> instance. ! * This is usefull for classes which need to programatically launch and stop ! * Babeldoc. ! * ! */ ! public Main(String[] args) { ! String home = System.getProperty(BABELDOC_HOME); ! if (home != null) { ! File fileHome = new File(home); ! ! if (fileHome.exists() && fileHome.isDirectory()) { ! File[] files = getLibraryFiles(home); ! if (files != null) { ! try { ! ClassLoader loader = setupClassLoader(files); ! Class main = loader.loadClass(BABELDOC_MAIN); ! // Constructor[] constructors = main.getConstructors(); ! Constructor constructor = main.getConstructor(new Class[]{String[].class}); ! babelDocThread = constructor.newInstance(new Object[]{args}); ! } ! catch (Throwable e) { ! System.err.println(e); ! e.printStackTrace(); ! } ! return; ! } ! } ! } ! System.err.println("Babeldoc home: " + home + " is invalid"); } ! public void interrupt() { ! if (babelDocThread != null) { ! try { ! // invoke the interrupt() method ! Method method = babelDocThread.getClass().getDeclaredMethod(INTERRUPT, null); ! method.invoke(babelDocThread,null); ! } ! catch (Throwable e) { ! System.err.println(e); ! } ! } ! } ! /** ! * The starting point for commandline Babeldoc. This then ! * checks the BABELDOC_HOME variable. Gets all the jar and ! * zip files. Proceed with this. ! * ! * @param args command line arguments. Set in script: babeldoc.sh/.bat ! */ ! public static void main(String[] args) { ! String home = System.getProperty(BABELDOC_HOME); ! if (home != null) { ! File fileHome = new File(home); ! if (fileHome.exists() && fileHome.isDirectory()) { ! runBabeldocMain(getLibraryFiles(home), args); ! return; ! } ! } ! ! System.err.println("Babeldoc home: " + home + " is invalid"); } ! /** ! * Get the urls for all the jars and zips in the library directory ! * ! * @param home path to the $BABELDOC_HOME directory ! * ! * @return list of zips and jars from 'lib' subdirectory ! */ ! private static File[] getLibraryFiles(String home) { ! File fileLib = new File(home, LIB); ! if (fileLib.exists() && fileLib.isDirectory()) { ! System.setProperty(BABELDOC_SCANDIR, fileLib.getAbsolutePath()); ! File[] libs = fileLib.listFiles(new FilenameFilter() { ! public boolean accept(File dir, String name) { ! if (name.endsWith(DOT_JAR) || name.endsWith(DOT_ZIP)) { ! return true; ! } else { ! return false; ! } ! } ! }); ! return libs; ! } else { ! System.err.println("The directory: " + fileLib + ! " does not exist or is not a directory. Babeldoc cannot find its library files and is quitting now."); ! } ! return null; ! } ! /** ! * Create a ClassLoader with the list of jars and zips and add them to the ! * classpath. Then call into the CORE Main method. ! * ! * @param files all the zip and the jar files ! * @param args command line arguments ! */ ! private static void runBabeldocMain(File[] files, String[] args) { ! if (files != null) { ! ClassLoader loader = setupClassLoader(files); ! try { ! Class main = loader.loadClass(BABELDOC_MAIN); ! Method method = main.getDeclaredMethod(MAIN, new Class[]{String[].class}); ! Object[] methodArgs = new Object[] { args }; ! ! method.invoke(main, methodArgs); ! return; ! ! } ! catch (java.lang.reflect.InvocationTargetException e) { ! System.err.println(e); ! } ! catch (java.lang.IllegalAccessException e) { ! System.err.println(e); ! } ! catch (java.lang.NoSuchMethodException e) { ! System.err.println(e); ! } ! catch (ClassNotFoundException e) { ! System.err.println(e); ! } ! } } ! /** ! * Setup the particular classloader. There are two: Adaptive ! * and URLClassLoader. Configure either one ! * with the library files. ! * <br/> ! * <strong>NOTE</strong> At the end of the method, this new ! * classloader is applied to the current thread. ! * ! * @param inFiles files discovered in the lib directory ! * @return the classloader with all the library files added. ! */ ! private static ClassLoader setupClassLoader(File[] inFiles) { ! File [] files = incorporateClassPath(inFiles); ! ClassLoader loader = null; ! if(ADAPTIVE.equals(System.getProperty(BABELDOC_CLASSLOADER))) { ! Vector vec = new Vector(files.length); ! for (int i = 0; i < files.length; i++) { ! vec.add(files[i]); ! } ! loader = new AdaptiveClassLoader(vec, false); ! } else { ! URL [] urls = new URL[files.length]; ! for (int i = 0; i < files.length; i++) { ! try { ! urls[i] = files[i].toURL(); ! } catch (MalformedURLException e) { ! System.err.println(e); ! } ! } ! loader = new URLClassLoader(urls); ! } ! Thread.currentThread().setContextClassLoader(loader); ! return loader; } ! /** ! * Load those classpath entries into the grand list of ! * files to place in the classpath ! * ! * @param files ! * @return ! */ ! private static File[] incorporateClassPath(File[] files) { ! File [] cpFiles = getClasspathFiles(); ! if(cpFiles!=null&&cpFiles.length>0) { ! File [] newFiles = new File[cpFiles.length+files.length]; ! int j = 0; ! for (int i = 0; i < files.length; i++) { ! newFiles[j++] = files[i]; ! } ! for(int i = 0; i < cpFiles.length; i++) { ! newFiles[j++] = cpFiles[i]; ! } ! files = newFiles; ! } ! return files; } ! /** ! * Get the files from the classpath - this is a hack to get around the ! * hidden classpath issue when running -jar (which is how babeldoc runs) ! * ! * @return array of files representing the classpath ! */ ! private static File[] getClasspathFiles() { ! String cp = System.getProperty(BABELDOC_CP); ! if(cp!=null) { ! StringTokenizer st = new StringTokenizer(cp, File.pathSeparator); ! Collection files = new ArrayList(); ! while(st.hasMoreTokens()) { ! String token = st.nextToken(); ! File file = new File(token); ! if (file.exists() && ( ! (token.endsWith(DOT_JAR)) || ! (token.endsWith(DOT_ZIP)) || ! (file.isDirectory()) ) ) { ! //System.out.println("Adding: "+token); ! files.add(file); ! } else { ! //System.out.println("Ignoring : "+token); ! } ! } ! return (File[])files.toArray(new File[0]); ! } else { ! return null; ! } } } |