From: paspes <pa...@us...> - 2008-01-04 13:47:05
|
Update of /cvsroot/babeldoc/babeldoc/modules/init/src/com/babeldoc/init In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv16065/modules/init/src/com/babeldoc/init Modified Files: Main.java Log Message: NO VA. intentant carregar babeldoc.user al inici = que el babeldoc.home Index: Main.java =================================================================== RCS file: /cvsroot/babeldoc/babeldoc/modules/init/src/com/babeldoc/init/Main.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** Main.java 10 Aug 2004 10:49:06 -0000 1.9 --- Main.java 4 Jan 2008 13:47:05 -0000 1.10 *************** *** 80,333 **** /** ! * Initial entry point for Babeldoc using a custom class loader to avoid the very ! * large classpaths that need to set for reasonably sized Java applications. This ! * class loads all the library files in the %BABELDOC_HOME/lib directory into the ! * classpath. */ 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; } - } } --- 80,379 ---- /** ! * Initial entry point for Babeldoc using a custom class loader to avoid the ! * very large classpaths that need to set for reasonably sized Java ! * applications. This class loads all the library files in the ! * %BABELDOC_HOME/lib directory into the classpath. */ public class Main { ! // private static ClassLoader loader; ! public static final String BABELDOC_HOME = "babeldoc.home"; ! public static final String BABELDOC_USER = "babeldoc.user"; ! 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); ! String user = System.getProperty(BABELDOC_USER); ! ! File[] filesUser = null; ! File[] filesHome = null; ! File[] files = null; ! ! if (user != null) { ! File fileUser = new File(user); ! if (fileUser.exists() && fileUser.isDirectory()) { ! filesUser = getLibraryFiles(user); ! } } ! if (home != null) { ! File fileHome = new File(home); ! if (fileHome.exists() && fileHome.isDirectory()) { ! filesHome = getLibraryFiles(home); ! } ! } ! files = copyTo(filesHome, filesUser); ! 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; ! } ! ! private static File[] copyTo(File[] files1, File[] files2) { ! ! if (files1 != null) { ! return files2; ! } ! if (files2 != null && files2.length > 0) { ! File[] newFiles = new File[files1.length + files2.length]; ! int j = 0; ! for (int i = 0; i < files1.length; i++) { ! newFiles[j++] = files1[i]; ! } ! ! for (int i = 0; i < files2.length; i++) { ! newFiles[j++] = files2[i]; ! } ! return newFiles; ! } ! return files1; ! } ! ! /** ! * 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; } } } |