Index: lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/DeployConfig.java =================================================================== diff -u -r60f8ad90ac03834ede1c4a45585f7f117d5844de -rf558d23e35d9602a349c466ad60c4f92dd3c45be --- lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/DeployConfig.java (.../DeployConfig.java) (revision 60f8ad90ac03834ede1c4a45585f7f117d5844de) +++ lams_tool_deploy/src/java/org/lamsfoundation/lams/tool/deploy/DeployConfig.java (.../DeployConfig.java) (revision f558d23e35d9602a349c466ad60c4f92dd3c45be) @@ -1,671 +1,281 @@ +/* +Copyright (C) 2005 LAMS Foundation (http://lamsfoundation.org) + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +USA + +http://www.gnu.org/licenses/gpl.txt +*/ + /* - *Copyright (C) 2005 LAMS Foundation (http://lamsfoundation.org) - * - *This program is free software; you can redistribute it and/or modify - *it under the terms of the GNU General Public License as published by - *the Free Software Foundation; either version 2 of the License, or - *(at your option) any later version. - * - *This program is distributed in the hope that it will be useful, - *but WITHOUT ANY WARRANTY; without even the implied warranty of - *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - *GNU General Public License for more details. - * - *You should have received a copy of the GNU General Public License - *along with this program; if not, write to the Free Software - *Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 - *USA - * - *http://www.gnu.org/licenses/gpl.txt + * Created on 24/11/2005 */ - package org.lamsfoundation.lams.tool.deploy; -import java.io.OutputStream; -import java.net.URL; -import java.util.ArrayList; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Writer; import java.util.List; -import org.apache.commons.configuration.ConfigurationException; -import org.apache.commons.configuration.PropertiesConfiguration; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.xml.sax.SAXException; + +import com.thoughtworks.xstream.XStream; +import com.thoughtworks.xstream.io.xml.DomDriver; + /** - * Encapsulates configuration data for the Deployer. - * @author chris + * @author mtruong + * + * Parent of the config related classes. + * + * Contains the shared constant definitions that is used by child classes + * DeployToolConfig and DeployLibraryConfig. + * Instantiates an XStream object which is used by its subclasses to specify + * aliases for class names to XML elements. See DeployToolConfig or + * DeployLibraryConfig for more details. + * + * All subclasses must implement the methods updateConfigurationProperties and validateProperties. + * + * Some variables are declared as transient so it won't be serialized into XML + * */ -public class DeployConfig -{ - public static final String TOOL_SIGNATURE_KEY = "toolSignature"; - public static final String TOOL_WEB_URI_KEY = "toolWebUri"; - public static final String TOOL_CONTEXT_KEY = "toolContext"; - public static final String LAMS_EAR_PATH_KEY = "lamsEarPath"; - public static final String TOOL_INSERT_SCRIPT_PATH_KEY = "toolInsertScriptPath"; - public static final String TOOL_LIBRARY_INSERT_SCRIPT_PATH_KEY = "toolLibraryInsertScriptPath"; - public static final String TOOL_ACTIVITY_INSERT_SCRIPT_PATH_KEY = "toolActivityInsertScriptPath"; - public static final String TOOL_TABLES_SCRIPT_PATH_KEY = "toolTablesScriptPath"; - public static final String TOOL_TABLES_DELETE_SCRIPT_PATH_KEY = "toolTablesDeleteScriptPath"; - public static final String DB_USERNAME_KEY = "dbUsername"; - public static final String DB_PASSWORD_KEY = "dbPassword"; - public static final String DB_DRIVER_CLASS_KEY = "dbDriverClass"; - public static final String DB_DRIVER_URL_KEY = "dbDriverUrl"; - public static final String DEPLOY_FILES_KEY = "deployFiles"; +public abstract class DeployConfig { - private PropertiesConfiguration props = null; - private String validationError = ""; + /* Common tags in the XML file */ + public static final String ROOT_ELEMENT = "Deploy"; + public static final String DB_USERNAME = "dbUsername"; + public static final String DB_PASSWORD = "dbPassword"; + public static final String DB_DRIVER_CLASS = "dbDriverClass"; + public static final String DB_DRIVER_URL = "dbDriverUrl"; + public static final String TOOL_SIGNATURE = "toolSignature"; + public static final String TOOL_ACTIVITY_INSERT_SCRIPT_PATH = "toolActivityInsertScriptPath"; - - /** - * Holds value of property toolSignature. - */ - private String toolSignature; - - /** - * Holds value of property toolWebUri. - */ - private String toolWebUri; + protected transient String validationError = ""; + protected transient XStream xstream; - /** - * Holds value of property toolContextRoot. - */ - private String toolContextRoot; /** - * Holds value of property lamsEarPath. - */ - private String lamsEarPath; - - /** - * Holds value of property toolInsertScriptPath. - */ - private String toolInsertScriptPath; - - /** - * Holds value of property toolLibraryInsertScriptPath. - */ - private String toolLibraryInsertScriptPath; - - /** - * Holds value of property toolActivityInsertScriptPath. - */ - private String toolActivityInsertScriptPath; - - /** - * Holds value of property toolTablesScriptPath. - */ - private String toolTablesScriptPath; - - /** - * Holds value of property toolTablesDeleteScriptPath. - */ - private String toolTablesDeleteScriptPath; - - /** - * Holds value of property deployFiles. - */ - private List deployFiles; - - /** - * Holds value of property dbUsername. - */ - private String dbUsername; - - /** - * Holds value of property dbPassword. - */ - private String dbPassword; - - /** * Holds value of property dbDriverClass. */ private String dbDriverClass; - + /** * Holds value of property dbDriverUrl. */ private String dbDriverUrl; - - /** Creates a new instance of DeployConfig - * @param deployPropertiesFilePath file to load - * @param validate validate the properties after loading. Set to false if - * intending to load some more properties later. - * @throws DeployException if a configuration error occurs or if the validation fails. - */ - public DeployConfig(final String deployPropertiesFilePath, final boolean validate) throws DeployException - { - updateConfiguration(deployPropertiesFilePath, validate); - } - /** Creates a new instance of DeployConfig - * @param deployPropertiesFilePath file to load - * @param validate validate the properties after loading. Set to false if - * intending to load some more properties later. - * @throws DeployException if a configuration error occurs or if the validation fails. - */ - public DeployConfig(final URL deployPropertiesFilePath, final boolean validate) throws DeployException - { - updateConfiguration(deployPropertiesFilePath, validate); - } - - /** Update the configuration. Only updates a value if a particular property is found. - * @param deployPropertiesFilePath: file to load - * @param validate validate the properties after loading. Set to false if - * intending to load some more properties later. - * @throws DeployException if a configuration error occurs or if the validation fails. - */ - public void updateConfiguration(final URL deployPropertiesFilePath, final boolean validate) - throws DeployException - { - try { - props = new PropertiesConfiguration(deployPropertiesFilePath); - updateConfigurationFromProps(validate); - } catch (ConfigurationException confex) { - throw new DeployException("Failed to load configuration", confex); - } - } - - /** Update the configuration. Only updates a value if a particular property is found. - * @param deployPropertiesFilePath: file to load - * @param validate validate the properties after loading. Set to false if - * intending to load some more properties later. - * @throws DeployException if a configuration error occurs or if the validation fails. - */ - public void updateConfiguration(final String deployPropertiesFilePath, final boolean validate) - throws DeployException - { - try { - props = new PropertiesConfiguration(deployPropertiesFilePath); - updateConfigurationFromProps(validate); - } catch (ConfigurationException confex) { - throw new DeployException("Failed to load configuration", confex); - } - } - - /** Update the current configuration from the props variable - which should - * have already been set by the calling function. - * @param validate validate the properties after loading. Set to false if - * intending to load some more properties later. - */ - private void updateConfigurationFromProps (final boolean validate) { - - String value = getProperty(TOOL_SIGNATURE_KEY); - if ( value != null ) { - toolSignature = value; - } - - value = getProperty(TOOL_WEB_URI_KEY); - if ( value != null ) { - toolWebUri = value; - } - - value = getProperty(TOOL_CONTEXT_KEY); - if ( value != null ) { - toolContextRoot = value; - } - - value = getProperty(LAMS_EAR_PATH_KEY); - if ( value != null ) { - lamsEarPath = value; - } - - value = getProperty(TOOL_INSERT_SCRIPT_PATH_KEY); - if ( value != null ) { - toolInsertScriptPath = value; - } - - value = getProperty(TOOL_LIBRARY_INSERT_SCRIPT_PATH_KEY); - if ( value != null ) { - toolLibraryInsertScriptPath = value; - } - - value = getProperty(TOOL_ACTIVITY_INSERT_SCRIPT_PATH_KEY); - if ( value != null ) { - toolActivityInsertScriptPath = value; - } - - value = getProperty(TOOL_TABLES_SCRIPT_PATH_KEY); - if ( value != null ) { - toolTablesScriptPath = value; - } - - value = getProperty(TOOL_TABLES_DELETE_SCRIPT_PATH_KEY); - if ( value != null ) { - toolTablesDeleteScriptPath = value; - } - - value = getProperty(DB_USERNAME_KEY); - if ( value != null ) { - dbUsername = value; - } - - value = getProperty(DB_PASSWORD_KEY); - if ( value != null ) { - dbPassword = value; - } - - value = getProperty(DB_DRIVER_CLASS_KEY); - if ( value != null ) { - dbDriverClass = value; - } - - value = getProperty(DB_DRIVER_URL_KEY); - if ( value != null ) { - dbDriverUrl = value; - } - - List listValue = getPropertyList(DEPLOY_FILES_KEY); - if ( listValue != null && listValue.size() > 0) { - deployFiles = listValue; - } - - if ( validate ) - validateProperties(); - } - - /** - * Write out the current settings to the supplied stream. - * - * @param outputStream - */ - public void writeProperties(OutputStream os) { - if ( os == null ) { - throw new DeployException("Invalid parameter - outputStream os is null"); - } - - PropertiesConfiguration newProps = new PropertiesConfiguration(); - newProps.setProperty(TOOL_SIGNATURE_KEY,toolSignature); - newProps.setProperty(TOOL_WEB_URI_KEY,toolWebUri); - newProps.setProperty(TOOL_CONTEXT_KEY,toolContextRoot); - newProps.setProperty(LAMS_EAR_PATH_KEY,lamsEarPath); - newProps.setProperty(TOOL_INSERT_SCRIPT_PATH_KEY,toolInsertScriptPath); - newProps.setProperty(TOOL_LIBRARY_INSERT_SCRIPT_PATH_KEY,toolLibraryInsertScriptPath); - newProps.setProperty(TOOL_ACTIVITY_INSERT_SCRIPT_PATH_KEY,toolActivityInsertScriptPath); - newProps.setProperty(TOOL_TABLES_SCRIPT_PATH_KEY,toolTablesScriptPath); - newProps.setProperty(TOOL_TABLES_DELETE_SCRIPT_PATH_KEY,toolTablesDeleteScriptPath); - newProps.setProperty(DB_USERNAME_KEY,dbUsername); - newProps.setProperty(DB_PASSWORD_KEY,dbPassword); - newProps.setProperty(DB_DRIVER_CLASS_KEY,dbDriverClass); - newProps.setProperty(DB_DRIVER_URL_KEY,dbDriverUrl); - newProps.setProperty(DEPLOY_FILES_KEY,deployFiles); - - try { - newProps.save(os); - } catch (ConfigurationException e) { - throw new DeployException("Unable to create generate properties: "+e.getMessage(), e); - } - } - /** - * The value to be used in the web-uri element of the application xml - * for the tool being deployed. - * @return Value of property toolWebAppUri. + * Holds value of property dbUsername. */ - public String getToolWebUri() - { + private String dbUsername; - return this.toolWebUri; - } - /** - * The value to be used in the context-root element of the application xml - * for the tool being deployed. - * @return Value of property toolContextRoot. + * Holds value of property dbPassword. */ - public String getToolContextRoot() - { - - return this.toolContextRoot; - } + private String dbPassword; - /** - * The path to the lasm ear. - * @return Value of property lamsEarPath. - */ - public String getLamsEarPath() - { - return this.lamsEarPath; - } - - /** - * The path to the sql script that inserts the lams_tool record. - * @return Value of property toolIinsertScriptPath. - */ - public String getToolInsertScriptPath() - - + public DeployConfig() { - - return this.toolInsertScriptPath; + this.validationError = ""; + xstream = new XStream(new DomDriver()); } /** - * The path to the sql script that inserts that lams_learning_library record. - * @return Value of property libraryInsertScriptPath. + * In general, this method should read in the xml configuration file, + * deserialise the xml string into an object, and copy the properties + * to the calling object. + * @param configFilePath The file location of the configuration file deploy.xml. + * @throws ParserConfigurationException + * @throws IOException + * @throws SAXException */ - public String getToolLibraryInsertScriptPath() + public abstract void updateConfigurationProperties(String configFilePath) throws ParserConfigurationException, IOException, SAXException; - { - - return this.toolLibraryInsertScriptPath; - } + /** + * Check that all the properties have a value, will throw an exception if a property does not have a value + * @throws DeployException + */ + public abstract void validateProperties() throws DeployException; - /** - * The path to the sql script that inserts the lams_learning_activity record. - * @return Value of property toolActivityInsertScriptPath. - */ - public String getToolActivityInsertScriptPath() - { - - return this.toolActivityInsertScriptPath; - } - /** - * The path to the sql script that creates the tools tables and inserts - * any required data. - * @return Value of property toolTablesScriptPath. + * Validates the value of a property. If the property does not exist, or + * the property doesn't have a value, it will return false. + * Otherwise return true. + * @param property + * @param key + * @return */ - public String getToolTablesScriptPath() - - { - - return this.toolTablesScriptPath; + protected boolean validateStringProperty(String property, String key) { + if ( ((property == null) || (property.length() < 1)) ) { + validationError = validationError + "Property "+key+" is missing or has no value."; + return false; + } + return true; } - + /** - * The list of files that need to be nmoved to the lams ear - * to deploy the tool. - * @return Value of property deployFiles. + * Will return false if the list property is empty or null. * + * @param property + * @param key + * @return */ - public List getDeployFiles() - { - - return this.deployFiles; + protected boolean validateListProperty(List property, String key) { + if ( ((property == null) || (property.isEmpty())) ) { + validationError = validationError + "Property "+key+" is missing or has no value."; + return false; + } + return true; } /** - * The username for the lams db. - * @return Value of property dbUsername. + * Reads in the deploy configuration xml file, and will return the xml + * as a string. + * @param configFilePath + * @return String. The xml contents as a String */ - public String getDbUsername() + protected String readFile(String configFilePath) { - - return this.dbUsername; + BufferedReader in; + StringBuffer tempStrBuf; + String tempStr; + try + { + in = new BufferedReader(new FileReader(configFilePath)); + tempStrBuf = new StringBuffer(); + + tempStr = in.readLine(); + while ( tempStr != null ) + { + tempStrBuf.append(tempStr); + tempStr = in.readLine(); + } + } + catch(IOException e) + { + throw new DeployException("An error has occurred while trying to read file " + configFilePath); + } + + return(tempStrBuf.toString()); } /** - * The password for the lams db. - * @return Value of property dbPassword. + * Takes in the xml string and deserialises it to an object + * @param xml The xml to deserialise + * @return the object constructed from the xml */ - public String getDbPassword() + protected Object deserialiseXML(String xml) { - - return this.dbPassword; - } + // System.out.println(xml); + return xstream.fromXML(xml); + } /** - * The class name of the JDBC driver to use. - * @return Value of property dbDriverClass. - */ - public String getDbDriverClass() - { - - return this.dbDriverClass; - } - - /** - * The URL for the JDBC driver to connect with. - * @return Value of property dbDriverURL. - */ - public String getDbDriverUrl() - - { - - return this.dbDriverUrl; - } - - /** * Set an arbitrary property. Note: these will probably have come through * ant, and that removes the case of the key, so ignore case. + * + * @param key + * @param value + * @throws DeployException */ protected void setProperty(String key, String value) throws DeployException { if ( key == null ) throw new DeployException("Invalid parameter: Key is null. "); + //System.out.println(key + " is: " + value); - if ( key.equalsIgnoreCase(TOOL_SIGNATURE_KEY) ) { - toolSignature = value; + if ( key.equalsIgnoreCase(DB_USERNAME) ) { + setDbUsername(value); } - if ( key.equalsIgnoreCase(TOOL_WEB_URI_KEY) ) { - toolWebUri = value; + if ( key.equalsIgnoreCase(DB_PASSWORD) ) { + setDbPassword(value); } - if ( key.equalsIgnoreCase(TOOL_CONTEXT_KEY) ) { - toolContextRoot = value; + if ( key.equalsIgnoreCase(DB_DRIVER_CLASS) ) { + setDbDriverClass(value); } - if ( key.equalsIgnoreCase(LAMS_EAR_PATH_KEY) ) { - lamsEarPath = value; - } - - if ( key.equalsIgnoreCase(TOOL_INSERT_SCRIPT_PATH_KEY) ) { - toolInsertScriptPath = value; - } - - if ( key.equalsIgnoreCase(TOOL_LIBRARY_INSERT_SCRIPT_PATH_KEY) ) { - toolLibraryInsertScriptPath = value; - } - - if ( key.equalsIgnoreCase(TOOL_ACTIVITY_INSERT_SCRIPT_PATH_KEY) ) { - toolActivityInsertScriptPath = value; - } - - if ( key.equalsIgnoreCase(TOOL_TABLES_SCRIPT_PATH_KEY) ) { - toolTablesScriptPath = value; - } - - if ( key.equalsIgnoreCase(TOOL_TABLES_DELETE_SCRIPT_PATH_KEY) ) { - toolTablesDeleteScriptPath = value; - } - - if ( key.equalsIgnoreCase(DB_USERNAME_KEY) ) { - dbUsername = value; - } - - if ( key.equalsIgnoreCase(DB_PASSWORD_KEY) ) { - dbPassword = value; - } - - if ( key.equalsIgnoreCase(DB_DRIVER_CLASS_KEY) ) { - dbDriverClass = value; - } - - if ( key.equalsIgnoreCase(DB_DRIVER_URL_KEY) ) { - dbDriverUrl = value; - } - - if ( key.equalsIgnoreCase(DEPLOY_FILES_KEY) ) { - deployFiles = convertList(value); - } - + if ( key.equalsIgnoreCase(DB_DRIVER_URL) ) { + setDbDriverUrl(value); + } } + /** - * @param dbDriverUrl The dbDriverUrl to set. + * Serialize an object to the given Writer as pretty-printed XML. + * @param writer */ - protected void setDbDriverUrl(String dbDriverUrl) { - this.dbDriverUrl = dbDriverUrl; - } + protected void writePropertiesToFile(Writer writer) + { + xstream.toXML(this, writer); + } + /** - * @param dbPassword The dbPassword to set. + * @return Returns the dbDriverClass. */ - protected void setDbPassword(String dbPassword) { - this.dbPassword = dbPassword; + public String getDbDriverClass() { + return dbDriverClass; } /** - * @param dbUsername The dbUsername to set. + * @param dbDriverClass The dbDriverClass to set. */ - protected void setDbUsername(String dbUsername) { - this.dbUsername = dbUsername; + public void setDbDriverClass(String dbDriverClass) { + this.dbDriverClass = dbDriverClass; } /** - * @param deployFiles The deployFiles to set. + * @return Returns the dbDriverUrl. */ - protected void setDeployFiles(List deployFiles) { - this.deployFiles = deployFiles; + public String getDbDriverUrl() { + return dbDriverUrl; } /** - * @param lamsEarPath The lamsEarPath to set. + * @param dbDriverUrl The dbDriverUrl to set. */ - protected void setLamsEarPath(String lamsEarPath) { - this.lamsEarPath = lamsEarPath; + public void setDbDriverUrl(String dbDriverUrl) { + this.dbDriverUrl = dbDriverUrl; } /** - * @param toolActivityInsertScriptPath The toolActivityInsertScriptPath to set. + * @return Returns the dbPassword. */ - protected void setToolActivityInsertScriptPath( - String toolActivityInsertScriptPath) { - this.toolActivityInsertScriptPath = toolActivityInsertScriptPath; + public String getDbPassword() { + return dbPassword; } /** - * @param toolContextRoot The toolContextRoot to set. + * @param dbPassword The dbPassword to set. */ - protected void setToolContextRoot(String toolContextRoot) { - this.toolContextRoot = toolContextRoot; + public void setDbPassword(String dbPassword) { + this.dbPassword = dbPassword; } /** - * @param toolInsertScriptPath The toolInsertScriptPath to set. + * @return Returns the dbUsername. */ - protected void setToolInsertScriptPath(String toolInsertScriptPath) { - this.toolInsertScriptPath = toolInsertScriptPath; + public String getDbUsername() { + return dbUsername; } /** - * @param toolLibraryInsertScriptPath The toolLibraryInsertScriptPath to set. + * @param dbUsername The dbUsername to set. */ - protected void setToolLibraryInsertScriptPath( - String toolLibraryInsertScriptPath) { - this.toolLibraryInsertScriptPath = toolLibraryInsertScriptPath; + public void setDbUsername(String dbUsername) { + this.dbUsername = dbUsername; } - /** - * @param toolTablesScriptPath The toolTablesScriptPath to set. - */ - protected void setToolTablesScriptPath(String toolTablesScriptPath) { - this.toolTablesScriptPath = toolTablesScriptPath; - } - /** - * @param toolWebUri The toolWebUri to set. - */ - protected void setToolWebUri(String toolWebUri) { - this.toolWebUri = toolWebUri; - } - - /** - * Tries to load the property for the given key from the props object. - * @param key the key to the desired property. - * @return String the property value or null if not found in props. - */ - protected String getProperty(final String key) throws DeployException - { - String property = props.getString(key); - if ( property !=null && property.length() > 0 ) { - return property; - } else { - return null; - } - } - - /** - * Returns a list of property strings for the given key from the props object. - * @param key identifies the property to locate - * @return List of (String) properties, null if not found. - */ - protected List getPropertyList(final String key) throws DeployException - { - List propsList = props.getList(key); - if ((propsList != null) && !propsList.isEmpty()) - { - return propsList; - } else { - return null; - } - } - - /** - * Converts a String to a List. Entries should be comma separated. - * @param Input string containing entries. - * @return List of (String) properties, null if not found. - */ - protected List convertList(String input) throws DeployException - { - String[] strings = input.split(","); - List list = new ArrayList(strings.length); - for ( int i=0; i