KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > optional > JavaxScriptRunner


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.util.optional;
20
21 import org.apache.tools.ant.BuildException;
22
23
24 import java.util.Iterator JavaDoc;
25 import org.apache.tools.ant.util.ScriptRunnerBase;
26 import org.apache.tools.ant.util.ReflectUtil;
27 import org.apache.tools.ant.util.ReflectWrapper;
28
29 /**
30  * This class is used to run scripts using JSR 223.
31  * @since Ant 1.7.0
32  */

33 public class JavaxScriptRunner extends ScriptRunnerBase {
34     private ReflectWrapper engine;
35
36     /**
37      * Get the name of the manager prefix.
38      * @return "javax"
39      */

40     public String JavaDoc getManagerName() {
41         return "javax";
42     }
43
44     /** {@inheritDoc}. */
45     public boolean supportsLanguage() {
46         if (engine != null) {
47             return true;
48         }
49         checkLanguage();
50         ClassLoader JavaDoc origLoader = replaceContextLoader();
51         try {
52             return createEngine() != null;
53         } catch (Exception JavaDoc ex) {
54             return false;
55         } finally {
56             restoreContextLoader(origLoader);
57         }
58     }
59
60     /**
61      * Do the work to run the script.
62      *
63      * @param execName the name that will be passed to the
64      * scripting engine for this script execution.
65      *
66      * @exception BuildException if someting goes wrong exectuing the script.
67      */

68     public void executeScript(String JavaDoc execName) throws BuildException {
69         evaluateScript(execName);
70     }
71
72     /**
73      * Do the work to eval the script.
74      *
75      * @param execName the name that will be passed to the
76      * scripting engine for this script execution.
77      * @return the result of the evalulation
78      * @exception BuildException if someting goes wrong exectuing the script.
79      */

80     public Object JavaDoc evaluateScript(String JavaDoc execName) throws BuildException {
81         checkLanguage();
82         ClassLoader JavaDoc origLoader = replaceContextLoader();
83         try {
84             ReflectWrapper engine = createEngine();
85             if (engine == null) {
86                 throw new BuildException(
87                     "Unable to create javax script engine for "
88                     + getLanguage());
89             }
90             for (Iterator JavaDoc i = getBeans().keySet().iterator(); i.hasNext();) {
91                 String JavaDoc key = (String JavaDoc) i.next();
92                 Object JavaDoc value = getBeans().get(key);
93                 engine.invoke(
94                     "put", String JavaDoc.class, key, Object JavaDoc.class, value);
95             }
96             // execute the script
97
return engine.invoke("eval", String JavaDoc.class, getScript());
98         } catch (Exception JavaDoc be) {
99             Throwable JavaDoc t = be;
100             Throwable JavaDoc te = (Throwable JavaDoc) ReflectUtil.invoke(be, "getCause");
101             if (te != null) {
102                 if (te instanceof BuildException) {
103                     throw (BuildException) te;
104                 } else {
105                     t = te;
106                 }
107             }
108             throw new BuildException(t);
109         } finally {
110             restoreContextLoader(origLoader);
111         }
112     }
113
114     private ReflectWrapper createEngine() throws Exception JavaDoc {
115         if (engine != null) {
116             return engine;
117         }
118         ReflectWrapper manager = new ReflectWrapper(
119             getClass().getClassLoader(), "javax.script.ScriptEngineManager");
120         Object JavaDoc e = manager.invoke(
121             "getEngineByName", String JavaDoc.class, getLanguage());
122         if (e == null) {
123             return null;
124         }
125         ReflectWrapper ret = new ReflectWrapper(e);
126         if (getKeepEngine()) {
127             this.engine = ret;
128         }
129         return ret;
130     }
131 }
132
Popular Tags