KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > deployment > helper > JavaContextHelper


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JavaContextHelper.java 518 2006-05-28 19:48:55Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.deployment.helper;
27
28 import static javax.ejb.TransactionManagementType.CONTAINER JavaDoc;
29
30 import java.util.List JavaDoc;
31
32 import javax.naming.Context JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34
35 import org.objectweb.easybeans.api.Factory;
36 import org.objectweb.easybeans.container.EasyBeansEJBContext;
37 import org.objectweb.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata;
38 import org.objectweb.easybeans.deployment.xml.struct.EJB3;
39 import org.objectweb.easybeans.deployment.xml.struct.EnterpriseBeans;
40 import org.objectweb.easybeans.deployment.xml.struct.Session;
41 import org.objectweb.easybeans.deployment.xml.struct.common.EnvEntry;
42 import org.objectweb.easybeans.log.JLog;
43 import org.objectweb.easybeans.log.JLogFactory;
44 import org.objectweb.easybeans.naming.NamingManager;
45
46 /**
47  * Builds a Context (java: context).
48  * @author Florent Benoit
49  */

50 public final class JavaContextHelper {
51
52     /**
53      * Logger.
54      */

55     private static JLog logger = JLogFactory.getLog(JavaContextHelper.class);
56
57     /**
58      * Utility class.
59      */

60     private JavaContextHelper() {
61
62     }
63
64     /**
65      * Build a new context for the given bean.
66      * @param bean the bean for which there is a need to build a context.
67      * @param easyBeansFactory the factory to build EJBContext
68      * @return a java: context.
69      * @throws JavaContextHelperException if environment cannot be built
70      */

71     public static Context JavaDoc build(final ClassAnnotationMetadata bean, final Factory easyBeansFactory)
72             throws JavaContextHelperException {
73         Context JavaDoc javaCtx = null;
74         try {
75             javaCtx = NamingManager.getInstance().createEnvironmentContext(bean.getClassName());
76         } catch (NamingException JavaDoc e) {
77             throw new IllegalStateException JavaDoc("Cannot build a new environment", e);
78         }
79
80         String JavaDoc beanClassName = bean.getClassName().replace("/", ".");
81
82         // Gets java:comp
83
Context JavaDoc compCtx;
84         try {
85             compCtx = (Context JavaDoc) javaCtx.lookup("comp");
86         } catch (NamingException JavaDoc e) {
87             throw new JavaContextHelperException("Cannot create subcontext", e);
88         }
89
90
91         // Unbind UserTransaction context if it is not a BMT bean
92
// as specified in chapter 16.12 of EJB 3 spec.
93
if (easyBeansFactory.getBeanInfo().getTransactionManagementType() == CONTAINER) {
94             logger.debug("Bean is container managed so remove availability of java:comp/UserTransaction object");
95             try {
96                 compCtx.unbind("UserTransaction");
97             } catch (NamingException JavaDoc e) {
98                 throw new IllegalStateException JavaDoc("Cannot remove java:comp/UserTransaction object", e);
99             }
100         }
101
102
103         // bind EJBContext
104
try {
105             compCtx.bind("EJBContext", new EasyBeansEJBContext(easyBeansFactory));
106         } catch (NamingException JavaDoc e) {
107             throw new JavaContextHelperException("Cannot bind EJBContext", e);
108         }
109
110
111         Context JavaDoc envCtx;
112         try {
113             envCtx = compCtx.createSubcontext("env");
114         } catch (NamingException JavaDoc e) {
115             throw new JavaContextHelperException("Cannot create subcontext", e);
116         }
117
118         // get DD of the bean.
119
EJB3 ejb3 = bean.getEjbJarAnnotationMetadata().getEjb3();
120
121         String JavaDoc beanName = bean.getJCommonBean().getName();
122         if (ejb3 != null) {
123             // Get env-entry of the given bean.
124
EnterpriseBeans enterpriseBeans = ejb3.getEnterpriseBeans();
125             if (enterpriseBeans != null) {
126                 List JavaDoc<Session> sessionList = enterpriseBeans.getSessionList();
127                 for (Session session : sessionList) {
128                     // Not the bean, continue
129
if (!beanClassName.equals(session.getEjbClass())
130                             && (beanName != null && !beanName.equals(session.getEjbName()))) {
131                         continue;
132                     }
133
134                     // bean is found, get env-entry
135
List JavaDoc<EnvEntry> envEntryList = session.getEnvEntryList();
136                     for (EnvEntry envEntry : envEntryList) {
137                         String JavaDoc name = envEntry.getEnvEntryName();
138                         Object JavaDoc value = getEnvEntryValue(envEntry);
139                         // if null, no entry in java:comp/env as specified chapter 15.4.1
140
if (value != null) {
141                             try {
142                                 envCtx.rebind(name, value);
143                             } catch (NamingException JavaDoc e) {
144                                 throw new JavaContextHelperException("Cannot bind element '" + name + "'.", e);
145                             }
146                         }
147                     }
148
149                 }
150
151             }
152
153         }
154
155         return javaCtx;
156     }
157
158     /**
159      * Gets the value for a given env-entry.
160      * @param envEntry the element representing env-entry.
161      * @return the value associated to the given element.
162      */

163     private static Object JavaDoc getEnvEntryValue(final EnvEntry envEntry) {
164         final String JavaDoc type = envEntry.getEnvEntryType();
165         final String JavaDoc value = envEntry.getEnvEntryValue();
166
167         Object JavaDoc returnedValue = null;
168
169         if (type.equals(Boolean JavaDoc.class.getName())) {
170             if ("true".equalsIgnoreCase(value)) {
171                 returnedValue = Boolean.TRUE;
172             } else if ("false".equalsIgnoreCase(value)) {
173                 returnedValue = Boolean.FALSE;
174             }
175         } else if (type.equals(String JavaDoc.class.getName())) {
176             returnedValue = value;
177         } else if (type.equals(Integer JavaDoc.class.getName())) {
178             if (value != null) {
179                 returnedValue = new Integer JavaDoc(value);
180             }
181         } else if (type.equals(Character JavaDoc.class.getName())) {
182             if (value != null) {
183                 if (value.length() != 1) {
184                     throw new IllegalStateException JavaDoc("The value '" + value
185                             + "' is not a valid value for env-entry of type java.lang.Character.");
186                 }
187                 returnedValue = new Character JavaDoc(value.charAt(0));
188             }
189         } else if (type.equals(Double JavaDoc.class.getName())) {
190             if (value != null) {
191                 returnedValue = new Double JavaDoc(value);
192             }
193         } else if (type.equals(Byte JavaDoc.class.getName())) {
194             if (value != null) {
195                 returnedValue = new Byte JavaDoc(value);
196             }
197         } else if (type.equals(Short JavaDoc.class.getName())) {
198             if (value != null) {
199                 returnedValue = new Short JavaDoc(value);
200             }
201         } else if (type.equals(Long JavaDoc.class.getName())) {
202             if (value != null) {
203                 returnedValue = new Long JavaDoc(value);
204             }
205         } else if (type.equals(Float JavaDoc.class.getName())) {
206             if (value != null) {
207                 returnedValue = new Float JavaDoc(value);
208             }
209         } else {
210             throw new IllegalStateException JavaDoc(type + " is not a valid type for env-entry.");
211         }
212         return returnedValue;
213     }
214 }
215
Popular Tags