1 29 30 package com.caucho.ejb; 31 32 import com.caucho.loader.EnvironmentClassLoader; 33 import com.caucho.loader.SimpleLoader; 34 import com.caucho.util.ExceptionWrapper; 35 import com.caucho.util.L10N; 36 import com.caucho.vfs.MergePath; 37 import com.caucho.vfs.Path; 38 import com.caucho.vfs.Vfs; 39 40 import java.util.ArrayList ; 41 import java.util.logging.Logger ; 42 43 46 public class EJBCompiler { 47 static L10N L = new L10N(EJBCompiler.class); 48 protected static Logger log 49 = Logger.getLogger(EJBCompiler.class.getName()); 50 51 private Path _classDir; 52 private Path _appDir; 53 private ArrayList <String > _ejbPath = new ArrayList <String >(); 54 55 public EJBCompiler(String []args) 56 throws Exception 57 { 58 int i = 0; 59 60 while (i < args.length) { 61 if (args[i].equals("-app-dir")) { 62 _appDir = Vfs.lookup(args[i + 1]); 63 if (_classDir == null) 64 _classDir = _appDir.lookup("WEB-INF/work"); 65 i += 2; 66 } 67 else if (args[i].equals("-class-dir")) { 68 _classDir = Vfs.lookup(args[i + 1]); 69 i += 2; 70 } 71 else 72 break; 73 } 74 75 if (i == args.length) { 76 printUsage(); 77 throw new Exception ("bad args"); 78 } 79 80 for (; i < args.length; i++) 81 _ejbPath.add(args[i]); 82 } 83 84 public void compile() 85 throws Exception 86 { 87 ClassLoader oldLoader = Thread.currentThread().getContextClassLoader(); 88 89 EnvironmentClassLoader loader; 91 92 loader = new EnvironmentClassLoader(oldLoader); 93 if (_appDir != null) { 94 loader.addLoader(new SimpleLoader(_appDir.lookup("WEB-INF/classes"), 95 null)); 96 } 97 98 Thread.currentThread().setContextClassLoader(loader); 99 100 try { 101 EjbServerManager container = new EjbServerManager(); 102 container.setValidateDatabaseSchema(false); 103 if (_classDir == null) 104 container.setWorkPath(Vfs.lookup(".")); 105 else { 106 container.setWorkPath(_classDir); 107 } 108 109 MergePath mergePath = new MergePath(); 110 mergePath.addClassPath(loader); 111 if (_appDir != null) 112 mergePath.addMergePath(_appDir.lookup("WEB-INF")); 113 if (_classDir != null) 114 mergePath.addMergePath(_classDir); 115 116 118 for (int i = 0; i < _ejbPath.size(); i++) { 119 Path path = mergePath.lookup(_ejbPath.get(i)); 120 container.addEJBPath(path, path); 121 } 122 123 container.build(); 124 } finally { 125 Thread.currentThread().setContextClassLoader(oldLoader); 126 } 127 } 128 129 public static void main(String []args) 130 throws Throwable 131 { 132 if (args.length == 0) { 133 printUsage(); 134 System.exit(1); 135 } 136 137 try { 138 new EJBCompiler(args).compile(); 139 } catch (Throwable e) { 140 while (e instanceof ExceptionWrapper && 141 ((ExceptionWrapper) e).getRootCause() != null) { 142 e = ((ExceptionWrapper) e).getRootCause(); 143 } 144 145 throw e; 146 } 147 } 148 149 private static void printUsage() 150 { 151 System.out.println("usage: com.caucho.ejb.EJBCompiler [flags] foo.ejb"); 152 System.out.println(" -class-dir: The directory where the classes will be generated."); 153 System.out.println(" -app-dir: The source (web-app) directory."); 154 } 155 } 156 157 | Popular Tags |