001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JavaContextHelper.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.deployment.helper;
025:
026: import static javax.ejb.TransactionManagementType.CONTAINER;
027:
028: import java.util.List;
029:
030: import javax.naming.Context;
031: import javax.naming.NamingException;
032:
033: import org.ow2.easybeans.api.Factory;
034: import org.ow2.easybeans.container.EasyBeansEJBContext;
035: import org.ow2.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata;
036: import org.ow2.easybeans.deployment.xml.struct.EJB3;
037: import org.ow2.easybeans.deployment.xml.struct.EnterpriseBeans;
038: import org.ow2.easybeans.deployment.xml.struct.Session;
039: import org.ow2.easybeans.deployment.xml.struct.common.EnvEntry;
040: import org.ow2.easybeans.naming.NamingManager;
041: import org.ow2.util.log.Log;
042: import org.ow2.util.log.LogFactory;
043:
044: /**
045: * Builds a Context (java: context).
046: * @author Florent Benoit
047: */
048: public final class JavaContextHelper {
049:
050: /**
051: * Logger.
052: */
053: private static Log logger = LogFactory
054: .getLog(JavaContextHelper.class);
055:
056: /**
057: * Utility class.
058: */
059: private JavaContextHelper() {
060:
061: }
062:
063: /**
064: * Build a new context for the given bean.
065: * @param bean the bean for which there is a need to build a context.
066: * @param easyBeansFactory the factory to build EJBContext
067: * @return a java: context.
068: * @throws JavaContextHelperException if environment cannot be built
069: */
070: public static Context build(final ClassAnnotationMetadata bean,
071: final Factory easyBeansFactory)
072: throws JavaContextHelperException {
073: Context javaCtx = null;
074: try {
075: javaCtx = NamingManager.getInstance()
076: .createEnvironmentContext(bean.getClassName());
077: } catch (NamingException e) {
078: throw new IllegalStateException(
079: "Cannot build a new environment", e);
080: }
081:
082: String beanClassName = bean.getClassName().replace("/", ".");
083:
084: // Gets java:comp
085: Context compCtx;
086: try {
087: compCtx = (Context) javaCtx.lookup("comp");
088: } catch (NamingException e) {
089: throw new JavaContextHelperException(
090: "Cannot create subcontext", e);
091: }
092:
093: // Unbind UserTransaction context if it is not a BMT bean
094: // as specified in chapter 16.12 of EJB 3 spec.
095: if (easyBeansFactory.getBeanInfo()
096: .getTransactionManagementType() == CONTAINER) {
097: logger
098: .debug("Bean is container managed so remove availability of java:comp/UserTransaction object");
099: try {
100: compCtx.unbind("UserTransaction");
101: } catch (NamingException e) {
102: throw new IllegalStateException(
103: "Cannot remove java:comp/UserTransaction object",
104: e);
105: }
106: }
107:
108: // bind EJBContext
109: EasyBeansEJBContext<?> context = new EasyBeansEJBContext(
110: easyBeansFactory);
111: try {
112: compCtx.bind("EJBContext", context);
113: } catch (NamingException e) {
114: throw new JavaContextHelperException(
115: "Cannot bind EJBContext", e);
116: }
117:
118: // bind TimerService
119: try {
120: compCtx.bind("TimerService", context.getTimerService());
121: } catch (NamingException e) {
122: throw new JavaContextHelperException(
123: "Cannot bind EJBContext", e);
124: }
125:
126: Context envCtx;
127: try {
128: envCtx = compCtx.createSubcontext("env");
129: } catch (NamingException e) {
130: throw new JavaContextHelperException(
131: "Cannot create subcontext", e);
132: }
133:
134: // get DD of the bean.
135: EJB3 ejb3 = bean.getEjbJarAnnotationMetadata().getEjb3();
136:
137: String beanName = bean.getJCommonBean().getName();
138: if (ejb3 != null) {
139: // Get env-entry of the given bean.
140: EnterpriseBeans enterpriseBeans = ejb3.getEnterpriseBeans();
141: if (enterpriseBeans != null) {
142: List<Session> sessionList = enterpriseBeans
143: .getSessionList();
144: for (Session session : sessionList) {
145: // Not the bean, continue
146: if (!beanClassName.equals(session.getEjbClass())
147: && (beanName != null && !beanName
148: .equals(session.getEjbName()))) {
149: continue;
150: }
151:
152: // bean is found, get env-entry
153: List<EnvEntry> envEntryList = session
154: .getEnvEntryList();
155: for (EnvEntry envEntry : envEntryList) {
156: String name = envEntry.getEnvEntryName();
157: Object value = getEnvEntryValue(envEntry);
158: // if null, no entry in java:comp/env as specified chapter 15.4.1
159: if (value != null) {
160: try {
161: envCtx.rebind(name, value);
162: } catch (NamingException e) {
163: throw new JavaContextHelperException(
164: "Cannot bind element '" + name
165: + "'.", e);
166: }
167: }
168: }
169:
170: }
171:
172: }
173:
174: }
175:
176: return javaCtx;
177: }
178:
179: /**
180: * Gets the value for a given env-entry.
181: * @param envEntry the element representing env-entry.
182: * @return the value associated to the given element.
183: */
184: private static Object getEnvEntryValue(final EnvEntry envEntry) {
185: final String type = envEntry.getEnvEntryType();
186: final String value = envEntry.getEnvEntryValue();
187:
188: Object returnedValue = null;
189:
190: if (type.equals(Boolean.class.getName())) {
191: if ("true".equalsIgnoreCase(value)) {
192: returnedValue = Boolean.TRUE;
193: } else if ("false".equalsIgnoreCase(value)) {
194: returnedValue = Boolean.FALSE;
195: }
196: } else if (type.equals(String.class.getName())) {
197: returnedValue = value;
198: } else if (type.equals(Integer.class.getName())) {
199: if (value != null) {
200: returnedValue = new Integer(value);
201: }
202: } else if (type.equals(Character.class.getName())) {
203: if (value != null) {
204: if (value.length() != 1) {
205: throw new IllegalStateException(
206: "The value '"
207: + value
208: + "' is not a valid value for env-entry of type java.lang.Character.");
209: }
210: returnedValue = new Character(value.charAt(0));
211: }
212: } else if (type.equals(Double.class.getName())) {
213: if (value != null) {
214: returnedValue = new Double(value);
215: }
216: } else if (type.equals(Byte.class.getName())) {
217: if (value != null) {
218: returnedValue = new Byte(value);
219: }
220: } else if (type.equals(Short.class.getName())) {
221: if (value != null) {
222: returnedValue = new Short(value);
223: }
224: } else if (type.equals(Long.class.getName())) {
225: if (value != null) {
226: returnedValue = new Long(value);
227: }
228: } else if (type.equals(Float.class.getName())) {
229: if (value != null) {
230: returnedValue = new Float(value);
231: }
232: } else {
233: throw new IllegalStateException(type
234: + " is not a valid type for env-entry.");
235: }
236: return returnedValue;
237: }
238: }
|