View Javadoc
1   package net.logAnalyzer;
2   
3   import java.net.URL;
4   import java.net.URLClassLoader;
5   import java.net.MalformedURLException;
6   import java.io.File;
7   import java.util.StringTokenizer;
8   import java.util.List;
9   import java.util.ArrayList;
10  import java.util.Iterator;
11  
12  
13  /***
14   *  This is a launcher for LogAnalyzer.
15   *  
16   *  Based on Ant Launcher 
17   *
18   * @author David Vicente
19   * @version 0.1
20   */
21  public class LogAnalyzerLauncher {
22      /*** The LogAnalyzer Home property */
23      public static final String LOGANALYZERHOME_PROPERTY = "loganalyzer.home";
24  
25      /*** The LogAnalyzer Library Directory property */
26      public static final String LOGANALYZERLIBDIR_PROPERTY = "loganalyzer.library.dir";
27  
28      /*** The directory name of the per-user ant directory */
29      public static final String LOGANALYZER_PRIVATEDIR = ".ant";
30  
31      /***
32       * The location of a per-user library directory
33       */
34      public static final String LOGANALYZER_PRIVATELIB = "lib";
35  
36      /*** The location of a per-user library directory */
37      public static final String USER_LIBDIR = LOGANALYZER_PRIVATEDIR + "/" + LOGANALYZER_PRIVATELIB;
38  
39      /*** The startup class that is to be run */
40      public static final String MAIN_CLASS = "net.logAnalyzer.LogAnalyzerBatch";
41      /***
42       * system property with user home directory
43       */
44      public static final String USER_HOMEDIR = "user.home";
45  
46      /***
47       *  Entry point for starting command line Ant
48       *
49       * @param  args commandline arguments
50       */
51      public static void main(String[] args) {
52          try {
53              LogAnalyzerLauncher launcher = new LogAnalyzerLauncher();
54              launcher.run(args);
55          } catch (Exception e) {
56              System.err.println(e.getMessage());
57          } catch (Throwable t) {
58              t.printStackTrace();
59          }
60      }
61  
62      /***
63        * Add a CLASSPATH or -lib to lib path urls.
64        * @param path        the classpath or lib path to add to the libPathULRLs
65        * @param getJars     if true and a path is a directory, add the jars in
66        *                    the directory to the path urls
67        * @param libPathURLs the list of paths to add to
68        */
69      private void addPath(String path, boolean getJars, List libPathURLs)
70          throws MalformedURLException {
71          StringTokenizer myTokenizer
72              = new StringTokenizer(path, System.getProperty("path.separator"));
73          while (myTokenizer.hasMoreElements()) {
74              String elementName = myTokenizer.nextToken();
75              File element = new File(elementName);
76              if (elementName.indexOf("%") != -1 && !element.exists()) {
77                  continue;
78              }
79              if (getJars && element.isDirectory()) {
80                  // add any jars in the directory
81                  URL[] dirURLs = Locator.getLocationURLs(element);
82                  for (int j = 0; j < dirURLs.length; ++j) {
83                      libPathURLs.add(dirURLs[j]);
84                  }
85              }
86  
87              libPathURLs.add(element.toURL());
88          }
89      }
90  
91      /***
92       * Run the launcher to launch Ant
93       *
94       * @param args the command line arguments
95       *
96       * @exception MalformedURLException if the URLs required for the classloader
97       *            cannot be created.
98       */
99      private void run(String[] args) throws Exception, MalformedURLException {
100         String antHomeProperty = System.getProperty(LOGANALYZERHOME_PROPERTY);
101         File antHome = null;
102 
103         File sourceJar = Locator.getClassSource(getClass());
104         File jarDir = sourceJar.getParentFile();
105 
106         if (antHomeProperty != null) {
107             antHome = new File(antHomeProperty);
108         }
109 
110         if (antHome == null || !antHome.exists()) {
111             antHome = jarDir.getParentFile();
112             System.setProperty(LOGANALYZERHOME_PROPERTY, antHome.getAbsolutePath());
113         }
114 
115         if (!antHome.exists()) {
116             throw new Exception("Ant home is set incorrectly or "
117                 + "ant could not be located");
118         }
119 
120         List libPaths = new ArrayList();
121         String cpString = null;
122         List argList = new ArrayList();
123         String[] newArgs;
124         boolean  noUserLib = false;
125         boolean  noClassPath = false;
126 
127         for (int i = 0; i < args.length; ++i) {
128             if (args[i].equals("-lib")) {
129                 if (i == args.length - 1) {
130                     throw new Exception("The -lib argument must "
131                         + "be followed by a library location");
132                 }
133                 libPaths.add(args[++i]);
134             } else if (args[i].equals("-cp")) {
135                 if (i == args.length - 1) {
136                     throw new Exception("The -cp argument must "
137                         + "be followed by a classpath expression");
138                 }
139                 if (cpString != null) {
140                     throw new Exception("The -cp argument must "
141                         + "not be repeated");
142                 }
143                 cpString = args[++i];
144             } else if (args[i].equals("--nouserlib") || args[i].equals("-nouserlib")) {
145                 noUserLib = true;
146             } else if (args[i].equals("--noclasspath") || args[i].equals("-noclasspath")) {
147                 noClassPath = true;
148             } else {
149                 argList.add(args[i]);
150             }
151         }
152 
153         //decide whether to copy the existing arg set, or
154         //build a new one from the list of all args excluding the special
155         //operations that only we handle
156 
157         if (libPaths.size() == 0 && cpString == null) {
158             newArgs = args;
159         } else {
160             newArgs = (String[]) argList.toArray(new String[0]);
161         }
162 
163         List libPathURLs = new ArrayList();
164 
165         if (cpString != null && !noClassPath) {
166             addPath(cpString, false, libPathURLs);
167         }
168 
169         for (Iterator i = libPaths.iterator(); i.hasNext();) {
170             String libPath = (String) i.next();
171             addPath(libPath, true, libPathURLs);
172         }
173 
174         URL[] libJars = (URL[]) libPathURLs.toArray(new URL[0]);
175 
176         // Now try and find JAVA_HOME
177         File toolsJar = Locator.getToolsJar();
178 
179         // determine ant library directory for system jars: use property
180         // or default using location of ant-launcher.jar
181         File antLibDir = null;
182         String antLibDirProperty = System.getProperty(LOGANALYZERLIBDIR_PROPERTY);
183         if (antLibDirProperty != null) {
184             antLibDir = new File(antLibDirProperty);
185         }
186         if ((antLibDir == null) || !antLibDir.exists()) {
187             antLibDir = jarDir;
188             System.setProperty(LOGANALYZERLIBDIR_PROPERTY, antLibDir.getAbsolutePath());
189         }
190         URL[] systemJars = Locator.getLocationURLs(antLibDir);
191 
192         File userLibDir
193             = new File(System.getProperty(USER_HOMEDIR),
194                     LOGANALYZER_PRIVATEDIR + File.separatorChar + LOGANALYZER_PRIVATELIB);
195 
196         URL[] userJars = noUserLib ? new URL[0] : Locator.getLocationURLs(userLibDir);
197 
198         int numJars = libJars.length + userJars.length + systemJars.length;
199         if (toolsJar != null) {
200             numJars++;
201         }
202         URL[] jars = new URL[numJars];
203         System.arraycopy(libJars, 0, jars, 0, libJars.length);
204         System.arraycopy(userJars, 0, jars, libJars.length, userJars.length);
205         System.arraycopy(systemJars, 0, jars, userJars.length + libJars.length,
206             systemJars.length);
207 
208         if (toolsJar != null) {
209             jars[jars.length - 1] = toolsJar.toURL();
210         }
211 
212 
213         // now update the class.path property
214         StringBuffer baseClassPath
215             = new StringBuffer(System.getProperty("java.class.path"));
216         if (baseClassPath.charAt(baseClassPath.length() - 1)
217                 == File.pathSeparatorChar) {
218             baseClassPath.setLength(baseClassPath.length() - 1);
219         }
220 
221         for (int i = 0; i < jars.length; ++i) {
222             baseClassPath.append(File.pathSeparatorChar);
223             baseClassPath.append(Locator.fromURI(jars[i].toString()));
224         }
225 
226         System.setProperty("java.class.path", baseClassPath.toString());
227 
228         URLClassLoader loader = new URLClassLoader(jars);
229         Thread.currentThread().setContextClassLoader(loader);
230         try {
231             Class mainClass = loader.loadClass(MAIN_CLASS);
232             LogAnalyzerBatch logBatch = (LogAnalyzerBatch) mainClass.newInstance();
233             logBatch.main(newArgs);
234         } catch (Throwable t) {
235             t.printStackTrace();
236         }
237     }
238 }
239