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.core.ContainerContextHandler;
21  import org.figure8.join.core.setup.BootstrapUtil;
22  import org.figure8.join.core.setup.BootstrapManager;
23  import org.figure8.join.services.scripting.persistence.ScriptLogInfoDao;
24  import org.figure8.join.util.LogUtil;
25  
26  import org.apache.commons.logging.Log;
27  
28  import java.io.File;
29  import java.util.List;
30  import java.util.Iterator;
31  import java.util.Properties;
32  /**
33   * This is a support class to extend for classes wanting to launch scripts.
34   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
35   * @version $Revision: 1.2 $
36   */
37  public abstract class ScriptLauncherAdapterSupport implements Configurable{
38  
39     // Static -------------------------------------------------------------------
40  
41     /** Get a commons logger. */
42     private static final Log log = LogUtil.getLog(ScriptLauncherAdapterSupport.class);
43  
44  
45     // Attributes ---------------------------------------------------------------
46  
47     /** The name identifying this script adapter. */
48     protected String name;
49  
50  
51     // Public -------------------------------------------------------------------
52  
53     /**
54      * Retrieve the name of this consumer.
55      * @return The name identifying this consumer
56      */
57     public String getName(){
58        return name;
59     }
60     /**
61      * Set the name identifying this consumer.
62      * @param name The name of this consumer
63      */
64     public void setName(String name){
65        this.name = name;
66     }
67  
68     /**
69      * Subclasses must implement this method in order to specify the
70      * ScriptLauncher implementation to use (and thus the scripting language).
71      * @return The script launcher implementation
72      */
73     public abstract ScriptLauncher getScriptLauncher();
74  
75  
76     // Implementation of Configurable -------------------------------------------
77  
78     /**
79      * Get this object parameters definitions as a list
80      * @return A list of {@code ParameterDefinition} objects
81      */
82     public List getParameterDefinitionsAsList(){
83        // Use script launcher parameters as base.
84        List result = getScriptLauncher().getParameterDefinitionsAsList();
85        return result;
86     }
87  
88     /**
89      * Get this object parameters definitions as an array
90      * @return An array of {@code ParameterDefinition} objects
91      */
92     public ParameterDefinition[] getParameterDefinitions(){
93        List result = getParameterDefinitionsAsList();
94        return (ParameterDefinition[])result.toArray(new ParameterDefinition[result.size()]);
95     }
96  
97     /**
98      * Set the value of a parameter using its name
99      * @param parameterName The name of parameter so set value for
100     * @param parameterValue The value of the paramater
101     * @throws org.figure8.join.core.InvalidParameterException if this parameter is not supported by this object
102     */
103    public void setParameter(String parameterName, String parameterValue) throws InvalidParameterException{
104       // Delegate to script launcher class.
105       getScriptLauncher().setParameter(parameterName, parameterValue);
106    }
107 
108    /**
109     * Set the value of a parameter using its definition. Implementation
110     * delegates the setting of parameter to <code>setParameter(String, String)</code>
111     * so that subclasses may not ovveride this implementation.
112     * @param parameter The definitino of the paramater to set
113     * @param parameterValue The value of the parameter
114     * @throws org.figure8.join.core.InvalidParameterException if this parameter is not supported by this object
115     */
116    public void setParameter(ParameterDefinition parameter, String parameterValue) throws InvalidParameterException{
117       setParameter(parameter.getName(), parameterValue);
118    }
119 
120    /**
121     * Convenient methods for setting all attributes values using a single method.
122     * @param parameters Properties where keys are parameter names
123     * @throws org.figure8.join.core.InvalidParameterException if one of these parameters is not supported by this object
124     */
125    public void setParameters(Properties parameters) throws InvalidParameterException{
126       Iterator keys = parameters.keySet().iterator();
127       while (keys != null && keys.hasNext()){
128          String parameterName = (String)keys.next();
129          String parameterValue = parameters.getProperty(parameterName);
130          setParameter(parameterName, parameterValue);
131       }
132    }
133 
134 
135    // Protected ----------------------------------------------------------------
136 
137    /** */
138    protected File createNewLogFile(){
139       // Get Join home directory from bootstrap manager.
140       BootstrapManager manager = BootstrapUtil.getBootstrapManager();
141       String root = manager.getJoinHome();
142       // Check subdirectory for storing logs.
143       File logsDirectory = new File(root, "scriptlogs");
144       File scriptDirectory = new File(logsDirectory, name);
145       if (!scriptDirectory.isDirectory())
146          scriptDirectory.mkdirs();
147       // Create new log file within this subdir.
148       return new File(scriptDirectory, name + "-" + System.currentTimeMillis() + ".log");
149    }
150 
151    /**
152     * Save a ScriptLogInfo into datastore if a DAO if available
153     * @param logInfo The bean representing log info to save
154     * @return true if save has been done, false otherwise
155     */
156    protected boolean saveLogInfoIfAvailable(ScriptLogInfo logInfo){
157       try{
158          // Get dao from container context if possible.
159          ContainerContextHandler handler = ContainerContextHandler.getInstance();
160          log.debug("ContainerContextHandler is available");
161          ScriptLogAccessor accessor = (ScriptLogAccessor)handler.getComponent("scriptLogAccessor");
162          log.debug("scriptLogAccessor is an available component in context.");
163          accessor.saveScriptLogInfo(logInfo);
164          log.info("ScriptLogInfo is saved for '" + name + "'");
165       }
166       catch (Exception e){
167          // Trace and return false.
168          log.error("Save of ScriptLogInfo is not possible, here's the message: " + e.getMessage());
169          return false;
170       }
171       return true;
172    }
173 }