KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > ScriptRunnerCreator


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 package org.apache.tools.ant.util;
19
20 import org.apache.tools.ant.BuildException;
21 import org.apache.tools.ant.Project;
22
23 /**
24  * This is a helper class used by ScriptRunnerHelper to
25  * create a ScriptRunner based on a classloader and
26  * on a language.
27  */

28 public class ScriptRunnerCreator {
29     private static final String JavaDoc AUTO = "auto";
30     private static final String JavaDoc OATAU = "org.apache.tools.ant.util";
31     private static final String JavaDoc UTIL_OPT = OATAU + ".optional";
32
33     private static final String JavaDoc BSF = "bsf";
34     private static final String JavaDoc BSF_PACK = "org.apache.bsf";
35     private static final String JavaDoc BSF_MANAGER = BSF_PACK + ".BSFManager";
36     private static final String JavaDoc BSF_RUNNER = UTIL_OPT + ".ScriptRunner";
37
38     private static final String JavaDoc JAVAX = "javax";
39     private static final String JavaDoc JAVAX_MANAGER = "javax.script.ScriptEngineManager";
40     private static final String JavaDoc JAVAX_RUNNER = UTIL_OPT + ".JavaxScriptRunner";
41
42     private Project project;
43     private String JavaDoc manager;
44     private String JavaDoc language;
45     private ClassLoader JavaDoc scriptLoader = null;
46
47     /**
48      * Constructor for creator.
49      * @param project the current project.
50      */

51     public ScriptRunnerCreator(Project project) {
52         this.project = project;
53     }
54
55     /**
56      * Create a ScriptRunner.
57      * @param manager the script manager ("auto" | "bsf" | "javax")
58      * @param language the language.
59      * @param classLoader the classloader to use
60      * @return the created script runner.
61      * @throws BuildException if unable to create the ScriptRunner.
62      */

63     public ScriptRunnerBase createRunner(
64         String JavaDoc manager, String JavaDoc language, ClassLoader JavaDoc classLoader) {
65         this.manager = manager;
66         this.language = language;
67         this.scriptLoader = classLoader;
68
69         if (language == null) {
70             throw new BuildException("script language must be specified");
71         }
72         if (!manager.equals(AUTO) && !manager.equals(JAVAX) && !manager.equals(BSF)) {
73                 throw new BuildException(
74                     "Unsupported language prefix " + manager);
75         }
76
77         // Check for bsf first then javax
78
// This version does not check if the scriptManager
79
// supports the language.
80

81         ScriptRunnerBase ret = null;
82         ret = createRunner(BSF, BSF_MANAGER, BSF_RUNNER);
83         if (ret == null) {
84             ret = createRunner(JAVAX, JAVAX_MANAGER, JAVAX_RUNNER);
85         }
86         if (ret != null) {
87             return ret;
88         }
89         if (JAVAX.equals(manager)) {
90             throw new BuildException(
91                 "Unable to load the script engine manager "
92                 + "(" + JAVAX_MANAGER + ")");
93         } else if (BSF.equals(manager)) {
94             throw new BuildException(
95                 "Unable to load the BSF script engine manager "
96                 + "(" + BSF_MANAGER + ")");
97         } else {
98             throw new BuildException(
99                 "Unable to load a script engine manager "
100                 + "(" + BSF_MANAGER + " or " + JAVAX_MANAGER + ")");
101         }
102     }
103
104     /**
105      * Create a script runner if the scriptManager matches the passed
106      * in manager.
107      * This checks if the script manager exists in the scriptLoader
108      * classloader and if so it creates and returns the script runner.
109      * @param checkManager check if the manager matchs this value.
110      * @param mangagerClass the name of the script manager class.
111      * @param runnerClass the name of ant's script runner for this manager.
112      * @return the script runner class.
113      * @throws BuildException if there is a problem creating the runner class.
114      */

115     private ScriptRunnerBase createRunner(
116         String JavaDoc checkManager, String JavaDoc managerClass, String JavaDoc runnerClass) {
117         ScriptRunnerBase runner = null;
118         if (!manager.equals(AUTO) && !manager.equals(checkManager)) {
119             return null;
120         }
121         if (scriptLoader.getResource(
122                 LoaderUtils.classNameToResource(managerClass)) == null) {
123             return null;
124         }
125         try {
126             runner = (ScriptRunnerBase) Class.forName(
127                 runnerClass, true, scriptLoader).newInstance();
128             runner.setProject(project);
129         } catch (Exception JavaDoc ex) {
130             ReflectUtil.throwBuildException(ex);
131             // NotReached
132
}
133
134         runner.setLanguage(language);
135         runner.setScriptClassLoader(scriptLoader);
136         return runner;
137     }
138 }
139
Popular Tags