KickJava   Java API By Example, From Geeks To Geeks.

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


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.ProjectComponent;
21 import org.apache.tools.ant.types.Path;
22 import java.io.File JavaDoc;
23 import org.apache.tools.ant.types.Reference;
24
25
26 /**
27  * A class to help in creating, setting and getting
28  * script runners.
29  */

30 public class ScriptRunnerHelper {
31     private ClasspathUtils.Delegate cpDelegate = null;
32     private File JavaDoc srcFile;
33     private String JavaDoc manager = "auto";
34     private String JavaDoc language;
35     private String JavaDoc text;
36     private boolean setBeans = true;
37     private ProjectComponent projectComponent;
38     private ClassLoader JavaDoc scriptLoader = null;
39
40     /**
41      * Set the project component associated with this helper.
42      * @param component the project component that owns this helper.
43      */

44     public void setProjectComponent(ProjectComponent component) {
45         this.projectComponent = component;
46     }
47
48     /**
49      * Create and set text on a script.
50      * @return the created or reused script runner.
51      */

52     public ScriptRunnerBase getScriptRunner() {
53         ScriptRunnerBase runner = getRunner();
54         if (srcFile != null) {
55             runner.setSrc(srcFile);
56         }
57         if (text != null) {
58             runner.addText(text);
59         }
60         if (setBeans) {
61             runner.bindToComponent(projectComponent);
62         } else {
63             runner.bindToComponentMinimum(projectComponent);
64         }
65         return runner;
66     }
67
68     /**
69      * Classpath to be used when searching for classes and resources.
70      *
71      * @return an empty Path instance to be configured by Ant.
72      */

73     public Path createClasspath() {
74         return getClassPathDelegate().createClasspath();
75     }
76
77     /**
78      * Set the classpath to be used when searching for classes and resources.
79      *
80      * @param classpath an Ant Path object containing the search path.
81      */

82     public void setClasspath(Path classpath) {
83         getClassPathDelegate().setClasspath(classpath);
84     }
85
86     /**
87      * Set the classpath by reference.
88      *
89      * @param r a Reference to a Path instance to be used as the classpath
90      * value.
91      */

92     public void setClasspathRef(Reference r) {
93         getClassPathDelegate().setClasspathref(r);
94     }
95
96     /**
97      * Load the script from an external file ; optional.
98      *
99      * @param file the file containing the script source.
100      */

101     public void setSrc(File JavaDoc file) {
102         this.srcFile = file;
103     }
104
105     /**
106      * The script text.
107      *
108      * @param text a component of the script text to be added.
109      */

110     public void addText(String JavaDoc text) {
111         this.text = text;
112     }
113
114     /**
115      * Defines the script manager - defaults to "auto".
116      *
117      * @param manager the scripting manager - "bsf" or "javax" or "auto"
118      */

119     public void setManager(String JavaDoc manager) {
120         this.manager = manager;
121     }
122
123     /**
124      * Defines the language (required).
125      *
126      * @param language the scripting language name for the script.
127      */

128     public void setLanguage(String JavaDoc language) {
129         this.language = language;
130     }
131
132     /**
133      * Get the language.
134      * @return the scripting language.
135      */

136     public String JavaDoc getLanguage() {
137         return language;
138     }
139
140     /**
141      * Set the setbeans attribute.
142      * If this is true, <script> will create variables in the
143      * script instance for all
144      * properties, targets and references of the current project.
145      * It this is false, only the project and self variables will
146      * be set.
147      * The default is true.
148      * @param setBeans the value to set.
149      */

150     public void setSetBeans(boolean setBeans) {
151         this.setBeans = setBeans;
152     }
153
154     /**
155      * Used when called by scriptdef.
156      * @param loader the loader used by scriptdef.
157      */

158     public void setClassLoader(ClassLoader JavaDoc loader) {
159         scriptLoader = loader;
160     }
161
162
163     private ClassLoader JavaDoc generateClassLoader() {
164         if (scriptLoader != null) {
165             return scriptLoader;
166         }
167         if (cpDelegate == null) {
168             scriptLoader = getClass().getClassLoader();
169             return scriptLoader;
170         }
171
172         scriptLoader = cpDelegate.getClassLoader();
173         return scriptLoader;
174     }
175
176     private ClasspathUtils.Delegate getClassPathDelegate() {
177         if (cpDelegate == null) {
178             cpDelegate = ClasspathUtils.getDelegate(projectComponent);
179         }
180         return cpDelegate;
181     }
182
183     /**
184      * Get a script runner.
185      */

186     private ScriptRunnerBase getRunner() {
187         return new ScriptRunnerCreator(
188             projectComponent.getProject()).createRunner(
189                 manager, language, generateClassLoader());
190     }
191 }
192
Popular Tags