1 18 package org.apache.activemq.console; 19 20 import java.io.File ; 21 import java.io.InputStream ; 22 import java.io.PrintStream ; 23 import java.lang.reflect.InvocationTargetException ; 24 import java.lang.reflect.Method ; 25 import java.net.JarURLConnection ; 26 import java.net.MalformedURLException ; 27 import java.net.URI ; 28 import java.net.URL ; 29 import java.net.URLClassLoader ; 30 import java.util.ArrayList ; 31 import java.util.Arrays ; 32 import java.util.Comparator ; 33 import java.util.Iterator ; 34 import java.util.LinkedList ; 35 import java.util.List ; 36 import java.util.Set ; 37 import java.util.HashSet ; 38 import java.util.StringTokenizer ; 39 40 46 public class Main { 47 48 public static final String TASK_DEFAULT_CLASS = "org.apache.activemq.console.command.ShellCommand"; 49 50 private File activeMQHome; 51 private File activeMQBase; 52 private ClassLoader classLoader; 53 private Set extensions = new HashSet (5); 54 private Set activeMQClassPath = new HashSet (5); 55 56 57 private static boolean useDefExt = true; 58 59 public static void main(String [] args) { 60 Main app = new Main(); 61 62 List tokens = new LinkedList (Arrays.asList(args)); 64 app.parseExtensions(tokens); 66 67 if(useDefExt && app.canUseExtdir()) { 78 79 boolean baseIsHome = app.getActiveMQBase().equals(app.getActiveMQHome()); 80 81 File baseLibDir = new File (app.getActiveMQBase(), "lib"); 82 File homeLibDir = new File (app.getActiveMQHome(), "lib"); 83 84 if(!baseIsHome) { 85 app.addExtensionDirectory(baseLibDir); 86 } 87 app.addExtensionDirectory(homeLibDir); 88 89 if(!baseIsHome) { 90 app.addExtensionDirectory(new File (baseLibDir, "optional")); 91 app.addExtensionDirectory(new File (baseLibDir, "web")); 92 } 93 app.addExtensionDirectory(new File (homeLibDir, "optional")); 94 app.addExtensionDirectory(new File (homeLibDir, "web")); 95 96 } 97 98 app.addClassPathList(System.getProperty("activemq.classpath")); 100 101 try { 102 app.runTaskClass(tokens); 103 } catch (ClassNotFoundException e) { 104 System.out.println("Could not load class: " + e.getMessage()); 105 try { 106 ClassLoader cl = app.getClassLoader(); 107 if( cl!=null ) { 108 System.out.println("Class loader setup: "); 109 printClassLoaderTree(cl); 110 } 111 } catch (MalformedURLException e1) { 112 } 113 } catch (Throwable e) { 114 System.out.println("Failed to execute main task. Reason: " + e); 115 } 116 } 117 118 124 private static int printClassLoaderTree(ClassLoader cl) { 125 int depth = 0; 126 if( cl.getParent()!=null ) { 127 depth = printClassLoaderTree(cl.getParent())+1; 128 } 129 130 StringBuffer indent = new StringBuffer (); 131 for (int i = 0; i < depth; i++) { 132 indent.append(" "); 133 } 134 135 if( cl instanceof URLClassLoader ) { 136 URLClassLoader ucl = (URLClassLoader ) cl; 137 System.out.println(indent+cl.getClass().getName()+" {"); 138 URL [] urls = ucl.getURLs(); 139 for (int i = 0; i < urls.length; i++) { 140 System.out.println(indent+" "+urls[i]); 141 } 142 System.out.println(indent+"}"); 143 } else { 144 System.out.println(indent+cl.getClass().getName()); 145 } 146 return depth; 147 } 148 149 public void parseExtensions(List tokens) { 150 if (tokens.isEmpty()) { 151 return; 152 } 153 154 int count = tokens.size(); 155 int i = 0; 156 157 while (i < count) { 159 String token = (String )tokens.get(i); 160 if (token.equals("--extdir")) { 162 count--; 164 tokens.remove(i); 165 166 if (i >= count || ((String )tokens.get(i)).startsWith("-")) { 168 System.out.println("Extension directory not specified."); 169 System.out.println("Ignoring extension directory option."); 170 continue; 171 } 172 173 count--; 175 File extDir = new File ((String )tokens.remove(i)); 176 177 if(!canUseExtdir()) { 178 System.out.println("Extension directory feature not available due to the system classpath being able to load: " + TASK_DEFAULT_CLASS); 179 System.out.println("Ignoring extension directory option."); 180 continue; 181 } 182 183 if (!extDir.isDirectory()) { 184 System.out.println("Extension directory specified is not valid directory: " + extDir); 185 System.out.println("Ignoring extension directory option."); 186 continue; 187 } 188 189 addExtensionDirectory(extDir); 190 } else if (token.equals("--noDefExt")) { count--; 192 tokens.remove(i); 193 useDefExt = false; 194 } else { 195 i++; 196 } 197 } 198 199 } 200 201 public void runTaskClass(List tokens) throws Throwable { 202 203 System.out.println("ACTIVEMQ_HOME: "+ getActiveMQHome()); 204 System.out.println("ACTIVEMQ_BASE: "+ getActiveMQBase()); 205 206 ClassLoader cl = getClassLoader(); 207 208 try { 210 String [] args = (String []) tokens.toArray(new String [tokens.size()]); 211 Class task = cl.loadClass(TASK_DEFAULT_CLASS); 212 Method runTask = task.getMethod("main", new Class [] { String [].class, InputStream .class, PrintStream .class }); 213 runTask.invoke(task.newInstance(), new Object [] { args, System.in, System.out }); 214 } catch (InvocationTargetException e) { 215 throw e.getCause(); 216 } 217 } 218 219 public void addExtensionDirectory(File directory) { 220 extensions.add(directory); 221 } 222 223 public void addClassPathList(String fileList) { 224 if (fileList != null && fileList.length() > 0) { 225 StringTokenizer tokenizer = new StringTokenizer (fileList, ";"); 226 while (tokenizer.hasMoreTokens()) { 227 addClassPath(new File (tokenizer.nextToken())); 228 } 229 } 230 } 231 232 public void addClassPath(File classpath) { 233 activeMQClassPath.add(classpath); 234 } 235 236 242 public boolean canUseExtdir() { 243 try { 244 Main.class.getClassLoader().loadClass(TASK_DEFAULT_CLASS); 245 return false; 246 } catch (ClassNotFoundException e) { 247 return true; 248 } 249 } 250 251 public ClassLoader getClassLoader() throws MalformedURLException { 252 if(classLoader==null) { 253 classLoader = Main.class.getClassLoader(); 255 if (!extensions.isEmpty() || !activeMQClassPath.isEmpty()) { 256 257 ArrayList urls = new ArrayList (); 258 259 for (Iterator iter = activeMQClassPath.iterator(); iter.hasNext();) { 260 File dir = (File ) iter.next(); 261 urls.add(dir.toURL()); 263 } 264 265 for (Iterator iter = extensions.iterator(); iter.hasNext();) { 266 File dir = (File ) iter.next(); 267 if( dir.isDirectory() ) { 268 File [] files = dir.listFiles(); 269 if( files!=null ) { 270 271 Arrays.sort(files, new Comparator (){ 275 public int compare(Object o1, Object o2) { 276 File f1 = (File ) o1; 277 File f2 = (File ) o2; 278 return f1.getName().compareTo(f2.getName()); 279 } 280 }); 281 282 for (int j = 0; j < files.length; j++) { 283 if( files[j].getName().endsWith(".zip") || files[j].getName().endsWith(".jar") ) { 284 urls.add(files[j].toURL()); 286 } 287 } 288 } 289 } 290 } 291 292 URL u[] = new URL [urls.size()]; 293 urls.toArray(u); 294 classLoader = new URLClassLoader (u, classLoader); 295 } 296 Thread.currentThread().setContextClassLoader(classLoader); 297 } 298 return classLoader; 299 } 300 301 public void setActiveMQHome(File activeMQHome) { 302 this.activeMQHome = activeMQHome; 303 } 304 305 public File getActiveMQHome() { 306 if(activeMQHome==null) { 307 if(System.getProperty("activemq.home") != null) { 308 activeMQHome = new File (System.getProperty("activemq.home")); 309 } 310 311 if(activeMQHome==null){ 312 URL url = Main.class.getClassLoader().getResource("org/apache/activemq/console/Main.class"); 314 if (url != null) { 315 try { 316 JarURLConnection jarConnection = (JarURLConnection ) url.openConnection(); 317 url = jarConnection.getJarFileURL(); 318 URI baseURI = new URI (url.toString()).resolve(".."); 319 activeMQHome = new File (baseURI).getCanonicalFile(); 320 System.setProperty("activemq.home",activeMQHome.getAbsolutePath()); 321 } catch (Exception ignored) { 322 } 323 } 324 } 325 326 if(activeMQHome==null){ 327 activeMQHome = new File ("../."); 328 System.setProperty("activemq.home",activeMQHome.getAbsolutePath()); 329 } 330 } 331 332 return activeMQHome; 333 } 334 335 public File getActiveMQBase() { 336 if(activeMQBase==null) { 337 if(System.getProperty("activemq.base") != null) { 338 activeMQBase = new File (System.getProperty("activemq.base")); 339 } 340 341 if(activeMQBase==null){ 342 activeMQBase = getActiveMQHome(); 343 System.setProperty("activemq.base",activeMQBase.getAbsolutePath()); 344 } 345 } 346 347 return activeMQBase; 348 } 349 } 350 | Popular Tags |