001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.types.optional;
019:
020: import org.apache.tools.ant.Project;
021: import org.apache.tools.ant.ProjectComponent;
022: import org.apache.tools.ant.types.Path;
023: import org.apache.tools.ant.types.Reference;
024: import org.apache.tools.ant.util.ScriptRunnerBase;
025: import org.apache.tools.ant.util.ScriptRunnerHelper;
026:
027: import java.io.File;
028:
029: /**
030: * This is a {@link ProjectComponent} that has script support built in
031: * Use it as a foundation for scriptable things.
032: */
033: public abstract class AbstractScriptComponent extends ProjectComponent {
034: /**
035: * script runner helper
036: */
037: private ScriptRunnerHelper helper = new ScriptRunnerHelper();
038:
039: /**
040: * script runner.
041: */
042: private ScriptRunnerBase runner = null;
043:
044: /**
045: * Set the project.
046: * @param project the owner of this component.
047: */
048: public void setProject(Project project) {
049: super .setProject(project);
050: helper.setProjectComponent(this );
051: }
052:
053: /**
054: * Get our script runner
055: * @return the runner
056: */
057: public ScriptRunnerBase getRunner() {
058: initScriptRunner();
059: return runner;
060: }
061:
062: /**
063: * Load the script from an external file ; optional.
064: *
065: * @param file the file containing the script source.
066: */
067: public void setSrc(File file) {
068: helper.setSrc(file);
069: }
070:
071: /**
072: * The script text.
073: *
074: * @param text a component of the script text to be added.
075: */
076: public void addText(String text) {
077: helper.addText(text);
078: }
079:
080: /**
081: * Defines the manager.
082: *
083: * @param manager the scripting manager.
084: */
085: public void setManager(String manager) {
086: helper.setManager(manager);
087: }
088:
089: /**
090: * Defines the language (required).
091: *
092: * @param language the scripting language name for the script.
093: */
094: public void setLanguage(String language) {
095: helper.setLanguage(language);
096: }
097:
098: /**
099: * Initialize the script runner. Calls this before running the system
100: */
101: protected void initScriptRunner() {
102: if (runner != null) {
103: return;
104: }
105: helper.setProjectComponent(this );
106: runner = helper.getScriptRunner();
107: }
108:
109: /**
110: * Set the classpath to be used when searching for classes and resources.
111: *
112: * @param classpath an Ant Path object containing the search path.
113: */
114: public void setClasspath(Path classpath) {
115: helper.setClasspath(classpath);
116: }
117:
118: /**
119: * Classpath to be used when searching for classes and resources.
120: *
121: * @return an empty Path instance to be configured by Ant.
122: */
123: public Path createClasspath() {
124: return helper.createClasspath();
125: }
126:
127: /**
128: * Set the classpath by reference.
129: *
130: * @param r a Reference to a Path instance to be used as the classpath
131: * value.
132: */
133: public void setClasspathRef(Reference r) {
134: helper.setClasspathRef(r);
135: }
136:
137: /**
138: * Run a script
139: * @param execName name of the script
140: */
141: protected void executeScript(String execName) {
142: getRunner().executeScript(execName);
143: }
144: }
|