[Batchserver-cvs] batchserver/src/org/jmonks/batchserver/framework/config/xml XMLJobConfig.java,1.1,
Brought to you by:
suresh_pragada
From: Suresh <sur...@us...> - 2006-03-07 23:33:29
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/framework/config/xml In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20701 Modified Files: XMLJobConfig.java XMLJobConfigFactory.java Log Message: no message Index: XMLJobConfig.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/framework/config/xml/XMLJobConfig.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** XMLJobConfig.java 3 Mar 2006 04:19:57 -0000 1.1 --- XMLJobConfig.java 7 Mar 2006 23:33:23 -0000 1.2 *************** *** 15,20 **** /** ! * ! * @author Suresh Pragada */ public class XMLJobConfig extends JobConfig --- 15,22 ---- /** ! * XMLJobConfig provides the implementation of JobConfig ! * @author Suresh Pragada ! * @since 1.0 ! * @version 1.0 */ public class XMLJobConfig extends JobConfig Index: XMLJobConfigFactory.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/framework/config/xml/XMLJobConfigFactory.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** XMLJobConfigFactory.java 3 Mar 2006 04:19:57 -0000 1.1 --- XMLJobConfigFactory.java 7 Mar 2006 23:33:23 -0000 1.2 *************** *** 11,37 **** package org.jmonks.batchserver.framework.config.xml; import java.util.Map; import org.jmonks.batchserver.framework.config.JobConfig; import org.jmonks.batchserver.framework.config.JobConfigFactory; /** * ! * @author Suresh Pragada */ public class XMLJobConfigFactory extends JobConfigFactory { ! private Map mConfigFactoryProps=null; ! /** Creates a new instance of XMLJobConfigFactory */ public XMLJobConfigFactory(Map configFactoryProps) { } ! public JobConfig getJobConfig(String jobName) { /** ! * Look up XML configuration for jobName and pass that element. */ ! return new XMLJobConfig(null); } } --- 11,236 ---- package org.jmonks.batchserver.framework.config.xml; + import java.io.File; + import java.io.FileInputStream; + import java.io.FileNotFoundException; + import java.io.IOException; + import java.io.InputStream; import java.util.Map; + import javax.xml.parsers.DocumentBuilder; + import javax.xml.parsers.DocumentBuilderFactory; + import javax.xml.parsers.ParserConfigurationException; + import org.jmonks.batchserver.framework.common.ErrorCode; + import org.jmonks.batchserver.framework.config.ConfigurationException; import org.jmonks.batchserver.framework.config.JobConfig; import org.jmonks.batchserver.framework.config.JobConfigFactory; + import org.w3c.dom.Document; + import org.w3c.dom.Element; + import org.w3c.dom.NodeList; + import org.xml.sax.SAXException; /** + * <p> + * XMLJobConfigFactory reads the job configuration from XML file to create the + * configuration objects needed by the job. This factory looks for few properties + * in the config properties map to identify the source of the XML file. The source of + * the XML file can be defined as absolute path on the file system or resource on the + * class path. + * </p> + * <p> + * Following are the two properties, this factory looks to find the source. If it find + * these two it will give priority to the physical absolute path location. + * <table> + * <tr><td><b>property key</b></td><td><b>property value</b></td></tr> + * <tr><td>job-config-file-absolute-location</td><td>/batchserver/config/batch-config.xml</td></tr> + * <tr><td>job-config-file-classpath-location</td><td>batch-config.xml</td></tr> + * </table> + * These properties can be configured using <property> element in the <job-config-factory-config> element + * in the framework configuration file. + * </p> * ! * @author Suresh Pragada ! * @version 1.0 ! * @since 1.0 */ public class XMLJobConfigFactory extends JobConfigFactory { ! /** ! * Constant defines the property name of config file absolute location. ! */ ! private static final String PROPERTY_JOB_CONFIG_FILE_ABSOLUTE_LOCATION = "job-config-file-absolute-location"; ! /** ! * Constant defines the property name of config file classpath location. ! */ ! private static final String PROPERTY_JOB_CONFIG_FILE_CLASSPATH_LOCATION = "job-config-file-classpath-location"; ! /** ! * Element name that identifies the job configuration. ! */ ! private static final String JOB_CONFIG_ELEMENT_NAME = "job-config"; ! /** ! * Attribute name that identifies the job name. ! */ ! private static final String JOB_NAME_ATTRIBUTE_NAME = "job-name"; ! /** ! * Property map holds the properites required by this factory. ! */ ! private Map configFactoryProps=null; ! ! /** ! * batch-config element in the XML configuration file. ! */ ! private Element batchConfigElement=null; ! ! /** ! * This constructor initializes the factory by accepting the required properties ! * map as a parameter to the constructor. If it doesnt find any required properties ! * to initialize the factory, throws ConfigurationException with the appropriate error code. ! * ! * @param configFactoryProps Map consists of all the properties defined for this factory. ! * ! * @throws ConfigurationException If required properties are missing. ! */ public XMLJobConfigFactory(Map configFactoryProps) { + this.configFactoryProps=configFactoryProps; + + String absoluteLocation=(String)this.configFactoryProps.get(XMLJobConfigFactory.PROPERTY_JOB_CONFIG_FILE_ABSOLUTE_LOCATION); + String classpathLocation=(String)this.configFactoryProps.get(XMLJobConfigFactory.PROPERTY_JOB_CONFIG_FILE_CLASSPATH_LOCATION); + if(absoluteLocation==null && classpathLocation==null) + throw new ConfigurationException(ErrorCode.XML_JOB_CONFIG_FACTORY_PROPERTIES_MISSING); + else if(absoluteLocation!=null) + { + /** + * Look for the absolute location property and load the stream. + * If file is not found or the given entry is not a file throw configuration exception. + */ + try + { + File jobConfigFile=new File(absoluteLocation); + if(jobConfigFile.exists() && jobConfigFile.isFile()) + { + InputStream inputStream=new FileInputStream(jobConfigFile); + if(!this.createBatchConfigElement(inputStream)) + throw new ConfigurationException(ErrorCode.NO_BATCH_CONFIG_ELEMENT_IN_XML_JOB_CONFIGURATION); + } + else + throw new ConfigurationException(ErrorCode.XML_JOB_CONFIG_FACTORY_INVALID_ABS_LOCATION); + } + catch(FileNotFoundException exception) + { + exception.printStackTrace(); + throw new ConfigurationException(ErrorCode.XML_JOB_CONFIG_FACTORY_INVALID_ABS_LOCATION); + } + } + else if(classpathLocation!=null) + { + /** + * Look for the classpath location property and load the file using classloader. + * If we dont find this file on classpath, throw configuration exception. + */ + InputStream inputStream=XMLJobConfigFactory.class.getClassLoader().getResourceAsStream(classpathLocation); + if(inputStream==null) + throw new ConfigurationException(ErrorCode.XML_JOB_CONFIG_FACTORY_INVALID_CLASSPATH_LOCATION); + else + { + if(!this.createBatchConfigElement(inputStream)) + throw new ConfigurationException(ErrorCode.NO_BATCH_CONFIG_ELEMENT_IN_XML_JOB_CONFIGURATION); + } + } } ! ! /** ! * Returns the requested job configuration as JobConfig object. The requested job ! * name should be passed as a parameter. If it doesnt find the configuration of ! * the requested jobName in the job configuration file, it returns null. ! * ! * @param jobName Name of the job. ! * ! * @return Returns the job config object if found, null, otherwise. ! * ! * @throws IllegalArgumentException If jobName passed as parameter is null. ! * @throws IllegalStateException If factory is not initialized properly. ! */ public JobConfig getJobConfig(String jobName) { + JobConfig jobConfig=null; /** ! * Look for all the job-config elements and pass the element whose job-name ! * attribute matches the input jobName parameter to the XMLJobConfig. */ ! if(this.batchConfigElement==null) ! throw new IllegalStateException("Factory is not initialized properly. Batch config element is null."); ! ! if(jobName==null) ! throw new IllegalArgumentException("jobName cannot be null."); ! ! NodeList jobConfigNodeList=this.batchConfigElement.getElementsByTagName(XMLJobConfigFactory.JOB_CONFIG_ELEMENT_NAME); ! for(int i=0;i<jobConfigNodeList.getLength();i++) ! { ! Element jobConfigElement=(Element)jobConfigNodeList.item(i); ! String elementJobName=jobConfigElement.getAttribute(XMLJobConfigFactory.JOB_NAME_ATTRIBUTE_NAME); ! if(jobName.equals(elementJobName)) ! { ! jobConfig=new XMLJobConfig(jobConfigElement); ! break; ! } ! } ! return jobConfig; } + + /** + * Creates the DOM Element represents the batch-config element from the given stream + * and assigns it to the class instance varaibale batchConfigElement. + * + * @param jobConfigStream InputStream of XML job configuration file. + * @return Returns true, if it could load the element from the given inputstream, false, otherwise. + */ + private boolean createBatchConfigElement(InputStream jobConfigStream) + { + boolean created=false; + + try + { + DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance(); + documentBuilderFactory.setValidating(true); + DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder(); + Document document=documentBuilder.parse(jobConfigStream); + this.batchConfigElement=document.getDocumentElement(); + + if(this.batchConfigElement!=null) + created=true; + } + catch(ParserConfigurationException exception) + { + exception.printStackTrace(); + } + catch(SAXException exception) + { + exception.printStackTrace(); + } + catch(IOException exception) + { + exception.printStackTrace(); + } + + return created; + } + + /** + * <p> + * Returns the string representation of XMLJobConfigFactory class in the format + * <br> {XMLJobConfigFactory [configProperties = value] [element = value]} + * </p> + * + * @return Returns the string representation of XMLJobConfigFactory. + */ + public String toString() + { + StringBuffer stringValue=new StringBuffer("{XMLJobConfigFactory "); + stringValue.append("[configProperties = " + this.configFactoryProps + "]"); + stringValue.append("[element = " + this.batchConfigElement + "]"); + stringValue.append("}"); + return stringValue.toString(); + } + } |