[Batchserver-cvs] batchserver/src/org/jmonks/batch/framework/config/xml XMLBasicJobControllerConfi
Brought to you by:
suresh_pragada
From: Suresh <sur...@us...> - 2006-09-15 20:06:23
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batch/framework/config/xml In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv19878 Added Files: XMLBasicJobControllerConfig.java XMLJobConfig.java XMLJobConfigFactory.java XMLJobControllerConfig.java XMLJobLoggingConfig.java XMLPoolJobControllerConfig.java Log Message: no message --- NEW FILE: XMLJobConfig.java --- /* * XMLJobConfig.java * * Created on March 2, 2006, 11:51 AM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package org.jmonks.batch.framework.config.xml; import org.apache.log4j.Logger; import org.jmonks.batch.framework.config.ConfigurationException; import org.jmonks.batch.framework.config.JobConfig; import org.w3c.dom.Element; import org.w3c.dom.NodeList; /** * <p> * XMLJobConfig provides the implementation of JobConfig by reading the * job configuration from XML DOM element <job-config> configured in the * job configuration file. This reads and represents the following piece of XML * block from job configuration file. * <br><br> * <pre> * <job-config job-name="process_file_xyz"> * <job-controller job-controller-class-name="org.jmonks.batch.framework.controller.basic.BasicJobController"> * <basic-job-processor basic-job-processor-class-name="com.mycompany.batch.processfilexyz.XyzProcessor" thread-count="1"> * <property key="basic-job-processor-config1">processor-value1</property> * </basic-job-processor> * <property key="basic-job-controller-config1">config-value1</property> * </job-controller> * <job-logging-config> * <logger-config logger-name="com.mycompany.batch" logger-level="ERROR"/> * </job-logging-config> * </job-config> * </pre> * </p> * * @author Suresh Pragada * @since 1.0 * @version 1.0 */ public class XMLJobConfig extends JobConfig { /** * Element name that identifies the job configuration which is <code>job-config</code>. */ public static final String JOB_CONFIG_ELEMENT_NAME = "job-config"; /** * Attribute name that identifies the job name which is <code>job-name</code>. */ public static final String JOB_NAME_ATTRIBUTE_NAME = "job-name"; /** * Attribute name that identifies the job status which is <code>job-status</code>. */ public static final String JOB_STATUS_ATTRIBUTE_NAME = "job-status"; private static Logger logger=Logger.getLogger(XMLJobConfig.class); /** * Loads the job name and controller configuration into the job configuration object. * * @param jobConfigElement XML DOM Element that represents the <job-confg> * element in batch job configuration file. * * @throws ConfigurationException If more than one job controllers are found in job configuration. */ XMLJobConfig(Element jobConfigElement) { logger.trace("Entering Constructor"); this.jobName=jobConfigElement.getAttribute(XMLJobConfig.JOB_NAME_ATTRIBUTE_NAME); logger.info("Job name = " + this.jobName); String jobStatusInStr=jobConfigElement.getAttribute(XMLJobConfig.JOB_STATUS_ATTRIBUTE_NAME); if(jobStatusInStr.equalsIgnoreCase("active")) { this.jobStatus=true; } else { this.jobStatus=false; } logger.info("Job status = " + this.jobStatus); NodeList jobLoggingConfigNodeList=jobConfigElement.getElementsByTagName(XMLJobLoggingConfig.JOB_LOGGING_CONFIG_ELEMENT_NAME); if(jobLoggingConfigNodeList.getLength()==1) { Element jobLoggingConfigElement=(Element)jobLoggingConfigNodeList.item(0); logger.trace("Creating the XML Job logging configuration object"); this.jobLoggingConfig=new XMLJobLoggingConfig(jobLoggingConfigElement); logger.info("Job logging configuration = " + this.jobLoggingConfig); } else throw new ConfigurationException(ConfigurationException.JOB_CONFIG, "Found " + jobLoggingConfigNodeList.getLength() + XMLJobLoggingConfig.JOB_LOGGING_CONFIG_ELEMENT_NAME + " element(s) in configuration of job " + jobName + "."); NodeList controllerConfigNodeList=jobConfigElement.getElementsByTagName(XMLJobControllerConfig.JOB_CONTROLLER_ELEMENT_NAME); if(controllerConfigNodeList.getLength()==1) { Element controllerConfigElement=(Element)controllerConfigNodeList.item(0); logger.trace("Requesting controller config factory to get the controller configuration object"); this.jobControllerConfig=XMLJobControllerConfig.getJobControllerConfig(controllerConfigElement); logger.info("Job controller configuration = " + this.jobControllerConfig); } else throw new ConfigurationException(ConfigurationException.JOB_CONFIG, "Found " + controllerConfigNodeList.getLength() + XMLJobControllerConfig.JOB_CONTROLLER_ELEMENT_NAME + " element(s) in configuration of job " + jobName + "."); logger.trace("Exiting Constructor"); } } --- NEW FILE: XMLJobLoggingConfig.java --- /* * XMLJobLoggingConfig.java * * Created on March 13, 2006, 7:27 PM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package org.jmonks.batch.framework.config.xml; import org.apache.log4j.Logger; import org.jmonks.batch.framework.config.JobLoggingConfig; import org.w3c.dom.Element; import org.w3c.dom.NodeList; /** * Reads the job logging configuration from the XML job configuration file. * * @author Suresh Pragada * @version 1.0 * @since 1.0 */ public class XMLJobLoggingConfig extends JobLoggingConfig { /** * Element name represents the job logging config which is <code>job-logging-config</code>. */ public static final String JOB_LOGGING_CONFIG_ELEMENT_NAME = "job-logging-config"; /** * Element name represents the job logger config which is <code>job-logger-config</code>. */ private static final String JOB_LOGGER_CONFIG_ELEMENT_NAME = "job-logger-config"; /** * Atrribute name represents the logger name in job logger config which is <code>logger-name</code>. */ private static final String LOGGER_NAME_ATTRIBUTE_NAME = "logger-name"; /** * Atrribute name represents the logger level in job logger config which is <code>logger-level</code>. */ private static final String LOGGER_LEVEL_ATTRIBUTE_NAME = "logger-level"; private static Logger logger=Logger.getLogger(XMLJobLoggingConfig.class); /** * <p> * Loads the job logging config object from the input element represents * job-logging-config element. This element represents the following XML block. * <br><br> * <pre> * <job-logging-config> * <job-logger-config logger-name="com.mycompany.batch.xyz" logger-level="DEBUG"/> * <job-logger-config logger-name="com.mycompany.batch.abc.processor" logger-level="ERROR"/> * </job-logging-config> * </pre> * </p> * * @param jobLoggingConfigElement XML DOM Element represents the job-logging-config element. */ public XMLJobLoggingConfig(Element jobLoggingConfigElement) { logger.trace("Entering constructor"); NodeList jobLoggerConfigNodeList=jobLoggingConfigElement.getElementsByTagName(XMLJobLoggingConfig.JOB_LOGGER_CONFIG_ELEMENT_NAME); for(int i=0;i<jobLoggerConfigNodeList.getLength();i++) { Element jobLoggerConfigElement=(Element)jobLoggerConfigNodeList.item(i); String loggerName=jobLoggerConfigElement.getAttribute(XMLJobLoggingConfig.LOGGER_NAME_ATTRIBUTE_NAME); String loggerLevel=jobLoggerConfigElement.getAttribute(XMLJobLoggingConfig.LOGGER_LEVEL_ATTRIBUTE_NAME); logger.trace("Adding the logger information = " + loggerName + " " + loggerLevel); this.addLogger(loggerName,loggerLevel); } logger.trace("Exiting constructor"); } } --- NEW FILE: XMLPoolJobControllerConfig.java --- /* * XMLPoolJobControllerConfig.java * * Created on March 2, 2006, 1:33 PM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package org.jmonks.batch.framework.config.xml; import org.apache.log4j.Logger; import org.jmonks.batch.framework.util.FrameworkUtil; import org.jmonks.batch.framework.config.ConfigurationException; import org.jmonks.batch.framework.config.PoolJobControllerConfig; import org.w3c.dom.Element; import org.w3c.dom.NodeList; /** * <p> * XMLPoolJobControllerConfig loads the controller configuration from XML Job controller * configuration. This reads the following XML block and loads the XMLPoolJobControllerConfig. * <br><br> * <pre> * <job-controller job-controller-class-name="org.jmonks.batch.framework.controller.pool.PoolJobController"> * <pool-job-loader pool-job-loader-class-name="com.mycompany.batch.processfileabc.AbcJobLoader"> * <property key="pool-job-loader-key1">loader-value1</property> * </pool-job-loader> * <pool-job-processor pool-job-processor-class-name="com.mycompany.batch.processfileabc.AbcJobProcessor" thread-count="1"> * <property key="pool-job-processor-key1">processor-value1</property> * </pool-job-processor> * <job-pool job-pool-class-name="org.jmonks.batch.framework.controller.pool.DefaultJobPool"> * <property key="job-pool-size">50000</property> * </job-pool> * <property key="pool-job-controller-config1">config-value1</property> * </job-controller> * </pre> * </p> * @author Suresh Pragada * @version 1.0 * @since 1.0 */ public class XMLPoolJobControllerConfig extends PoolJobControllerConfig { /** * XML Element name defines the pool job loader configuration which is <code>pool-job-loader</code>. */ private static final String POOL_JOB_LOADER_ELEMENT_NAME = "pool-job-loader"; /** * XML Attribute name defines the pool job loader class name which is <code>pool-job-loader-class-name</code>. */ private static final String POOL_JOB_LOADER_CLASS_ATTRIBUTE_NAME = "pool-job-loader-class-name"; /** * XML Element name defines the job pool configuration which is <code>job-pool</code>. */ private static final String POOL_JOB_POOL_ELEMENT_NAME = "job-pool"; /** * XML Attribute name defines the job pool class name which is <code>job-pool-class-name</code>. */ private static final String POOL_JOB_POOL_CLASS_ATTRIBUTE_NAME = "job-pool-class-name"; /** * XML Element name defines the pool job processor configuration which is <code>pool-job-processor</code>. */ private static final String POOL_JOB_PROCESSOR_ELEMENT_NAME = "pool-job-processor"; /** * XML Attribute name defines the pool job processor class name which is <code>pool-job-processor-class-name</code>. */ private static final String POOL_JOB_PROCESSOR_CLASS_ATTRIBUTE_NAME = "pool-job-processor-class-name"; /** * XML Attribute name defines the thread count of the job processor which is <code>thread-count</code>. */ private static final String THREAD_COUNT_ATTRIBUTE_NAME = "thread-count"; private static Logger logger=Logger.getLogger(XMLPoolJobControllerConfig.class); /** * Loads the XMLPoolJobControllerConfig from XML DOM Element <job-controller> * * @param controllerConfigElement XML DOM Element represents the <job-controller> element. * * @throws ConfigurationException If controller class name not defined or required controller properties missing. */ public XMLPoolJobControllerConfig(Element controllerConfigElement) { logger.trace("Entering Constructor"); this.jobControllerClassName=controllerConfigElement.getAttribute(XMLJobControllerConfig.JOB_CONTROLLER_CLASS_ATTRIBUTE_NAME); if(this.jobControllerClassName==null) throw new ConfigurationException(ConfigurationException.JOB_CONTROLLER_CONFIG, XMLJobControllerConfig.JOB_CONTROLLER_CLASS_ATTRIBUTE_NAME + " attribute value cannot be null."); FrameworkUtil.loadPropertiesFromElementToMap(controllerConfigElement, this.jobControllerConfigProps); /** * Load pool job loader configuration. */ logger.trace("Started reading the pool job loader configuration"); NodeList xmlPoolJobLoaderNodeList=controllerConfigElement.getElementsByTagName(XMLPoolJobControllerConfig.POOL_JOB_LOADER_ELEMENT_NAME); if(xmlPoolJobLoaderNodeList.getLength()==1) { Element xmlPoolJobLoaderElement=(Element)xmlPoolJobLoaderNodeList.item(0); this.poolJobLoaderClassName=xmlPoolJobLoaderElement.getAttribute(XMLPoolJobControllerConfig.POOL_JOB_LOADER_CLASS_ATTRIBUTE_NAME); if(this.poolJobLoaderClassName==null) throw new ConfigurationException(ConfigurationException.JOB_CONTROLLER_CONFIG, XMLPoolJobControllerConfig.POOL_JOB_LOADER_CLASS_ATTRIBUTE_NAME + " attribute value cannot be null."); else { FrameworkUtil.loadPropertiesFromElementToMap(xmlPoolJobLoaderElement, this.poolJobLoaderConfigProps); } } else throw new ConfigurationException(ConfigurationException.JOB_CONTROLLER_CONFIG, "Found " + xmlPoolJobLoaderNodeList.getLength() + " element(s) in the pool job controller configuration."); logger.trace("Finished reading the pool job loader configuration"); /** * Load pool job procesor configuration. */ logger.trace("Started reading the pool job processor configuration"); NodeList xmlPoolJobProcessorNodeList=controllerConfigElement.getElementsByTagName(XMLPoolJobControllerConfig.POOL_JOB_PROCESSOR_ELEMENT_NAME); if(xmlPoolJobProcessorNodeList.getLength()==1) { Element xmlPoolJobProcessorElement=(Element)xmlPoolJobProcessorNodeList.item(0); this.poolJobProcessorClassName=xmlPoolJobProcessorElement.getAttribute(XMLPoolJobControllerConfig.POOL_JOB_PROCESSOR_CLASS_ATTRIBUTE_NAME); if(this.poolJobProcessorClassName==null) throw new ConfigurationException(ConfigurationException.JOB_CONTROLLER_CONFIG, XMLPoolJobControllerConfig.POOL_JOB_PROCESSOR_CLASS_ATTRIBUTE_NAME + " attribute value cannot be null."); else { this.poolJobProcessorThreadCount=Integer.parseInt(xmlPoolJobProcessorElement.getAttribute(XMLPoolJobControllerConfig.THREAD_COUNT_ATTRIBUTE_NAME)); FrameworkUtil.loadPropertiesFromElementToMap(xmlPoolJobProcessorElement, this.poolJobProcessorConfigProps); } } else throw new ConfigurationException(ConfigurationException.JOB_CONTROLLER_CONFIG, "Found " + xmlPoolJobProcessorNodeList.getLength() + " element(s) in the pool job controller configuration."); logger.trace("Finished reading the pool job processor configuration"); /** * Load job pool configuration. */ logger.trace("Started reading the job pool configuration"); NodeList xmlJobPoolNodeList=controllerConfigElement.getElementsByTagName(XMLPoolJobControllerConfig.POOL_JOB_POOL_ELEMENT_NAME); if(xmlJobPoolNodeList.getLength()==1) { Element xmlJobPoolElement=(Element)xmlJobPoolNodeList.item(0); this.poolClassName=xmlJobPoolElement.getAttribute(XMLPoolJobControllerConfig.POOL_JOB_POOL_CLASS_ATTRIBUTE_NAME); if(this.poolClassName==null) throw new ConfigurationException(ConfigurationException.JOB_CONTROLLER_CONFIG, XMLPoolJobControllerConfig.POOL_JOB_POOL_CLASS_ATTRIBUTE_NAME + " attribute value cannot be null."); else { FrameworkUtil.loadPropertiesFromElementToMap(xmlJobPoolElement, this.poolConfigProps); } } else throw new ConfigurationException(ConfigurationException.JOB_CONTROLLER_CONFIG, "Found " + xmlJobPoolNodeList.getLength() + " element(s) in the pool job controller configuration."); logger.trace("Finished reading the job pool configuration"); logger.trace("Exiting Constructor"); } } --- NEW FILE: XMLJobControllerConfig.java --- /* * XMLJobControllerConfig.java * * Created on March 2, 2006, 1:20 PM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package org.jmonks.batch.framework.config.xml; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import org.apache.log4j.Logger; import org.jmonks.batch.framework.config.ConfigurationException; import org.jmonks.batch.framework.config.FrameworkConfig; import org.jmonks.batch.framework.config.JobControllerConfig; import org.w3c.dom.Element; /** * XMLJobControllerConfig responsible to build the controller specific * JobControllerConfig objects from the given XML. Look at {@link #getJobControllerConfig(Element)} * to understand on how this is going to get the correct job controller config instance. * * @author Suresh Pragada * @version 1.0 * @since 1.0 */ public abstract class XMLJobControllerConfig extends JobControllerConfig { /** * Element name that identifies the job controller configuration which is <code>job-controller</code>. */ public static final String JOB_CONTROLLER_ELEMENT_NAME = "job-controller"; /** * Attribute name that identifies the job controller class name which is <code>job-controller-class-name</code>. */ public static final String JOB_CONTROLLER_CLASS_ATTRIBUTE_NAME = "job-controller-class-name"; /** * Element name that identifies the job controller configuration class name which is <code>xml-factory-config-class-name</code>. */ public static final String JOB_CONTROLLER_CONFIG_FACTORY_CLASS_ATTRIBUTE_NAME = "xml-factory-config-class-name"; private static Logger logger=Logger.getLogger(XMLJobControllerConfig.class); /** * <p> * Factory method creates and returns the controller specific controller config object * from the following XML block from the job configuration file. This looks for the * controller class name and tries to find specific controller config object for this controller * and defined for the XML factory in framework configuration file. * <br><br> * <pre> * <job-controller job-controller-class-name="org.jmonks.batch.framework.controller.basic.BasicJobController"> * <property key="basic-job-controller-config1">config-value1</property> * </job-controller> * </pre> * </p> * * @return Returns the controller specific controller config object. * * @throws ConfigurationException If controller config class name is not defined or controller specific config object cannot be found * in framework controller configuration. */ public static JobControllerConfig getJobControllerConfig(Element controllerConfigElement) { logger.trace("Entering getJobControllerConfig"); JobControllerConfig controllerConfig=null; String controllerClassName=controllerConfigElement.getAttribute(XMLJobControllerConfig.JOB_CONTROLLER_CLASS_ATTRIBUTE_NAME); logger.debug("Controller class name = " + controllerClassName); if(controllerClassName==null) throw new ConfigurationException(ConfigurationException.JOB_CONTROLLER_CONFIG, XMLJobControllerConfig.JOB_CONTROLLER_CLASS_ATTRIBUTE_NAME + " attribute value cannot be null."); else { try { String controllerConfigClassName=FrameworkConfig.getInstance().getJobControllerConfig().getConfigClassName(controllerClassName, XMLJobControllerConfig.JOB_CONTROLLER_CONFIG_FACTORY_CLASS_ATTRIBUTE_NAME); logger.debug("Controller configuration class name = " + controllerConfigClassName); if(controllerConfigClassName==null) throw new ConfigurationException(ConfigurationException.JOB_CONTROLLER_CONFIG, "Could not found the controller config class for the controller type " + controllerClassName + " for the factory " + XMLJobControllerConfig.JOB_CONTROLLER_CONFIG_FACTORY_CLASS_ATTRIBUTE_NAME + "."); else { Class configClass=Class.forName(controllerConfigClassName); Constructor constructor=configClass.getConstructor(new Class[]{Element.class}); controllerConfig=(JobControllerConfig)constructor.newInstance(new Object[]{controllerConfigElement}); logger.debug("Got the controller configuration : " + controllerConfig); } } catch(InstantiationException exception) { exception.printStackTrace(); logger.error(exception.getMessage(), exception); throw new ConfigurationException(ConfigurationException.JOB_CONTROLLER_CONFIG, exception.getMessage()); } catch(NoSuchMethodException exception) { exception.printStackTrace(); logger.error(exception.getMessage(), exception); throw new ConfigurationException(ConfigurationException.JOB_CONTROLLER_CONFIG, exception.getMessage()); } catch(ClassNotFoundException exception) { exception.printStackTrace(); logger.error(exception.getMessage(), exception); throw new ConfigurationException(ConfigurationException.JOB_CONTROLLER_CONFIG, exception.getMessage()); } catch(IllegalAccessException exception) { exception.printStackTrace(); logger.error(exception.getMessage(), exception); throw new ConfigurationException(ConfigurationException.JOB_CONTROLLER_CONFIG, exception.getMessage()); } catch(InvocationTargetException exception) { exception.printStackTrace(); logger.error(exception.getMessage(), exception); throw new ConfigurationException(ConfigurationException.JOB_CONTROLLER_CONFIG, exception.getMessage()); } } logger.trace("Exiting getJobControllerConfig"); return controllerConfig; } } --- NEW FILE: XMLJobConfigFactory.java --- /* * XMLJobConfigFactory.java * * Created on March 2, 2006, 12:00 PM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package org.jmonks.batch.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.HashMap; import java.util.Map; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.apache.log4j.Logger; import org.jmonks.batch.framework.config.ConfigurationException; import org.jmonks.batch.framework.config.JobConfig; import org.jmonks.batch.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 factory 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> attribute 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 which is <code>job-config-file-absolute-location</code>. */ 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 which is <code>job-config-file-classpath-location</code>. */ private static final String PROPERTY_JOB_CONFIG_FILE_CLASSPATH_LOCATION = "job-config-file-classpath-location"; /** * 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; private static Logger logger=Logger.getLogger(XMLJobConfigFactory.class); /** *<p> * <i> * Do not use this constructor to instantiate XMLJobConfigFactory directly. Use * the factory method getJobConfigFactory in JobConfigFactory class to get the config factory * instance. This constructor has been provided to make sure this should be * instantiable and accessible to the JobConfigFactory class. * </i> * </p> */ public XMLJobConfigFactory() { } /** * This method initializes the factory by accepting the required properties * from the input map. If it doesnt find any required properties * to initialize the factory, throws ConfigurationException with the appropriate error message. * * @param configFactoryProps Map consists of all the properties defined for this factory. * * @throws ConfigurationException If required properties are missing. */ protected void init(Map configFactoryProps) { logger.trace("Entering init"); this.configFactoryProps=new HashMap(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); logger.debug("Absolute location : " + absoluteLocation + " classpath location : " + classpathLocation); if(absoluteLocation==null && classpathLocation==null) throw new ConfigurationException(ConfigurationException.JOB_CONFIG_FACTORY_CONFIG, "XML Job configuration location is not specified " + "using either absolute or classpath attribute."); 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); logger.debug("Would be able to read the configuration stream from absolute path " + absoluteLocation); if(!this.createBatchConfigElement(inputStream)) throw new ConfigurationException(ConfigurationException.JOB_CONFIG_FACTORY_CONFIG, "Error while parsing and locating root element batch-config in the XML " + "job configuration file " + absoluteLocation + "."); } else throw new ConfigurationException(ConfigurationException.JOB_CONFIG_FACTORY_CONFIG, "XML job configuration file " + absoluteLocation + " is not exist or it could not be a file."); } catch(FileNotFoundException exception) { exception.printStackTrace(); logger.error(exception.getMessage(), exception); throw new ConfigurationException(ConfigurationException.JOB_CONFIG_FACTORY_CONFIG, "XML job configuration file " + absoluteLocation + " cannot be found"); } } 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(ConfigurationException.JOB_CONFIG_FACTORY_CONFIG, "XML job configuration file " + classpathLocation + " cannot be located on the classpath."); else { logger.debug("Would be able to read the configuration from class path location : " + classpathLocation); if(!this.createBatchConfigElement(inputStream)) throw new ConfigurationException(ConfigurationException.JOB_CONFIG_FACTORY_CONFIG, "Error while parsing and locating the batch-config element in the " + "XML Job configuration file " + classpathLocation + "."); } } logger.trace("Exiting init"); } /** * Returns the requested job configuration as JobConfig object. The requested job * name should be passed as a parameter. If it does not 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) { logger.trace("Entering getJobConfig " + 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(XMLJobConfig.JOB_CONFIG_ELEMENT_NAME); logger.trace("Number of job-config elements found in job configuration file = " + jobConfigNodeList.getLength()); for(int i=0;i<jobConfigNodeList.getLength();i++) { Element jobConfigElement=(Element)jobConfigNodeList.item(i); String elementJobName=jobConfigElement.getAttribute(XMLJobConfig.JOB_NAME_ATTRIBUTE_NAME); if(jobName.equals(elementJobName)) { logger.trace("Found the job-config matching the job name"); jobConfig=new XMLJobConfig(jobConfigElement); break; } } logger.trace("Exiting getJobConfig"); 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(); logger.error(exception.getMessage(), exception); } catch(SAXException exception) { exception.printStackTrace(); logger.error(exception.getMessage(), exception); } catch(IOException exception) { exception.printStackTrace(); logger.error(exception.getMessage(), exception); } 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(); } } --- NEW FILE: XMLBasicJobControllerConfig.java --- /* * XMLBasicJobControllerConfig.java * * Created on March 2, 2006, 1:56 PM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package org.jmonks.batch.framework.config.xml; import org.apache.log4j.Logger; import org.jmonks.batch.framework.util.FrameworkUtil; import org.jmonks.batch.framework.config.BasicJobControllerConfig; import org.jmonks.batch.framework.config.ConfigurationException; import org.w3c.dom.Element; import org.w3c.dom.NodeList; /** * <p> * XMLBasicJobControllerConfig loads the XML job controller configuration. This loads * the following XML block in <job-config> from job configuration file. * <br><br> * <pre> * <job-controller job-controller-class-name="org.jmonks.batch.framework.controller.basic.BasicJobController"> * <basic-job-processor basic-job-processor-class-name="com.mycompany.batch.processfilexyz.XyzProcessor" thread-count="1"> * <property key="basic-job-processor-config1">processor-value1</property> * </basic-job-processor> * <property key="basic-job-controller-config1">config-value1</property> * </job-controller> * </pre> * </p> * * @author Suresh Pragada * @version 1.0 * @since 1.0 */ public class XMLBasicJobControllerConfig extends BasicJobControllerConfig { /** * XML Element name defiens the basic job processor configuration which is <code>basic-job-processor</code>. */ private static final String BASIC_JOB_PROCESSOR_ELEMENT_NAME = "basic-job-processor"; /** * XML Attribute name defines the basic job processor class name which is <code>basic-job-processor-class-name</code>. */ private static final String BASIC_JOB_PROCESSOR_CLASS_ATTRIBUTE_NAME = "basic-job-processor-class-name"; /** * XML Attribute name defines the thread count of the job processor which is <code>thread-count</code>. */ private static final String THREAD_COUNT_ATTRIBUTE_NAME = "thread-count"; private static Logger logger=Logger.getLogger(XMLBasicJobControllerConfig.class); /** * Loads the XML job controller configuration into XMLBasicJobControllerConfig object. * * @param controllerConfigElement XML DOM Element represents the <job-controller> element; * * @throws ConfigurationException If controller class name or job processor name doest not found. */ public XMLBasicJobControllerConfig(Element controllerConfigElement) { logger.trace("Entering Constructor"); this.jobControllerClassName=controllerConfigElement.getAttribute(XMLJobControllerConfig.JOB_CONTROLLER_CLASS_ATTRIBUTE_NAME); if(this.jobControllerClassName==null) throw new ConfigurationException(ConfigurationException.JOB_CONTROLLER_CONFIG, XMLJobControllerConfig.JOB_CONTROLLER_CLASS_ATTRIBUTE_NAME + " attribute value cannot be null."); FrameworkUtil.loadPropertiesFromElementToMap(controllerConfigElement, this.jobControllerConfigProps); logger.trace("Started reading the basic job processor configuration"); NodeList xmlBasicJobProcessorNodeList=controllerConfigElement.getElementsByTagName(XMLBasicJobControllerConfig.BASIC_JOB_PROCESSOR_ELEMENT_NAME); if(xmlBasicJobProcessorNodeList.getLength()==1) { Element xmlBasicJobProcessorElement=(Element)xmlBasicJobProcessorNodeList.item(0); this.basicJobProcessorClassName=xmlBasicJobProcessorElement.getAttribute(XMLBasicJobControllerConfig.BASIC_JOB_PROCESSOR_CLASS_ATTRIBUTE_NAME); if(this.basicJobProcessorClassName==null) throw new ConfigurationException(ConfigurationException.JOB_CONTROLLER_CONFIG, XMLBasicJobControllerConfig.BASIC_JOB_PROCESSOR_CLASS_ATTRIBUTE_NAME + " attribute value cannot be null."); else { this.basicJobProcessorThreadCount=Integer.parseInt(xmlBasicJobProcessorElement.getAttribute(XMLBasicJobControllerConfig.THREAD_COUNT_ATTRIBUTE_NAME)); FrameworkUtil.loadPropertiesFromElementToMap(xmlBasicJobProcessorElement, this.basicJobProcessorConfigProps); } } else throw new ConfigurationException(ConfigurationException.JOB_CONTROLLER_CONFIG, "Found " + xmlBasicJobProcessorNodeList.getLength() + " element(s) in the basic job controller configuration."); logger.trace("Finished reading the basic job processor configuration"); logger.trace("Exiting Constructor"); } } |