KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > portable > HandleDelegateUtil


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.ejb.portable;
25
26 import java.util.Properties JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28
29 import javax.naming.InitialContext JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31 import javax.naming.NameNotFoundException JavaDoc;
32
33 import javax.ejb.spi.HandleDelegate JavaDoc;
34 import javax.ejb.EJBException JavaDoc;
35
36 /**
37  * Common code for looking up the java:comp/HandleDelegate.
38  *
39  * This class can potentially be instantiated in another vendor's container
40  * so it must not refer to any non-portable RI-specific classes.
41  *
42  * @author Kenneth Saks
43  */

44 public class HandleDelegateUtil
45 {
46
47     private static final String JavaDoc JNDI_PROPERTY_FILE_NAME =
48         "com.sun.ejb.portable.jndi.propertyfilename";
49
50     // Class-level state for jndi properties override. Flag is used
51
// so that we only check once for jndi properties file override.
52
private static boolean checkedJndiProperties = false;
53
54     // contents of file referred to by jndi properties override
55
private static Properties JavaDoc jndiProperties = null;
56
57     static HandleDelegate JavaDoc getHandleDelegate()
58         throws NamingException JavaDoc
59     {
60         HandleDelegate JavaDoc handleDelegate;
61         try {
62             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
63             handleDelegate = (HandleDelegate JavaDoc)
64                 ctx.lookup("java:comp/HandleDelegate");
65         } catch(NamingException JavaDoc ne) {
66       
67             // If the lookup fails, it's probably because the default
68
// InitialContext settings needed to access the correct
69
// java:comp/HandleDelegate have been overridden in this VM.
70
// In that case, check if the system value class override
71
// property file is available and if so use it.
72
Properties JavaDoc props = null;
73             try {
74                 props = getJndiProperties();
75             } catch(Exception JavaDoc e) {
76                 // Exception while attempting to access jndi property override.
77
// Create new NamingException that describes the error.
78
NamingException JavaDoc ne2 = new NamingException JavaDoc
79                     ("Error while accessing " + JNDI_PROPERTY_FILE_NAME +
80                      " : " + e.getMessage());
81                 ne2.initCause(e);
82                 throw ne2;
83             }
84         
85             if( props == null ) {
86                 // There was no property override set.
87
NamingException JavaDoc ne3 = new NamingException JavaDoc
88                     ("java:comp/HandleDelegate not found. Unable to " +
89                      " use jndi property file override since " +
90                      JNDI_PROPERTY_FILE_NAME + " has NOT been set");
91                 ne3.initCause(ne);
92                 throw ne3;
93             }
94         
95             try {
96                 InitialContext JavaDoc ctx = new InitialContext JavaDoc(props);
97                 handleDelegate = (HandleDelegate JavaDoc)
98                     ctx.lookup("java:comp/HandleDelegate");
99             } catch(NamingException JavaDoc ne4) {
100                 NamingException JavaDoc overrideEx =
101                     new NamingException JavaDoc("Unable to lookup HandleDelegate " +
102                                         "with override properties = " +
103                                         props.toString());
104                 overrideEx.initCause(ne4);
105                 throw overrideEx;
106             }
107         }
108
109         return handleDelegate;
110     }
111
112     /**
113      * Internal method for accessing jndi properties override. We only
114      * look for properties file at most once, whether it is present or not.
115      *
116      */

117     private static Properties JavaDoc getJndiProperties()
118         throws Exception JavaDoc
119     {
120
121         synchronized(HandleDelegateUtil.class) {
122             if( !checkedJndiProperties ) {
123                 try {
124                     String JavaDoc jndiPropertyFileName =
125                         System.getProperty(JNDI_PROPERTY_FILE_NAME);
126                     
127                     if( jndiPropertyFileName != null ) {
128                         FileInputStream JavaDoc fis =
129                             new FileInputStream JavaDoc(jndiPropertyFileName);
130                         jndiProperties = new Properties JavaDoc();
131                         jndiProperties.load(fis);
132                         // Let an exception encountered here bubble up, so
133
// we can include its info in the exception propagated
134
// to the application.
135
}
136                 } finally {
137                     // Always set to true so we don't keep doing this
138
// system property and file access multiple times
139
checkedJndiProperties = true;
140                 }
141             }
142         }
143
144         return jndiProperties;
145     }
146
147
148 }
149
Popular Tags