Example: Connection factory lookup

 

Example: Connection factory lookup

import javax.resource.cci.*;
import javax.resource.ResourceException;

import javax.naming.*;

import java.util.*;

/** 
 * This class is used to look up a connection factory.
 */
public class ConnectionFactoryLookup {

   String jndiName = "java:comp/env/eis/SampleConnection";
   boolean verbose = false;

   /**
    * main method     */
   public static void main(String[] args) {
      ConnectionFactoryLookup cfl = new ConnectionFactoryLookup();
      cfl.checkParam(args);

      try {
         cfl.lookupConnectionFactory();
      }
      catch(javax.naming.NamingException ne) {
   System.out.println("Caught this " + ne);
   ne.printStackTrace(System.out);
      }
      catch(javax.resource.ResourceException re) {
   System.out.println("Caught this " + re);
   re.printStackTrace(System.out);
      }
   }

 /**
  * This method does a simple Connection Factory lookup. 
    *  
    * After the Connection Factory is looked up, a connection is got from 
  * the Connection Factory.  Then the Connection MetaData is retrieved 
    * to verfiy the connection is workable.
  */
 public void lookupConnectionFactory()
  throws javax.naming.NamingException, javax.resource.ResourceException {

  javax.resource.cci.ConnectionFactory factory = null;
  javax.resource.cci.Connection conn = null;
  javax.resource.cci.ConnectionMetaData metaData = null;

  try {
   // lookup the connection factory          if (verbose) System.out.println("Look up the connection factory...");

   InitialContext ic = new InitialContext();
   factory = (ConnectionFactory) ic.lookup(jndiName);

   // Get connection          if (verbose) System.out.println("Get the connection...");
   conn = factory.getConnection();

   // Get ConnectionMetaData    metaData = conn.getMetaData();

   // Print out the metadata Informatin.
   if (verbose) System.out.println("  **  EISProductName   :" + metaData.getEISProductName());
   if (verbose) System.out.println("      EISProductVersion:" + metaData.getEISProductName());
   if (verbose) System.out.println("      UserName         :" + metaData.getUserName());

         System.out.println("Connection factory "+jndiName+" is successfully looked up");
  }
  catch (javax.naming.NamingException ne) {
   // Connection factory cannot be looked up.
   throw ne;
  }
  catch (javax.resource.ResourceException re) {
   // Something wrong with connections.
   throw re;
  }
  finally {
   if (conn != null) {
    try {
     conn.close();
    }
    catch (javax.resource.ResourceException re) {
    }
   }
  }
 }

   /**
    * Check and gather all the parameters.
    */
   private void checkParam(String args[]) {
  int i = 0, j;
  String arg;
  char flag;
      boolean help = false;

  // parse out the options   while (i < args.length && args[i].startsWith("-")) {
   arg = args[i++];

   // get the database name    if (arg.equalsIgnoreCase("-jndiName")) {
    if (i < args.length)
     jndiName = args[i++];
    else {
     System.err.println("-jndiName requires a J2C Connection Factory JNDI name");
     break;
    }
   }
   else { // check for verbose, cmp , bmp     for (j = 1; j < arg.length(); j++) {
     flag = arg.charAt(j);
     switch (flag) {
      case 'v' :
      case 'V' :
       verbose = true;
       break;
                  case 'h' :
                  case 'H' :
                      help = true;
                      break;
      default :
       System.err.println("illegal option " + flag);
       break;
     }
    }
   }
  }

  if ((i != args.length) || help) {
   System.err.println("Usage: java ConnectionFactoryLookup [-v] [-h]");
   System.err.println("    [-jndiName the J2C Connection Factory JNDI name]");
   System.err.println("-v=verbose");
   System.err.println("-h=this information");
   System.exit(1);
  }
   }
   
}



Related concepts
Connection factory