[Batchserver-cvs] batchserver/src/org/jmonks/batchserver/framework/config/db DBBasicJobControllerCon
Brought to you by:
suresh_pragada
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/framework/config/db In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1852 Modified Files: DBBasicJobControllerConfig.java DBJobConfig.java DBJobConfigFactory.java DBJobControllerConfig.java DBPoolJobControllerConfig.java Log Message: no message Index: DBJobConfig.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/framework/config/db/DBJobConfig.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** DBJobConfig.java 3 Mar 2006 04:19:56 -0000 1.1 --- DBJobConfig.java 10 Mar 2006 08:15:16 -0000 1.2 *************** *** 11,28 **** package org.jmonks.batchserver.framework.config.db; import org.jmonks.batchserver.framework.config.JobConfig; /** ! * ! * @author Suresh Pragada */ public class DBJobConfig extends JobConfig { ! ! /** Creates a new instance of DBJobConfig */ ! DBJobConfig(Object dbValueToLoadJobConfig) { } - } --- 11,86 ---- package org.jmonks.batchserver.framework.config.db; + import java.sql.Connection; + import java.sql.PreparedStatement; + import java.sql.ResultSet; + import java.sql.SQLException; + import org.jmonks.batchserver.framework.common.ErrorCode; + import org.jmonks.batchserver.framework.config.ConfigurationException; import org.jmonks.batchserver.framework.config.JobConfig; /** ! * <p> ! * DBJobConfig provides the implementation of JobConfig by reading the ! * job configuration from given connection. This reads the job configuration ! * from table "job_config". Following is the schema of that table. ! * <br><br> ! * <pre> ! * <table> ! * <tr><td><b>Column Name</b></td><td><b>Data Type</b></td></tr> ! * <tr><td>JOB_NAME</td><td>VARCHAR2(64)</td></tr> ! * <tr><td>JOB_STATUS</td><td>NUMBER</td></tr> ! * <tr><td>JOB_CONTROLLER_CLASS_NAME</td><td>VARCHAR2(256)</td></tr> ! * <tr><td>JOB_CONTROLLER_PROPS</td><td>VARCHAR2(1024)</td></tr> ! * </table> ! * </pre> ! * </p> ! * ! * @author Suresh Pragada ! * @since 1.0 ! * @version 1.0 */ public class DBJobConfig extends JobConfig { ! /** ! * Query to load the job configuration from job_config table. ! */ ! private static final String JOB_CONFIG_QUERY = "select job_status from job_config where job_name=?"; ! /** ! * Loads the job name and controller configuration into the job configuration object. ! * ! * @param jobName Name of the job to be loaded. ! * @param connection Connection to the Database. ! * ! * @throws ConfigurationException If job or controllers are not configured properly. ! */ ! DBJobConfig(String jobName,Connection connection) { + this.jobName=jobName; + try + { + PreparedStatement statement=connection.prepareStatement(DBJobConfig.JOB_CONFIG_QUERY); + statement.setString(1, jobName); + ResultSet resultSet=statement.executeQuery(); + if(resultSet.next()) + { + int status=resultSet.getInt(1); + this.jobStatus=(status==1?true:false); + + this.jobControllerConfig=DBJobControllerConfig.getJobControllerConfig(jobName, connection); + } + else + { + /** + * How can this is possible? Just now.. I could see this entry in job_config. + */ + throw new IllegalStateException("Surprisingly.. Could not find entry in job_config with job_name " + jobName); + } + } + catch(SQLException exception) + { + exception.printStackTrace(); + throw new ConfigurationException(ErrorCode.CANNOT_CREATE_DB_JOB_CONFIG); + } } } Index: DBJobConfigFactory.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/framework/config/db/DBJobConfigFactory.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** DBJobConfigFactory.java 3 Mar 2006 04:19:56 -0000 1.1 --- DBJobConfigFactory.java 10 Mar 2006 08:15:16 -0000 1.2 *************** *** 11,37 **** package org.jmonks.batchserver.framework.config.db; import java.util.Map; import org.jmonks.batchserver.framework.config.JobConfig; import org.jmonks.batchserver.framework.config.JobConfigFactory; /** * ! * @author Suresh Pragada */ public class DBJobConfigFactory extends JobConfigFactory { ! private Map mConfigFactoryProps=null; ! /** Creates a new instance of DBJobConfigFactory */ public DBJobConfigFactory(Map configFactoryProps) { ! } public JobConfig getJobConfig(String jobName) { /** ! * Look up job configuration in database and pass that to DBJobConfig. */ ! return new DBJobConfig(null); } } --- 11,194 ---- package org.jmonks.batchserver.framework.config.db; + import java.sql.Connection; + import java.sql.DriverManager; + import java.sql.PreparedStatement; + import java.sql.ResultSet; + import java.sql.SQLException; + import java.util.HashMap; import java.util.Map; + 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; /** + * <p> + * DBJobConfigFactory reads the job configuration from defined database to create the + * configuration objects needed by the job. This factory looks for few properties + * in the config properties map to identify the database it needs to connect to. This + * factory looks for the job configuration in the table <b>job-config</b> in the database. + * Controller configuration will be defined in a seperate tables, and DBJobControllerConfig + * is responsible to read the job controller configuration from the database. + * </p> + * <p> + * Following are the four properties, this factory looks in job config properties + * to connect to the database. If anyone of the property is missing, it throws + * ConfigurationException. + * <table> + * <tr><td><b>property key</b></td><td><b>property value</b></td></tr> + * <tr><td>config-db-jdbc-driver-class-name</td><td>oracle.jdbc.driver.OracleDriver</td></tr> + * <tr><td>config-db-url</td><td>jdbc:oracle:thin:@hostname:1521:instname</td></tr> + * <tr><td>config-db-username</td><td>scott</td></tr> + * <tr><td>config-db-password</td><td>tiger</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 DBJobConfigFactory extends JobConfigFactory { ! /** ! * Property map holds the properites required by this factory. ! */ ! private Map configFactoryProps=null; ! /** ! * Constant defines the property name of jdbc driver class name. ! */ ! private static final String PROPERTY_JOB_CONFIG_JDBC_DRIVER_CLASS_NAME = "config-db-jdbc-driver-class-name"; ! /** ! * Constant defines the property name of database URL. ! */ ! private static final String PROPERTY_JOB_CONFIG_DATABASE_URL = "config-db-url"; ! /** ! * Constant defines the property name of daabase user name. ! */ ! private static final String PROPERTY_JOB_CONFIG_DATABASE_USER_NAME = "config-db-username"; ! /** ! * Constant defines the property name of database password. ! */ ! private static final String PROPERTY_JOB_CONFIG_DATABASE_PASSWORD = "config-db-password"; ! /** ! * SQL Query to pull the job configuration. ! */ ! private static final String JOB_CONFIG_QUERY = "select 1 from job_config where job_name=?"; ! /** ! * This constructor initializes the factory by accepting the required properties ! * map as a parameter to the constructor. If it doesnt find any required properties ! * and values are not valid 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 DBJobConfigFactory(Map configFactoryProps) { ! this.configFactoryProps=new HashMap(configFactoryProps); ! /** ! * Do the basic validation like look for the required properites and make a connection ! * and close it to make sure property values are valid. ! */ ! Connection connection=this.getConnection(); ! if(connection==null) ! throw new ConfigurationException(ErrorCode.DB_JOB_CONFIG_FACTORY_PROPERTIES_INVALID); ! else ! { ! try ! { ! connection.close(); ! } ! catch(SQLException sqle) ! { ! sqle.printStackTrace(); ! } ! } } + /** + * 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 table <b>job_config</b> in given database, 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; + if(jobName==null) + throw new IllegalArgumentException("Job name cannot be null."); + + Connection connection=this.getConnection(); + if(connection==null) + throw new IllegalStateException("DBJobConfigFacotry not initialized properly. Unable to get the connection."); /** ! * Look up job configuration in table job_config and pass that information to DBJobConfig. */ ! try ! { ! PreparedStatement statement=connection.prepareStatement(DBJobConfigFactory.JOB_CONFIG_QUERY); ! statement.setString(1, jobName); ! ResultSet resultSet=statement.executeQuery(); ! if(resultSet.next()) ! { ! jobConfig=new DBJobConfig(jobName,connection); ! } ! else ! jobConfig=null; ! } ! finally ! { ! connection.close(); ! } ! ! return jobConfig; ! } ! ! /** ! * Get the required properties from the configFactoryProperties map and ! * establishes the connection. Returns null, if it cannot establish the ! * connection. ! * ! * @return Returns the database connction, null, if it cannot establish the connection. ! */ ! private Connection getConnection() ! { ! Connection connection=null; ! ! String driverClassName=(String)this.configFactoryProps.get(DBJobConfigFactory.PROPERTY_JOB_CONFIG_JDBC_DRIVER_CLASS_NAME); ! String databaseURL=(String)this.configFactoryProps.get(DBJobConfigFactory.PROPERTY_JOB_CONFIG_DATABASE_URL); ! String userName=(String)this.configFactoryProps.get(DBJobConfigFactory.PROPERTY_JOB_CONFIG_DATABASE_USER_NAME); ! String password=(String)this.configFactoryProps.get(DBJobConfigFactory.PROPERTY_JOB_CONFIG_DATABASE_PASSWORD); ! ! if(driverClassName==null || "".equals(driverClassName) || databaseURL==null || "".equals(databaseURL) || ! userName==null || "".equals(userName) || password==null || "".equals(password)) ! connection=null; ! else ! { ! try ! { ! Class.forName(driverClassName); ! connection=DriverManager.getConnection(databaseURL,userName, password); ! } ! /** ! * No need to worry about the specific exception. Either case need to return null. ! * So catching the generic one. ! */ ! catch(Exception exception) ! { ! exception.printStackTrace(); ! connection=null; ! } ! } ! return connection; } } Index: DBBasicJobControllerConfig.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/framework/config/db/DBBasicJobControllerConfig.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** DBBasicJobControllerConfig.java 9 Mar 2006 04:44:24 -0000 1.2 --- DBBasicJobControllerConfig.java 10 Mar 2006 08:15:16 -0000 1.3 *************** *** 11,25 **** package org.jmonks.batchserver.framework.config.db; import org.jmonks.batchserver.framework.config.BasicJobControllerConfig; /** * ! * @author Suresh Pragada */ public class DBBasicJobControllerConfig extends BasicJobControllerConfig { ! /** Creates a new instance of DBBasicJobControllerConfig */ ! public DBBasicJobControllerConfig(Object dbValueToReadControllerConfig) { } --- 11,84 ---- package org.jmonks.batchserver.framework.config.db; + import java.sql.Connection; + import java.sql.PreparedStatement; + import java.sql.ResultSet; + import java.sql.SQLException; + import org.jmonks.batchserver.framework.common.ErrorCode; + import org.jmonks.batchserver.framework.common.FrameworkUtil; import org.jmonks.batchserver.framework.config.BasicJobControllerConfig; + import org.jmonks.batchserver.framework.config.ConfigurationException; /** + * <p> + * DBBasicJobControllerConfig loads the job controller configuration from the defined + * database. This loads the basic controller configuration from the table "basic_job_controller_config". + * Following is the schema of that table defined in the database. + * <br><br> + * <pre> + * <table> + * <tr><td><b>Column Name</b></td><td><b>Data Type</b></td></tr> + * <tr><td>JOB_NAME</td><td>VARCHAR2(64)</td></tr> + * <tr><td>BASIC_JOB_PROCESSOR_CLASS_NAME</td><td>VARCHAR2(256)</td></tr> + * <tr><td>BASIC_JOB_PROCESSOR_THREAD_CNT</td><td>NUMBER</td></tr> + * <tr><td>BASIC_JOB_PROCESSOR_PROPS</td><td>VARCHAR2(1024)</td></tr> + * </table> + * </pre> + * </p> * ! * @author Suresh Pragada ! * @version 1.0 ! * @since 1.0 */ public class DBBasicJobControllerConfig extends BasicJobControllerConfig { ! /** ! * SQL Query pulls the information basic_job_controller_config table. ! */ ! private static final String BASIC_JOB_CONTROLLER_CONFIG_QUERY = "select basic_job_processor_class_name, basic_job_processor_thread_cnt, basic_job_processor_props from basic_job_controller_config where job_name=?"; ! /** ! * Loads the basic job controller configuration from table basic_job_controller_config ! * into DBBasicJobControllerConfig object. ! * ! * @param jobName Name of the job. ! * @param connection Connection to the defined database. ! * ! * @throws ConfigurationException If controller class name or job processor name doest not found. ! */ ! public DBBasicJobControllerConfig(String jobName,Connection connection) { + + try + { + PreparedStatement statement=connection.prepareStatement(DBBasicJobControllerConfig.BASIC_JOB_CONTROLLER_CONFIG_QUERY); + statement.setString(1,jobName); + ResultSet resultSet=statement.executeQuery(); + if(resultSet.next()) + { + this.basicJobProcessorClassName=resultSet.getString(1); + this.basicJobProcessorThreadCount=resultSet.getInt(2); + String processorConfigProps=resultSet.getString(3); + if(processorConfigProps!=null) + FrameworkUtil.loadPropertiesFromStringToMap(processorConfigProps,this.basicJobProcessorConfigProps); + } + else + { + throw new ConfigurationException(ErrorCode.BASIC_JOB_CONTROLLER_CONFIG_NOT_FOUND); + } + } + catch(SQLException exception) + { + exception.printStackTrace(); + } } Index: DBPoolJobControllerConfig.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/framework/config/db/DBPoolJobControllerConfig.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** DBPoolJobControllerConfig.java 9 Mar 2006 04:44:24 -0000 1.2 --- DBPoolJobControllerConfig.java 10 Mar 2006 08:15:16 -0000 1.3 *************** *** 11,14 **** --- 11,15 ---- package org.jmonks.batchserver.framework.config.db; + import java.sql.Connection; import org.jmonks.batchserver.framework.config.PoolJobControllerConfig; *************** *** 20,24 **** { /** Creates a new instance of DBPoolJobControllerConfig */ ! public DBPoolJobControllerConfig(Object dbValueToReadControllerConfig) { /** --- 21,25 ---- { /** Creates a new instance of DBPoolJobControllerConfig */ ! public DBPoolJobControllerConfig(String jobName,Connection connection) { /** Index: DBJobControllerConfig.java =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/framework/config/db/DBJobControllerConfig.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** DBJobControllerConfig.java 3 Mar 2006 04:19:56 -0000 1.1 --- DBJobControllerConfig.java 10 Mar 2006 08:15:16 -0000 1.2 *************** *** 11,25 **** package org.jmonks.batchserver.framework.config.db; import org.jmonks.batchserver.framework.config.JobControllerConfig; /** ! * ! * @author Suresh Pragada */ public abstract class DBJobControllerConfig extends JobControllerConfig { ! public static JobControllerConfig getJobControllerConfig(Object dbValueToReadControllerConfig) { ! return null; } } --- 11,114 ---- package org.jmonks.batchserver.framework.config.db; + import java.lang.reflect.Constructor; + import java.lang.reflect.InvocationTargetException; + import java.sql.Connection; + import java.sql.PreparedStatement; + import java.sql.ResultSet; + import java.sql.SQLException; + import org.jmonks.batchserver.framework.common.ErrorCode; + import org.jmonks.batchserver.framework.config.ConfigurationException; + import org.jmonks.batchserver.framework.config.FrameworkConfig; import org.jmonks.batchserver.framework.config.JobControllerConfig; /** ! * DBJobControllerConfig responsible to build the controller specific ! * JobControllerConfig objects from the given database. ! * ! * @author Suresh Pragada ! * @version 1.0 ! * @since 1.0 */ public abstract class DBJobControllerConfig extends JobControllerConfig { ! /** ! * SQL Query retrieves the job controller class name and controller properites. ! */ ! private static final String JOB_CONFIG_QUERY = "select job_controller_class_name,job_controller_props from job_config where job_name=?"; ! /** ! * Identifier used to request the Framework controller configuration for the specific controller config for this factory. ! */ ! public static final String JOB_CONTROLLER_CONFIG_FACTORY_CLASS_ATTRIBUTE_NAME = "db-factory-config-class-name"; ! ! /** ! * <p> ! * Factory method creates and returns the controller specific controller config object ! * from the table job_config and controller specific tables. This looks for the ! * controller class name in job_config and tries to find specific controller config object for this controller ! * and defined for the DB factory in framework configuration file. ! * </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(String jobName,Connection connection) { ! JobControllerConfig controllerConfig=null; ! ! try ! { ! PreparedStatement statement=connection.prepareStatement(DBJobControllerConfig.JOB_CONFIG_QUERY); ! statement.setString(1, jobName); ! ResultSet resultSet=statement.executeQuery(); ! if(resultSet.next()) ! { ! String controllerClassName=resultSet.getString(1); ! try ! { ! String controllerConfigClassName=FrameworkConfig.getInstance().getJobControllerConfig().getConfigClassName(controllerClassName, DBJobControllerConfig.JOB_CONTROLLER_CONFIG_FACTORY_CLASS_ATTRIBUTE_NAME); ! if(controllerConfigClassName==null) ! throw new ConfigurationException(ErrorCode.JOB_CONTROLLER_CLASS_NOT_DEFINED); ! else ! { ! Class configClass=Class.forName(controllerConfigClassName); ! Constructor constructor=configClass.getConstructor(new Class[]{String.class,Connection.class}); ! controllerConfig=(JobControllerConfig)constructor.newInstance(new Object[]{jobName,connection}); ! } ! } ! catch(InstantiationException exception) ! { ! exception.printStackTrace(); ! throw new ConfigurationException(ErrorCode.JOB_CONTROLLER_CONFIG_CLASS_NOT_VALID); ! } ! catch(NoSuchMethodException exception) ! { ! exception.printStackTrace(); ! throw new ConfigurationException(ErrorCode.JOB_CONTROLLER_CONFIG_CLASS_NOT_VALID); ! } ! catch(ClassNotFoundException exception) ! { ! exception.printStackTrace(); ! throw new ConfigurationException(ErrorCode.JOB_CONTROLLER_CONFIG_CLASS_NOT_VALID); ! } ! catch(IllegalAccessException exception) ! { ! exception.printStackTrace(); ! throw new ConfigurationException(ErrorCode.JOB_CONTROLLER_CONFIG_CLASS_NOT_VALID); ! } ! catch(InvocationTargetException exception) ! { ! exception.printStackTrace(); ! throw new ConfigurationException(ErrorCode.JOB_CONTROLLER_CONFIG_CLASS_NOT_VALID); ! } ! } ! } ! catch(SQLException exception) ! { ! exception.printStackTrace(); ! throw new ConfigurationException(ErrorCode.CANNOT_CREATE_DB_JOB_CONFIG); ! } ! return controllerConfig; } } |