KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > alt > assembler > classic > JndiEncBuilder


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact info@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2005 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: JndiEncBuilder.java 2080 2005-08-16 01:05:03Z dblevins $
44  */

45
46 package org.openejb.alt.assembler.classic;
47
48 import org.openejb.OpenEJBException;
49 import org.openejb.core.CoreUserTransaction;
50 import org.openejb.core.ivm.naming.IntraVmJndiReference;
51 import org.openejb.core.ivm.naming.IvmContext;
52 import org.openejb.core.ivm.naming.JndiReference;
53 import org.openejb.core.ivm.naming.NameNode;
54 import org.openejb.core.ivm.naming.ParsedName;
55 import org.openejb.core.ivm.naming.Reference;
56
57 import javax.naming.Context JavaDoc;
58 import javax.naming.NamingException JavaDoc;
59 import javax.transaction.UserTransaction JavaDoc;
60 import java.util.HashMap JavaDoc;
61 import java.util.Iterator JavaDoc;
62 import java.util.Map JavaDoc;
63
64 /**
65  * @version $Revision: 2080 $ $Date: 2005-08-15 18:05:03 -0700 (Mon, 15 Aug 2005) $
66  */

67 public class JndiEncBuilder {
68
69     private final ReferenceWrapper referenceWrapper;
70     private final boolean beanManagedTransactions;
71     private final EjbReferenceInfo[] ejbReferences;
72     private final EjbLocalReferenceInfo[] ejbLocalReferences;
73     private final EnvEntryInfo[] envEntries;
74     private final ResourceReferenceInfo[] resourceRefs;
75
76     /**
77      * Constructs the builder and normalizes the input data. Does not build till build() is called
78      *
79      * @param jndiEnc
80      * @param transactionType
81      * @throws OpenEJBException
82      */

83     public JndiEncBuilder(JndiEncInfo jndiEnc, String JavaDoc transactionType, EjbType ejbType) throws OpenEJBException {
84         if (ejbType.isEntity()) {
85             referenceWrapper = new EntityRefereceWrapper();
86         } else if (ejbType == EjbType.STATEFUL) {
87             referenceWrapper = new StatefulRefereceWrapper();
88         } else if (ejbType == EjbType.STATELESS) {
89             referenceWrapper = new StatelessRefereceWrapper();
90         } else {
91             throw new org.openejb.OpenEJBException("Unknown component type");
92         }
93
94         beanManagedTransactions = transactionType != null && transactionType.equalsIgnoreCase("Bean");
95
96         ejbReferences = (jndiEnc != null && jndiEnc.ejbReferences != null) ? jndiEnc.ejbReferences : new EjbReferenceInfo[]{};
97         ejbLocalReferences = (jndiEnc != null && jndiEnc.ejbLocalReferences != null) ? jndiEnc.ejbLocalReferences : new EjbLocalReferenceInfo[]{};
98         envEntries = (jndiEnc != null && jndiEnc.envEntries != null) ? jndiEnc.envEntries : new EnvEntryInfo[]{};
99         resourceRefs = (jndiEnc != null && jndiEnc.resourceRefs != null) ? jndiEnc.resourceRefs : new ResourceReferenceInfo[]{};
100     }
101
102     public Context build() throws OpenEJBException {
103         HashMap JavaDoc bindings = new HashMap JavaDoc();
104
105         if (beanManagedTransactions) {
106             Object JavaDoc userTransaction = referenceWrapper.wrap(new CoreUserTransaction());
107             bindings.put("java:comp/UserTransaction", userTransaction);
108         }
109
110         for (int i = 0; i < ejbReferences.length; i++) {
111             EjbReferenceInfo referenceInfo = ejbReferences[i];
112             EjbReferenceLocationInfo location = referenceInfo.location;
113
114             Reference reference = null;
115
116             if (!location.remote) {
117                 String JavaDoc jndiName = "java:openejb/ejb/" + location.ejbDeploymentId;
118                 reference = new IntraVmJndiReference(jndiName);
119             } else {
120                 String JavaDoc openEjbSubContextName = "java:openejb/remote_jndi_contexts/" + location.jndiContextId;
121                 reference = new JndiReference(openEjbSubContextName, location.remoteRefName);
122             }
123             bindings.put(normalize(referenceInfo.referenceName), wrapReference(reference));
124         }
125
126         for (int i = 0; i < ejbLocalReferences.length; i++) {
127             EjbLocalReferenceInfo referenceInfo = ejbLocalReferences[i];
128
129             EjbReferenceLocationInfo location = referenceInfo.location;
130             if (location != null && !location.remote) {
131                 String JavaDoc jndiName = "java:openejb/ejb/" + location.ejbDeploymentId + "Local";
132                 Reference reference = new IntraVmJndiReference(jndiName);
133                 bindings.put(normalize(referenceInfo.referenceName), wrapReference(reference));
134             }
135         }
136
137         for (int i = 0; i < envEntries.length; i++) {
138             EnvEntryInfo entry = envEntries[i];
139             try {
140                 Class JavaDoc type = Class.forName(entry.type.trim());
141                 Object JavaDoc obj = null;
142                 if (type == String JavaDoc.class)
143                     obj = new String JavaDoc(entry.value);
144                 else if (type == Double JavaDoc.class) {
145                     obj = new Double JavaDoc(entry.value);
146                 } else if (type == Integer JavaDoc.class) {
147                     obj = new Integer JavaDoc(entry.value);
148                 } else if (type == Long JavaDoc.class) {
149                     obj = new Long JavaDoc(entry.value);
150                 } else if (type == Float JavaDoc.class) {
151                     obj = new Float JavaDoc(entry.value);
152                 } else if (type == Short JavaDoc.class) {
153                     obj = new Short JavaDoc(entry.value);
154                 } else if (type == Boolean JavaDoc.class) {
155                     obj = new Boolean JavaDoc(entry.value);
156                 } else if (type == Byte JavaDoc.class) {
157                     obj = new Byte JavaDoc(entry.value);
158                 } else {
159                     throw new IllegalArgumentException JavaDoc("Invalid env-ref-type " + type);
160                 }
161
162                 bindings.put(normalize(entry.name), obj);
163             } catch (ClassNotFoundException JavaDoc e) {
164                 throw new IllegalArgumentException JavaDoc("Invalid environment entry type: " + entry.type.trim() + " for entry: " + entry.name);
165             } catch (NumberFormatException JavaDoc e) {
166                 throw new IllegalArgumentException JavaDoc("The env-entry-value for entry " + entry.name + " was not recognizable as type " + entry.type + ". Received Message: " + e.getLocalizedMessage());
167             }
168         }
169
170         for (int i = 0; i < resourceRefs.length; i++) {
171             ResourceReferenceInfo referenceInfo = resourceRefs[i];
172             Reference reference = null;
173
174             if (referenceInfo.resourceID != null) {
175                 String JavaDoc jndiName = "java:openejb/connector/" + referenceInfo.resourceID;
176                 reference = new IntraVmJndiReference(jndiName);
177             } else {
178                 String JavaDoc openEjbSubContextName1 = "java:openejb/remote_jndi_contexts/" + referenceInfo.location.jndiContextId;
179                 String JavaDoc jndiName2 = referenceInfo.location.remoteRefName;
180                 reference = new JndiReference(openEjbSubContextName1, jndiName2);
181             }
182             bindings.put(normalize(referenceInfo.referenceName), wrapReference(reference));
183         }
184
185         IvmContext enc = new IvmContext(new NameNode(null, new ParsedName("comp"), null));
186
187         for (Iterator JavaDoc iterator = bindings.entrySet().iterator(); iterator.hasNext();) {
188             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
189             String JavaDoc name = (String JavaDoc) entry.getKey();
190             Object JavaDoc value = entry.getValue();
191             try {
192                 enc.bind(name, value);
193             } catch (NamingException JavaDoc e) {
194                 throw new org.openejb.SystemException("Unable to bind '" + name + "' into bean's enc.", e);
195             }
196         }
197         return enc;
198     }
199
200     private String JavaDoc normalize(String JavaDoc name) {
201         if (name.charAt(0) == '/')
202             name = name.substring(1);
203         if (!(name.startsWith("java:comp/env") || name.startsWith("comp/env"))) {
204             if (name.startsWith("env/"))
205                 name = "comp/" + name;
206             else
207                 name = "comp/env/" + name;
208         }
209         return name;
210     }
211
212     private Object JavaDoc wrapReference(Reference reference) {
213         return referenceWrapper.wrap(reference);
214     }
215
216     static abstract class ReferenceWrapper {
217         abstract Object JavaDoc wrap(Reference reference);
218
219         abstract Object JavaDoc wrap(UserTransaction reference);
220     }
221
222     static class EntityRefereceWrapper extends ReferenceWrapper {
223         public Object JavaDoc wrap(Reference reference) {
224             return new org.openejb.core.entity.EncReference(reference);
225         }
226
227         public Object JavaDoc wrap(UserTransaction userTransaction) {
228             throw new IllegalStateException JavaDoc("Entity beans cannot have references to UserTransaction instance");
229         }
230     }
231
232     static class StatelessRefereceWrapper extends ReferenceWrapper {
233         public Object JavaDoc wrap(Reference reference) {
234             return new org.openejb.core.stateless.EncReference(reference);
235         }
236
237         public Object JavaDoc wrap(UserTransaction userTransaction) {
238             return new org.openejb.core.stateless.EncUserTransaction((CoreUserTransaction) userTransaction);
239         }
240     }
241
242     static class StatefulRefereceWrapper extends ReferenceWrapper {
243         public Object JavaDoc wrap(Reference reference) {
244             return new org.openejb.core.stateful.EncReference(reference);
245         }
246
247         public Object JavaDoc wrap(UserTransaction userTransaction) {
248             return new org.openejb.core.stateful.EncUserTransaction((CoreUserTransaction) userTransaction);
249         }
250     }
251 }
252
Popular Tags