FileDocCategorySizeDatePackage
HandleDelegateUtil.javaAPI DocGlassfish v2 API6568Fri May 04 22:33:12 BST 2007com.sun.ejb.portable

HandleDelegateUtil

public class HandleDelegateUtil extends Object
Common code for looking up the java:comp/HandleDelegate. This class can potentially be instantiated in another vendor's container so it must not refer to any non-portable RI-specific classes.
author
Kenneth Saks

Fields Summary
private static final String
JNDI_PROPERTY_FILE_NAME
private static boolean
checkedJndiProperties
private static Properties
jndiProperties
Constructors Summary
Methods Summary
static javax.ejb.spi.HandleDelegategetHandleDelegate()


       
         
    
        HandleDelegate handleDelegate;
        try {
            InitialContext ctx = new InitialContext();
            handleDelegate = (HandleDelegate) 
                ctx.lookup("java:comp/HandleDelegate");
        } catch(NamingException ne) {
      
            // If the lookup fails, it's probably because the default 
            // InitialContext settings needed to access the correct
            // java:comp/HandleDelegate have been overridden in this VM.  
            // In that case, check if the system value class override 
            // property file is available and if so use it.
            Properties props = null;
            try {
                props = getJndiProperties();
            } catch(Exception e) {
                // Exception while attempting to access jndi property override.
                // Create new NamingException that describes the error.
                NamingException ne2 = new NamingException
                    ("Error while accessing " + JNDI_PROPERTY_FILE_NAME +
                     " : " + e.getMessage());
                ne2.initCause(e);
                throw ne2;
            }
        
            if( props == null ) {                    
                // There was no property override set.
                NamingException ne3 = new NamingException
                    ("java:comp/HandleDelegate not found. Unable to " +
                     " use jndi property file override since " +
                     JNDI_PROPERTY_FILE_NAME + " has NOT been set");
                ne3.initCause(ne);
                throw ne3;
            }
        
            try {
                InitialContext ctx = new InitialContext(props);
                handleDelegate = (HandleDelegate) 
                    ctx.lookup("java:comp/HandleDelegate");
            } catch(NamingException ne4) {
                NamingException overrideEx = 
                    new NamingException("Unable to lookup HandleDelegate " +
                                        "with override properties = " + 
                                        props.toString());
                overrideEx.initCause(ne4);
                throw overrideEx;
            }
        }

        return handleDelegate;
    
private static java.util.PropertiesgetJndiProperties()
Internal method for accessing jndi properties override. We only look for properties file at most once, whether it is present or not.


        synchronized(HandleDelegateUtil.class) {
            if( !checkedJndiProperties ) {
                try {
                    String jndiPropertyFileName = 
                        System.getProperty(JNDI_PROPERTY_FILE_NAME);
                    
                    if( jndiPropertyFileName != null ) {
                        FileInputStream fis = 
                            new FileInputStream(jndiPropertyFileName);
                        jndiProperties = new Properties();
                        jndiProperties.load(fis);
                        // Let an exception encountered here bubble up, so
                        // we can include its info in the exception propagated
                        // to the application.
                    }
                } finally {
                    // Always set to true so we don't keep doing this 
                    // system property and file access multiple times
                    checkedJndiProperties = true;
                }
            }
        }

        return jndiProperties;