UniSessionFactory

From Pickwiki
Revision as of 23:48, 26 February 2015 by Conversion script (talk) (link fix)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

JavaSource -> UniSessionFactory

Test code here: TestFactory

There is an improved version of this class, see FailoverUniSessionFactory

package edu.asu.vpia.dao;

import org.apache.commons.logging.Log; 
import org.apache.commons.logging.[[LogFactory]]; 
import java.util.Properties;
import asjava.uniobjects.[[UniSession]];
import asjava.uniobjects.[[UniSessionException]];

/**
* Factory method to create connected [[UniSession]] objects
* You must place a file named 'unidata.properties' directly
* on the classpath.  The file may contain [[ACCOUNT_PATH]]=/path/to/data
* and may contain definitions for [[HOST_NAME]], [[HOST_PORT]], [[CONNECTION_STRING]]
* [[USER_NAME]] and PASSWORD to override the default settings.
* @author Wendy Smoak [email protected]
*/
public class [[UniSessionFactory]] 
{
       private static Log log = [[LogFactory]].getLog( [[UniSessionFactory]].class );
       
       /[[/UniData]] defaults:
       private static final int [[HOST_PORT]] = 31438;
       private static final String [[CONNECTION_STRING]] = "udcs";
       private static final String [[HOST_NAME]] = "your.db.server";
       private static final String [[USER_NAME]] = "user.name";
       private static final String PASSWORD  = "password";
       private static final String [[ACCOUNT_PATH]] = "/path/to/data";
       
       /[[/UniVerse]] defaults:
       //private static final String [[CONNECTION_STRING]] = "uvcs";
       

       private [[UniSessionFactory]] () {}
       
       public static [[UniSession]] openSession () 
         throws [[UniSessionException]]
       {
           [[UniSession]] uSession = new [[UniSession]]();
           log.debug("[[UniSessionFactory]].openSession, uSession=" + uSession);
           
           //you cannot change the port on [[UniData]], so no need [for me]
           //to pull this from properties.  Besides, parsing
           //the String to an integer is a pain.
           uSession.set[[HostPort]]( [[HOST_PORT]] );
           
           try {
              [[ClassLoader]] cl = [[UniSessionFactory]].class.get[[ClassLoader]]();
              Properties props = new Properties();
              props.load( cl.get[[ResourceAsStream]]( "unidata.properties" ) ) ;
              
              //set the [[UniSession]] properties, using the defaults if the
              //values are not present in the unidata.properties file
              uSession.set[[AccountPath]]( props.getProperty( "[[ACCOUNT_PATH]]", [[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]] ) );
              
           } catch ( java.io.IOException ex) {
              log.debug( ex );
           }
           
           uSession.connect();
           log.debug("connected to " + uSession.get[[AccountPath]]() );
           
           return uSession;
           
       }

}