KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > driver > janino > CodeCompiler


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.driver.janino;
17
18 import org.codehaus.janino.Scanner;
19 import org.codehaus.janino.ScriptEvaluator;
20 import scriptella.spi.Resource;
21 import scriptella.util.ExceptionUtils;
22 import scriptella.util.IOUtils;
23
24 import java.io.BufferedReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.Reader JavaDoc;
27 import java.util.IdentityHashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 /**
31  * Compiles Janino scripts.
32  *
33  * @author Fyodor Kupolov
34  * @version 1.0
35  */

36 final class CodeCompiler {
37     //Allowed exceptions to be thrown by a script
38
private static final Class JavaDoc[] THROWN_EXCEPTIONS = new Class JavaDoc[]{Exception JavaDoc.class};
39     //Compiled scripts(Static methods) cache
40
private Map JavaDoc<Resource, Object JavaDoc> objectCache = new IdentityHashMap JavaDoc<Resource, Object JavaDoc>();
41
42     public JaninoScript compileScript(final Resource resource) {
43         return (JaninoScript) compile(resource, false);
44     }
45
46     public JaninoQuery compileQuery(final Resource resource) {
47         return (JaninoQuery) compile(resource, true);
48     }
49
50     private Object JavaDoc compile(final Resource content, final boolean query) {
51         Object JavaDoc ctx = objectCache.get(content);
52         if (ctx == null) {
53             ScriptEvaluator evaluator = new ScriptEvaluator();
54             //Exception are not required to be handled
55
evaluator.setThrownExceptions(THROWN_EXCEPTIONS);
56             evaluator.setParentClassLoader(getClass().getClassLoader());
57             Class JavaDoc<?> type = query ? JaninoQuery.class : JaninoScript.class;
58             evaluator.setExtendedType(type);
59             evaluator.setStaticMethod(false);
60             evaluator.setMethodName("execute");
61             evaluator.setClassName(type.getName()+"_Generated");
62
63             Reader JavaDoc r = null;
64             try {
65                 r = content.open();
66                 evaluator.cook(content.toString(), r);
67             } catch (Exception JavaDoc e) {
68                 throw guestErrorStatement(new JaninoProviderException("Compilation failed", e), content);
69             } finally {
70                 IOUtils.closeSilently(r);
71             }
72             Class JavaDoc<?> cl = evaluator.getMethod().getDeclaringClass();
73             try {
74                 ctx = cl.newInstance();
75             } catch (Exception JavaDoc e) {
76                 throw new JaninoProviderException("Unable to instantiate compiled class", e);
77             }
78             objectCache.put(content, ctx);
79         }
80         return ctx;
81     }
82
83     /**
84      * Finds error statement which caused compilation error.
85      */

86     private static JaninoProviderException guestErrorStatement(JaninoProviderException pe, Resource r) {
87         Throwable JavaDoc cause = pe.getCause();
88         if (cause instanceof Scanner.LocatedException) {
89             Scanner.LocatedException le = (Scanner.LocatedException) cause;
90             if (le.getLocation() != null) {
91                 String JavaDoc line = getLine(r, le.getLocation().getLineNumber());
92                 pe.setErrorStatement(line);
93             }
94         }
95         return pe;
96     }
97
98     static String JavaDoc getLine(Resource resource, int line) {
99         BufferedReader JavaDoc r = null;
100         try {
101             r = new BufferedReader JavaDoc(resource.open());
102             for (int i = 0; i < line - 1; i++) {
103                 if (r.readLine() == null) {
104                     return null;
105                 }
106             }
107             return r.readLine();
108         } catch (IOException JavaDoc e) {
109             ExceptionUtils.ignoreThrowable(e);
110         } finally {
111             IOUtils.closeSilently(r);
112         }
113         return null;
114
115     }
116
117
118 }
119
Popular Tags