FailoverUniSessionFactory
From Pickwiki
Like the original UniSessionFactory, this class provides connected sessions to UniData.
package edu.asu.vpia.dao; import asjava.uniobjects.[[UniSession]]; import asjava.uniobjects.[[UniSessionException]]; import java.io.[[InputStream]]; import java.util.Properties; import org.apache.commons.logging.Log; import org.apache.commons.logging.[[LogFactory]]; /** * Factory method to create connected [[UniSession]] objects Uses the provided * .properties filename, or 'unidata.properties' if none is provided. File must * be on the classpath. The properties file must contain * [[ACCOUNT_PATH]]=/path/to/data and may contain definitions for [[HOST_NAME]], * [[HOST_PORT]], [[CONNECTION_STRING]] [[USER_NAME]], PASSWORD and TIMEOUT to override the * default settings. FAILOVER is optional and * names the next .properties file to try if we are unable to connect using the * initial one. * *@author Wendy Smoak *@created 2003 */ public class [[UniSessionFactory]] { private static Log log = [[LogFactory]].getLog( [[UniSessionFactory]].class ); /[[/UniData]] defaults: //private static final int [[HOST_PORT]] = 31438; private final static String [[CONNECTION_STRING]] = "udcs"; private final static String [[HOST_NAME]] = "your.host.name"; private final static String [[USER_NAME]] = "user"; private final static String PASSWORD = "pass"; //private static final String [[ACCOUNT_PATH]] = "/path/to/data"; private final static String [[PROPS_FILENAME]] = "unidata.properties"; /[[/UniVerse]] defaults: //private static final String [[CONNECTION_STRING]] = "uvcs"; /** * Private constructor. This class contains only static methods */ private [[UniSessionFactory]]() { } /** * Opens a session using the default .properties filename (unidata.properties) * *@return Description of the Return Value *@exception [[UniSessionException]] Description of the Exception *@exception DAOException Description of the Exception */ public static [[UniSession]] openSession() throws [[UniSessionException]], DAOException { log.debug( "openSession: using default unidata.properties filename" ); return openSession( [[PROPS_FILENAME]] ); } /** * Opens a session using the provided .properties filename * *@param props[[FileName]] Description of the Parameter *@return Description of the Return Value *@exception [[UniSessionException]] Description of the Exception *@exception DAOException Description of the Exception */ public static [[UniSession]] openSession( String props[[FileName]] ) throws [[UniSessionException]], DAOException { [[UniSession]] uSession = new [[UniSession]](); //log.debug( "openSession: uSession=" + uSession ); //you cannot change the port on [[UniData]], so no need [for me] //to pull this from the .properties file. //uSession.set[[HostPort]]( [[HOST_PORT]] ); Properties props = new Properties(); try { [[ClassLoader]] cl = [[UniSessionFactory]].class.get[[ClassLoader]](); [[InputStream]] input = cl.get[[ResourceAsStream]]( props[[FileName]] ); if ( input == null ) { log.fatal( "openSession: Unable to load " + props[[FileName]] ); throw new DAOException( "Properties file " + props[[FileName]] + " missing" ); } props.load( input ); //set the [[UniSession]] properties, using the defaults if the //values are not present in the [[PROPS_FILENAME]] file /[[/ACCOUNT_PATH]] *must* be in the properties file, no default provided //uSession.set[[AccountPath]]( props.getProperty( "[[ACCOUNT_PATH]]", [[ACCOUNT_PATH]] ) ); uSession.set[[AccountPath]]( props.getProperty( "[[ACCOUNT_PATH]]" ) ); uSession.set[[HostName]]( props.getProperty( "[[HOST_NAME]]", [[HOST_NAME]] ) ); uSession.set[[UserName]]( props.getProperty( "[[USER_NAME]]", [[USER_NAME]] ) ); uSession.setPassword( props.getProperty( "PASSWORD", PASSWORD ) ); uSession.set[[ConnectionString]]( props.getProperty( "[[CONNECTION_STRING]]", [[CONNECTION_STRING]] ) ); int defaultTimeout = uSession.getTimeout(); int timeout = Integer.parseInt( props.getProperty( "TIMEOUT", String.valueOf( defaultTimeout ) ) ); uSession.setTimeout( timeout ); log.debug( "openSession: [[ACCOUNT_PATH]]=" + uSession.get[[AccountPath]]() + " [[HOST_NAME]]=" + uSession.get[[HostName]]() + " [[USER_NAME]]=" + uSession.get[[UserName]]() ); } catch ( java.io.IOException ex ) { log.error( ex ); throw new DAOException( ex ); } try { uSession.connect(); } catch ( [[UniSessionException]] ex ) { String failover = props.getProperty( "FAILOVER" ); if ( failover != null && !failover.equals( "" ) ) { log.debug( "openSession: failing over to " + failover ); return [[UniSessionFactory]].openSession( failover ); } else { throw new DAOException( ex ); } } log.debug( "connected to " + uSession.get[[AccountPath]]() ); return uSession; } }