KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > java > GroovyCompiler


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.java;
30
31 import com.caucho.log.Log;
32 import com.caucho.server.util.CauchoSystem;
33 import com.caucho.util.CharBuffer;
34 import com.caucho.vfs.IOExceptionWrapper;
35 import com.caucho.vfs.Path;
36 import com.caucho.vfs.Vfs;
37
38 import java.io.IOException JavaDoc;
39 import java.lang.reflect.Method JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 /**
44  * Compiles Groovy source, returning the loaded class.
45  */

46 public class GroovyCompiler extends AbstractJavaCompiler {
47   protected static final Logger JavaDoc log = Log.open(GroovyCompiler.class);
48
49   private final static String JavaDoc GROOVY_COMPILER =
50     "org.codehaus.groovy.tools.FileSystemCompiler";
51
52   private static Class JavaDoc _groovyCompilerClass;
53   private static Method JavaDoc _setOutputDir;
54   private static Method JavaDoc _setClasspath;
55   private static Method JavaDoc _compile;
56   
57   String JavaDoc _userPrefix;
58   
59   public GroovyCompiler(JavaCompiler compiler)
60   {
61     super(compiler);
62   }
63
64   /**
65    * Compile the configured file.
66    *
67    * @param path the path to the java source.
68    * @param lineMap mapping from the generated source to the original files.
69    */

70   protected void compileInt(String JavaDoc []paths, LineMap lineMap)
71     throws IOException JavaDoc
72   {
73     Thread JavaDoc thread = Thread.currentThread();
74     ClassLoader JavaDoc loader = thread.getContextClassLoader();
75
76     if (_groovyCompilerClass == null) {
77       try {
78     _groovyCompilerClass = Class.forName(GROOVY_COMPILER, false, loader);
79     _setClasspath =
80       _groovyCompilerClass.getMethod("setClasspath",
81                      new Class JavaDoc[] { String JavaDoc.class });
82     
83     _setOutputDir =
84       _groovyCompilerClass.getMethod("setOutputDir",
85                      new Class JavaDoc[] { String JavaDoc.class });
86
87     _compile =
88       _groovyCompilerClass.getMethod("compile",
89                      new Class JavaDoc[] { String JavaDoc[].class });
90
91       } catch (Exception JavaDoc e) {
92     throw new RuntimeException JavaDoc(e);
93       }
94     }
95
96     Object JavaDoc compiler;
97     try {
98       compiler = _groovyCompilerClass.newInstance();
99     } catch (Exception JavaDoc e) {
100       throw new RuntimeException JavaDoc(e);
101     }
102
103     try {
104       String JavaDoc sourceExt = _compiler.getSourceExtension();
105
106       String JavaDoc path = paths[0];
107       int tail = path.length() - sourceExt.length();
108       String JavaDoc className = path.substring(0, tail);
109       Path classFile = _compiler.getClassDir().lookup(className + ".class");
110
111       String JavaDoc cp = normalizeClassPath(_compiler.getClassPath(), false);
112
113       _setClasspath.invoke(compiler, new Object JavaDoc[] { cp });
114
115       String JavaDoc dest = normalizePath(_compiler.getClassDirName(), false);
116
117       _setOutputDir.invoke(compiler, new Object JavaDoc[] { dest });
118
119       ArrayList JavaDoc<String JavaDoc> argList = new ArrayList JavaDoc<String JavaDoc>();
120       for (int i = 0; i < paths.length; i++) {
121     Path javaPath = _compiler.getSourceDir().lookup(paths[i]);
122     argList.add(javaPath.getNativePath());
123       }
124
125       String JavaDoc []files = new String JavaDoc[argList.size()];
126
127       argList.toArray(files);
128
129       _compile.invoke(compiler, new Object JavaDoc[] {files});
130     } catch (Exception JavaDoc e) {
131       e.printStackTrace();
132       throw new IOExceptionWrapper(e);
133     }
134   }
135
136   /**
137    * Converts any relative classpath references to the full path.
138    */

139   String JavaDoc normalizeClassPath(String JavaDoc classPath, boolean generateRelative)
140   {
141     char sep = CauchoSystem.getPathSeparatorChar();
142     int head = 0;
143     int tail = 0;
144
145     CharBuffer cb = CharBuffer.allocate();
146
147     while (head < classPath.length()) {
148       tail = classPath.indexOf(sep, head);
149       if (tail < 0)
150         tail = classPath.length();
151
152       if (tail > head) {
153         String JavaDoc segment = classPath.substring(head, tail);
154
155         if (cb.length() != 0)
156           cb.append(sep);
157       
158         cb.append(normalizePath(segment, generateRelative));
159       }
160
161       head = tail + 1;
162     }
163
164     return cb.close();
165   }
166   /**
167    * Normalizes a path.
168    */

169   String JavaDoc normalizePath(String JavaDoc segment, boolean generateRelative)
170   {
171     if (_userPrefix == null) {
172       Path userPath = Vfs.lookup(CauchoSystem.getUserDir());
173       char sep = CauchoSystem.getFileSeparatorChar();
174       _userPrefix = userPath.getNativePath();
175       
176       if (_userPrefix.length() == 0 ||
177           _userPrefix.charAt(_userPrefix.length() - 1) != sep) {
178         _userPrefix = _userPrefix + sep;
179       }
180     }
181     
182     Path path = Vfs.lookup(segment);
183     String JavaDoc nativePath = path.getNativePath();
184
185     if (! generateRelative)
186       return nativePath;
187
188     if (nativePath.startsWith(_userPrefix))
189       nativePath = nativePath.substring(_userPrefix.length());
190
191     if (nativePath.equals(""))
192       return ".";
193     else
194       return nativePath;
195   }
196
197   public static class CompilerThread implements Runnable JavaDoc {
198     private volatile boolean _isDone;
199     
200     public void run()
201     {
202       try {
203       } finally {
204     _isDone = true;
205
206     synchronized (this) {
207       notifyAll();
208     }
209       }
210     }
211   }
212 }
213
Popular Tags