[Batchserver-cvs] batchserver/src/org/jmonks/batch/framework/util FrameworkUtil.java, NONE, 1.1 Jdb
Brought to you by:
suresh_pragada
From: Suresh <sur...@us...> - 2006-09-15 20:07:43
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batch/framework/util In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv20378 Added Files: FrameworkUtil.java JdbcConnectionHelper.java Log Message: no message --- NEW FILE: FrameworkUtil.java --- package org.jmonks.batch.framework.util; import java.util.Map; import java.util.StringTokenizer; import java.util.logging.Logger; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** * <p> * FramworkUtil contains utility methods required by the framework. * </p> * * @author Suresh Pragada * @version 1.0 * @since 1.0 */ public final class FrameworkUtil { private static Logger logger=Logger.getLogger(FrameworkUtil.class.getName()); /** * Private constructor to make sure it will not be instantiated. */ private FrameworkUtil() { /** * Just to make sure this class will not be instantiated. */ } /** * <p> * Loads the property elements exists in the given element to the given map. * This looks for the <property> elements in the given element and looks * for the value of "key" attribute and the values of <property> element * and load them as key values pairs. * </p> * <p> * Property elements should be in the following format.<br><br> * <property key="some-config-key1">some-config-value1</property> * </p> * * @param configElement DOM Element consists of <property> elements. * @param propertyMap Map to be loaded with property key values. * * @throws IllegalArgumentException If either configElement or propertyMap is null. */ public static void loadPropertiesFromElementToMap(Element configElement,Map propertyMap) { logger.entering(FrameworkUtil.class.getName(),"loadPropertiesFromElementToMap"); if(configElement==null) throw new IllegalArgumentException("Input configuration element configElement cannot be null"); if(propertyMap==null) throw new IllegalArgumentException("Input properties map propertyMap cannot be null"); NodeList propertyElements=configElement.getElementsByTagName("property"); for(int i=0;i<propertyElements.getLength();i++) { Element propertyElement=(Element)propertyElements.item(i); String key=propertyElement.getAttribute("key"); String value=""; NodeList propertyValueNodes=propertyElement.getChildNodes(); for(int j=0;j<propertyValueNodes.getLength();j++) { Node propertyValueNode=propertyValueNodes.item(j); if(propertyValueNode.getNodeType()==Node.TEXT_NODE || propertyValueNode.getNodeType()==Node.CDATA_SECTION_NODE) { value=propertyValueNode.getNodeValue(); break; } } propertyMap.put(key,value); } logger.exiting(FrameworkUtil.class.getName(),"loadPropertiesFromElementToMap"); } /** * <p> * Loads the property key value pairs exists in the given string to the given map. * This looks for the properties in concantenated by ":" and each property in turn * concatenated by "=". * </p> * <p> * Property elements should be in the following format.<br><br> * key1=value1:key2=value2 * </p> * * @param propertiesString String consists of properties. * @param propertyMap Map to be loaded with property key values. * * @throws IllegalArgumentException If either propertiesString or propertyMap is null. */ public static void loadPropertiesFromStringToMap(String propertiesString,Map propertyMap) { logger.entering(FrameworkUtil.class.getName(),"loadPropertiesFromStringToMap"); if(propertiesString==null) throw new IllegalArgumentException("Input properties string cannot be null"); if(propertyMap==null) throw new IllegalArgumentException("Input properties map propertyMap cannot be null"); StringTokenizer propertiesTokenizer=new StringTokenizer(propertiesString,":"); while(propertiesTokenizer.hasMoreTokens()) { String property=propertiesTokenizer.nextToken(); if(property!=null && !"".equals(property.trim())) { StringTokenizer propertyTokenizer=new StringTokenizer(property,"="); String key=propertyTokenizer.nextToken(); String value=propertyTokenizer.nextToken(); propertyMap.put(key,value); } } logger.exiting(FrameworkUtil.class.getName(),"loadPropertiesFromStringToMap"); } } --- NEW FILE: JdbcConnectionHelper.java --- /* * JdbcConnectionHelper.java * * Created on September 9, 2006, 10:57 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.util; import java.sql.Connection; import java.sql.DriverManager; import java.util.Map; import org.apache.log4j.Logger; /** * JdbcConnectionHelper helps the jdbc repository and db configuration factory * to create the jdbc connections with the given configuration map and closes the * connections. * * @author Suresh Pragada * @version 1.0 * @since 1.0 */ public final class JdbcConnectionHelper { /** * Constant defines the property name for jdbc driver class name which is * <code>jdbc-driver-class-name</code>. */ public static final String PROPERTY_JDBC_DRIVER_CLASS_NAME = "jdbc-driver-class-name"; /** * Constant defines the property name for database URL which is * <code>jdbc-url</code>. */ public static final String PROPERTY_JDBC_URL = "jdbc-url"; /** * Constant defines the property name for database user name which is * <code>username</code>. */ public static final String PROPERTY_DATABASE_USER_NAME = "username"; /** * Constant defines the property name for database password which is * <code>password</code>. */ public static final String PROPERTY_DATABASE_PASSWORD = "password"; private static Logger logger=Logger.getLogger(JdbcConnectionHelper.class); /** * To restrict the instantiation of JdbcConnectionHelper */ private JdbcConnectionHelper() { } /** * Get the required properties from the given configMap and * establishes the connection. Returns null, if it cannot establish the * connection. * * @param configMap Map contains the properties required to create the connection. * * @return Returns the database connction, null, if it cannot establish the connection. */ public static synchronized Connection getConnection(Map configMap) { logger.trace("Entering getConnection"); logger.info("Connection configuration : " + configMap); Connection connection=null; String driverClassName=(String)configMap.get(JdbcConnectionHelper.PROPERTY_JDBC_DRIVER_CLASS_NAME); String databaseURL=(String)configMap.get(JdbcConnectionHelper.PROPERTY_JDBC_URL); String userName=(String)configMap.get(JdbcConnectionHelper.PROPERTY_DATABASE_USER_NAME); String password=(String)configMap.get(JdbcConnectionHelper.PROPERTY_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); connection.setAutoCommit(false); logger.debug("Connection has been created successfully"); } catch(Exception exception) { /** * No need to worry about the specific exception. In any case need to return null. * So catching the generic one. */ exception.printStackTrace(); logger.error(exception.getMessage(), exception); connection=null; } } logger.trace("Exiting getConnection"); return connection; } /** * Closes the given connection. Doesn't throw any exceptions to the caller. */ public static void closeConnection(Connection connection) { try { if(connection!=null && !connection.isClosed()) connection.close(); logger.debug("Connection has been closed successfully"); } catch(Exception exception) { exception.printStackTrace(); logger.error(exception.getMessage(),exception); } } } |