KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > EJBCompiler


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 /**
44  * Compiles EJB classes prior to instantiating in the server.
45  */

46 public class EJBCompiler {
47   static L10N L = new L10N(EJBCompiler.class);
48   protected static Logger JavaDoc log
49     = Logger.getLogger(EJBCompiler.class.getName());
50
51   private Path _classDir;
52   private Path _appDir;
53   private ArrayList JavaDoc<String JavaDoc> _ejbPath = new ArrayList JavaDoc<String JavaDoc>();
54
55   public EJBCompiler(String JavaDoc []args)
56     throws Exception JavaDoc
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 JavaDoc("bad args");
78     }
79
80     for (; i < args.length; i++)
81       _ejbPath.add(args[i]);
82   }
83
84   public void compile()
85     throws Exception JavaDoc
86   {
87     ClassLoader JavaDoc oldLoader = Thread.currentThread().getContextClassLoader();
88     
89     // wrap so the EJB code will work.
90
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       // container.setSearchPath(mergePath);
117

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 JavaDoc []args)
130     throws Throwable JavaDoc
131   {
132     if (args.length == 0) {
133       printUsage();
134       System.exit(1);
135     }
136
137     try {
138       new EJBCompiler(args).compile();
139     } catch (Throwable JavaDoc 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