001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.ejb;
031:
032: import com.caucho.loader.EnvironmentClassLoader;
033: import com.caucho.loader.SimpleLoader;
034: import com.caucho.util.ExceptionWrapper;
035: import com.caucho.util.L10N;
036: import com.caucho.vfs.MergePath;
037: import com.caucho.vfs.Path;
038: import com.caucho.vfs.Vfs;
039:
040: import java.util.ArrayList;
041: import java.util.logging.Logger;
042:
043: /**
044: * Compiles EJB classes prior to instantiating in the server.
045: */
046: public class EJBCompiler {
047: static L10N L = new L10N(EJBCompiler.class);
048: protected static Logger log = Logger.getLogger(EJBCompiler.class
049: .getName());
050:
051: private Path _classDir;
052: private Path _appDir;
053: private ArrayList<String> _ejbPath = new ArrayList<String>();
054:
055: public EJBCompiler(String[] args) throws Exception {
056: int i = 0;
057:
058: while (i < args.length) {
059: if (args[i].equals("-app-dir")) {
060: _appDir = Vfs.lookup(args[i + 1]);
061: if (_classDir == null)
062: _classDir = _appDir.lookup("WEB-INF/work");
063: i += 2;
064: } else if (args[i].equals("-class-dir")) {
065: _classDir = Vfs.lookup(args[i + 1]);
066: i += 2;
067: } else
068: break;
069: }
070:
071: if (i == args.length) {
072: printUsage();
073: throw new Exception("bad args");
074: }
075:
076: for (; i < args.length; i++)
077: _ejbPath.add(args[i]);
078: }
079:
080: public void compile() throws Exception {
081: ClassLoader oldLoader = Thread.currentThread()
082: .getContextClassLoader();
083:
084: // wrap so the EJB code will work.
085: EnvironmentClassLoader loader;
086:
087: loader = new EnvironmentClassLoader(oldLoader);
088: if (_appDir != null) {
089: loader.addLoader(new SimpleLoader(_appDir
090: .lookup("WEB-INF/classes"), null));
091: }
092:
093: Thread.currentThread().setContextClassLoader(loader);
094:
095: try {
096: /*
097: EjbServerManager container = new EjbServerManager();
098: container.setValidateDatabaseSchema(false);
099: if (_classDir == null)
100: container.setWorkPath(Vfs.lookup("."));
101: else {
102: container.setWorkPath(_classDir);
103: }
104:
105: MergePath mergePath = new MergePath();
106: mergePath.addClassPath(loader);
107: if (_appDir != null)
108: mergePath.addMergePath(_appDir.lookup("WEB-INF"));
109: if (_classDir != null)
110: mergePath.addMergePath(_classDir);
111:
112: // container.setSearchPath(mergePath);
113:
114: for (int i = 0; i < _ejbPath.size(); i++) {
115: Path path = mergePath.lookup(_ejbPath.get(i));
116: container.addEJBPath(path, path);
117: }
118:
119: container.build();
120: */
121: } finally {
122: Thread.currentThread().setContextClassLoader(oldLoader);
123: }
124: }
125:
126: public static void main(String[] args) throws Throwable {
127: if (args.length == 0) {
128: printUsage();
129: System.exit(1);
130: }
131:
132: EnvironmentClassLoader.initializeEnvironment();
133:
134: try {
135: new EJBCompiler(args).compile();
136: } catch (Throwable e) {
137: while (e instanceof ExceptionWrapper
138: && ((ExceptionWrapper) e).getRootCause() != null) {
139: e = ((ExceptionWrapper) e).getRootCause();
140: }
141:
142: throw e;
143: }
144: }
145:
146: private static void printUsage() {
147: System.out
148: .println("usage: com.caucho.ejb.EJBCompiler [flags] foo.ejb");
149: System.out
150: .println(" -class-dir: The directory where the classes will be generated.");
151: System.out
152: .println(" -app-dir: The source (web-app) directory.");
153: }
154: }
|