HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"The Business Advantage of EJB (Part 2)"
Volume: 4 Issue: 11, p. 8

	

Listing 1: EJBContextWrapper
 
import javax.naming.InitialContext; 
import javax.naming.Context; 
import java.util.Properties; 
import java.io.FileInputStream; 
  

// EJBContextWrapper is a utility class to service requests 
// for a JNDI context.  It loads JNDI lookup properties from 
// a properties file, or uses defaults when none are preset. 
// It also allows the properties to be bypassed by calling 
// overrides of getContext() 
  

public class EJBContextWrapper extends java.lang.Object 
{ 

        private static String url = ""; 
        private static String factory = ""; 
        private static String password = ""; 
        private static String user = ""; 

        static { 
         try { 
          // load in the JNDI settings from configuration file 
          FileInputStream in = new FileInputStream("JNDIConf.properties"); 
          Properties props = new Properties(); 
          props.load(in); 
          in.close(); 
  

          url = props.getProperty("url"); 
          factory = props.getProperty("contextfactory"); 
          user = props.getProperty("user"); 
          password = props.getProperty("password"); 

         } catch (Exception ex) { 
          // defaults 
          url = "t3://localhost:7001"; 
          factory = "weblogic.jndi.TengahInitialContextFactory"; 
          user = ""; 
          password = ""; 

          System.out.println("Failed to initialize EJBContextWrapper "+ 
                             "properties. Using defaults."); 
          System.out.println("url =  "+url 
                             +" factory = "+factory 
                             +" user = "+user 
                             +" password = "+password); 
        } 
       } 

  // constructor, do not let this get instantiated 

  public EJBContextWrapper () 
  {} 

  // getContext() override that uses loaded values to get a 
  // context in JNDI 

  public static InitialContext getContext() throws Exception 
  { 
     return getContext(url, factory, user, password); 
  } 

  // getContext() override that uses specified user/pwd and 
  // loaded url/factory to get a context in JNDI 

  public static InitialContext getContext(String aUser, String 
  aPwd) throws Exception 
  { 
    return getContext(url, factory, aUser, aPwd); 
  } 

  // getContext() override that uses specified parameters to 
  // get a context in JNDI 

  public static InitialContext getContext(String aUrl, String 
  aFactory, String aUser, String aPwd) throws Exception 
  { 
   Properties p = new Properties(); 
   p.put(Context.INITIAL_CONTEXT_FACTORY, aFactory); 
   p.put(Context.PROVIDER_URL, aUrl); 

   if (user != null || user != "") { 
    p.put(Context.SECURITY_PRINCIPAL, aUser); 

   if (password == null) 
    password = ""; 

   p.put(Context.SECURITY_CREDENTIALS, aPwd); 
  } 

  return new InitialContext(p); 
 } 

} 
  



      
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.