View Javadoc

1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.services.scripting;
16  
17  import org.figure8.join.core.Configurable;
18  import org.figure8.join.core.ParameterDefinition;
19  import org.figure8.join.core.InvalidParameterException;
20  import org.figure8.join.util.LogUtil;
21  
22  import org.apache.commons.logging.Log;
23  
24  import java.io.File;
25  import java.io.OutputStream;
26  import java.util.List;
27  import java.util.Iterator;
28  import java.util.ArrayList;
29  import java.util.Properties;
30  /**
31   * This is an abstract base class allowing to manage script initialization,
32   * parameters binding and execution. This class implements {@link Configurable}
33   * so that it can be configured through an interactive process.
34   * <br/>
35   * Subclasses may implement behaviour specific to the script engine they are
36   * wrapping. They may also be Configurable in order to add specific configuration
37   * parameters to the one defined in base class (that is the script path).
38   *
39   * @see org.figure8.join.core.Configurable
40   *
41   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
42   * @version $Revision: 1.2 $
43   */
44  public abstract class ScriptLauncher implements Configurable{
45  
46     // Static -------------------------------------------------------------------
47  
48     /** Get a commons logger. */
49     private static final Log log = LogUtil.getLog(ScriptLauncher.class);
50  
51     /** Name of configurable parameter denoting the path of script to run. */
52     public static final String SCRIPT_PARAM = "scriptPath";
53  
54     /** List of {@link ParameterDefinition}s supported by this launcher */
55     protected static List parameters = new ArrayList();
56  
57     /** ParameterDefinition representation of parameter denoting the path of script to run. */
58     protected static final ParameterDefinition scriptParam = new ParameterDefinition(SCRIPT_PARAM,
59             "The path to script file", "//host/share/myscript.ext", true);
60  
61     static{
62        parameters.add(scriptParam);
63     }
64  
65  
66     // Attributes ---------------------------------------------------------------
67  
68     /** The file representing script. */
69     private File script;
70     /** The path of script to execute. */
71     private String scriptPath;
72     /** The output stream that may be used for script logging. */
73     private OutputStream logOutputStream;
74  
75  
76     // Constructors -------------------------------------------------------------
77  
78     /** Creates a new instance of ScriptLauncher. */
79     protected ScriptLauncher(){
80     }
81  
82     /**
83      * Creates a new instance of ScriptLauncher spceifying script path.
84      * @param scriptPath the path of script to later execute
85      * @throws InvalidParameterException if path cannot be accessed
86      */
87     protected ScriptLauncher(String scriptPath) throws InvalidParameterException{
88        setScriptPath(scriptPath);
89     }
90  
91  
92     // Public -------------------------------------------------------------------
93  
94     /** @return The path of script to execute */
95     public String getScriptPath(){
96        return scriptPath;
97     }
98     /**
99      * @param scriptPath the path of script to execute
100     * @throws InvalidParameterException if path cannot be accessed
101     */
102    public void setScriptPath(String scriptPath) throws InvalidParameterException{
103       this.scriptPath = scriptPath;
104       // Get file for script.
105       script = new File(scriptPath);
106       if (!script.isFile()){
107          log.error("The script file denoted by " + scriptPath + " does not exists or it not reachable");
108          throw new InvalidParameterException("The script file denoted by " + scriptPath + " does not exists or it not reachable");
109       }
110    }
111 
112    /** @return The file corresponding to script or null if <b>scriptPath</b> has not been set yet. */
113    public File getScript(){
114       return script;
115    }
116 
117    /** @return The output stream that script may use for logging output */
118    public OutputStream getLogOutputStream(){
119       return logOutputStream;
120    }
121    /** @param logOutputStream The output stream that script may use for logging output */
122    public void setLogOutputStream(OutputStream logOutputStream){
123       this.logOutputStream = logOutputStream;
124    }
125 
126    /**
127     * Abstract method that do the real job by running the script specified
128     * by <code>scriptPath</code>. Implementation may use <code>properties</code>
129     * to initialize their runtime environment with corresponding global variables.
130     * @param properties The runtime properties to inject into execution environment
131     * @throws ScriptException if something wrong occurs during script parsing, evaluation, ...
132     */
133    public abstract void runScript(Properties properties) throws ScriptException;
134 
135 
136    // Implementation of Configurable -------------------------------------------
137 
138    /**
139     * Get this object parameters definitions as a list
140     * @return A list of {@code ParameterDefinition} objects
141     */
142    public List getParameterDefinitionsAsList(){
143       return parameters;
144    }
145 
146    /**
147     * Get this object parameters definitions as an array. Implementation
148     * uses result from <code>getParameterDefintiionsAdList()</code> so that
149     * subclasses may not override this implementation.
150     * @return An array of {@code ParameterDefinition} objects
151     */
152    public ParameterDefinition[] getParameterDefinitions(){
153       List result = getParameterDefinitionsAsList();
154       return (ParameterDefinition[])result.toArray(new ParameterDefinition[result.size()]);
155    }
156 
157    /**
158     * Set the value of a parameter using its nama
159     * @param parameterName The name of parameter so set value for
160     * @param parameterValue The value of the paramater
161     * @throws org.figure8.join.core.InvalidParameterException if this parameter is not supported by this object
162     */
163    public void setParameter(String parameterName, String parameterValue) throws InvalidParameterException{
164       if (SCRIPT_PARAM.equals(parameterName))
165          setScriptPath(parameterValue);
166       else{
167          log.warn("The parameter '" + parameterName + "' is not supported by ScriptLauncher");
168          throw new InvalidParameterException("The parameter '" + parameterName + "' is not supported by ScriptLauncher");
169       }
170    }
171 
172    /**
173     * Set the value of a parameter using its definition. Implementation
174     * deletgates the setting of parameter to <code>setParameter(String, String)</code>
175     * so that subclasses may not ovveride this implementation.
176     * @param parameter The definitino of the paramater to set
177     * @param parameterValue The value of the parameter
178     * @throws org.figure8.join.core.InvalidParameterException if this parameter is not supported by this object
179     */
180    public void setParameter(ParameterDefinition parameter, String parameterValue) throws InvalidParameterException{
181       setParameter(parameter.getName(), parameterValue);
182    }
183 
184    /**
185     * Convenient methods for setting all attributes values using a single method.
186     * @param parameters Properties where keys are parameter names
187     * @throws org.figure8.join.core.InvalidParameterException if one of these parameters is not supported by this object
188     */
189    public void setParameters(Properties parameters) throws InvalidParameterException{
190       Iterator keys = parameters.keySet().iterator();
191       while (keys != null && keys.hasNext()){
192          String parameterName = (String)keys.next();
193          String parameterValue = parameters.getProperty(parameterName);
194          setParameter(parameterName, parameterValue);
195       }
196    }
197 }