KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > util > DefaultDOLVisitor


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.enterprise.deployment.util;
25
26 import java.util.Iterator JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.lang.reflect.Field JavaDoc;
29
30 import com.sun.enterprise.deployment.Descriptor;
31 import com.sun.enterprise.deployment.Application;
32 import com.sun.enterprise.deployment.BundleDescriptor;
33 import com.sun.enterprise.deployment.EjbBundleDescriptor;
34 import com.sun.enterprise.deployment.EjbDescriptor;
35 import com.sun.enterprise.deployment.MethodPermission;
36 import com.sun.enterprise.deployment.MethodDescriptor;
37 import com.sun.enterprise.deployment.WebService;
38 import com.sun.enterprise.deployment.ContainerTransaction;
39 import com.sun.enterprise.deployment.types.EjbReference;
40 import com.sun.enterprise.deployment.types.MessageDestinationReferencer;
41 import com.sun.enterprise.deployment.ServiceReferenceDescriptor;
42 import com.sun.enterprise.deployment.JmsDestinationReferenceDescriptor;
43 import com.sun.enterprise.deployment.MessageDestinationDescriptor;
44 import com.sun.enterprise.deployment.MessageDestinationReferenceDescriptor;
45 import com.sun.enterprise.deployment.ResourceReferenceDescriptor;
46 import com.sun.enterprise.deployment.RoleReference;
47 import com.sun.enterprise.deployment.EnvironmentProperty;
48 import com.sun.enterprise.deployment.FieldDescriptor;
49 import com.sun.enterprise.deployment.QueryDescriptor;
50 import com.sun.enterprise.deployment.RelationshipDescriptor;
51 import com.sun.enterprise.deployment.ConnectorDescriptor;
52 import com.sun.enterprise.deployment.WebBundleDescriptor;
53 import com.sun.enterprise.deployment.ApplicationClientDescriptor;
54 import com.sun.enterprise.deployment.WebComponentDescriptor;
55 import com.sun.enterprise.deployment.InjectionCapable;
56 import com.sun.enterprise.deployment.InjectionTarget;
57 import com.sun.enterprise.util.TypeUtil;
58
59 /**
60  * Default implementation of all the DOL visitor interface for convenience
61  *
62  * @author Jerome Dochez
63  * @version
64  */

65
66 public class DefaultDOLVisitor implements ApplicationVisitor, EjbBundleVisitor, EjbVisitor, ConnectorVisitor, WebBundleVisitor, AppClientVisitor {
67    protected BundleDescriptor bundleDescriptor = null;
68
69     /** Creates new TraceVisitor */
70     public DefaultDOLVisitor() {
71     }
72     
73     /**
74      * visit an application object
75      * @param the application descriptor
76      */

77     public void accept(Application application) {
78     }
79     
80     /**
81      * visits an ejb bundle descriptor
82      * @param an ejb bundle descriptor
83      */

84     public void accept(EjbBundleDescriptor bundleDescriptor) {
85         this.bundleDescriptor = bundleDescriptor;
86     }
87     
88     /**
89      * visits an ejb descriptor
90      * @param ejb descriptor
91      */

92     public void accept(EjbDescriptor ejb) {
93     }
94
95     /**
96      * visits a connector descriptor
97      * @param connectordescriptor
98      */

99     public void accept(ConnectorDescriptor conDesc) {
100     }
101         
102     public void accept(InjectionCapable injectable) {
103     }
104     
105     /**
106      * visits an ejb reference for the last J2EE component visited
107      * @param the ejb reference
108      */

109     public void accept(EjbReference ejbRef) {
110     }
111
112     public void accept(MessageDestinationReferencer msgDestReferencer) {
113     }
114
115     /**
116      * visits a web service reference descriptor
117      * @param serviceRef
118      */

119     public void accept(ServiceReferenceDescriptor serviceRef) {
120     }
121
122     /**
123      * visits a web service definition
124      * @param web service
125      */

126     public void accept(WebService webService) {
127     }
128
129     /**
130      * visits a method permission and permitted methods for the last J2EE component visited
131      * @param method permission
132      * @param the methods associated with the above permission
133      */

134     public void accept(MethodPermission pm, Iterator JavaDoc methods) {
135     }
136     
137     /**
138      * visits a role reference for the last J2EE component visited
139      * @param role reference
140      */

141     public void accept(RoleReference roleRef) {
142     }
143     
144     /**
145      * visists a method transaction for the last J2EE component visited
146      * @param method descriptor the method
147      * @param container transaction
148      */

149     public void accept(MethodDescriptor method, ContainerTransaction ct) {
150     }
151     
152     /**
153      * visits an environment property for the last J2EE component visited
154      * @param the environment property
155      */

156     public void accept(EnvironmentProperty envEntry) {
157     }
158
159     /**
160      * visits an resource reference for the last J2EE component visited
161      * @param the resource reference
162      */

163     public void accept(ResourceReferenceDescriptor resRef) {
164     }
165
166     /**
167      * visits an jms destination reference for the last J2EE component visited
168      * @param the jms destination reference
169      */

170     public void accept(JmsDestinationReferenceDescriptor jmsDestRef) {
171     }
172
173     /**
174      * visits an message destination reference for the last J2EE component visited
175      * @param the message destination reference
176      */

177     public void accept(MessageDestinationReferenceDescriptor msgDestRef) {
178     }
179
180     /**
181      * visits an message destination for the last J2EE component visited
182      * @param the message destination
183      */

184     public void accept(MessageDestinationDescriptor msgDest) {
185     }
186
187     /**
188      * visits a appclient descriptor
189      * @param appclientdescriptor
190      */

191     public void accept(ApplicationClientDescriptor appclientdescriptor) {
192        bundleDescriptor = appclientdescriptor;
193     }
194
195      /**
196      * @return an implementation of the ejbBundleVisitor (if any)
197      * to visit ejb bundle descriptors
198      */

199     public EjbBundleVisitor getEjbBundleVisitor() {
200         return this;
201     }
202     
203     /**
204      * @return a EjbVisitor (if ejbs should be visited)
205      */

206     public EjbVisitor getEjbVisitor() {
207         return this;
208     }
209
210     /**
211      * @return a ConnectorVisitor (if connector should be visited)
212      */

213     public ConnectorVisitor getConnectorVisitor() {
214         return this;
215     }
216
217     /**
218      * @return a AppClientVisitor (if app client should be visited)
219      */

220     public AppClientVisitor getAppClientVisitor() {
221         return this;
222     }
223     
224     /**
225      * visits a CMP field definition (for CMP entity beans)
226      * @param field descriptor for the CMP field
227      */

228     public void accept(FieldDescriptor fd) {
229     }
230     
231     /**
232      * visits a query method
233      * @param method descriptor for the method
234      * @param query descriptor
235      */

236     public void accept(MethodDescriptor method, QueryDescriptor qd) {
237     }
238     
239     /**
240      * visits an ejb relationship descriptor
241      * @param the relationship descriptor
242      */

243     public void accept(RelationshipDescriptor descriptor) {
244     }
245     
246     /**
247      * visits a J2EE descriptor
248      * @param the descriptor
249      */

250     public void accept(Descriptor descriptor) {
251     }
252     
253     /**
254      * @return an implementation of the ejbBundleVisitor (if any)
255      * to visit ejb bundle descriptors
256      */

257     public WebBundleVisitor getWebBundleVisitor() {
258         return this;
259     }
260     
261     /**
262      * visit a web bundle descriptor
263      *
264      * @param the web bundle descriptor
265      */

266     public void accept(WebBundleDescriptor descriptor) {
267         this.bundleDescriptor = descriptor;
268     }
269     
270     /**
271      * visit a web component descriptor
272      *
273      * @param the web component
274      */

275     public void accept(WebComponentDescriptor descriptor) {
276     }
277     
278
279     // we need to split the accept(InjectionCapable) into two parts:
280
// one needs classloader and one doesn't. This is needed because
281
// in the standalone war case, the classloader is not created
282
// untill the web module is being started.
283

284     protected void acceptWithCL(InjectionCapable injectable) {
285         // If parsed from deployment descriptor, we need to determine whether
286
// the inject target name refers to an injection field or an
287
// injection method for each injection target
288
for (InjectionTarget target : injectable.getInjectionTargets()) {
289             if( (target.getFieldName() == null) &&
290                     (target.getMethodName() == null) ) {
291
292                 String JavaDoc injectTargetName = target.getTargetName();
293                 String JavaDoc targetClassName = target.getClassName();
294                 ClassLoader JavaDoc classLoader = getBundleDescriptor().getClassLoader();
295
296                 Class JavaDoc targetClazz = null;
297
298                 try {
299
300                     targetClazz = classLoader.loadClass(targetClassName);
301
302                 } catch(ClassNotFoundException JavaDoc cnfe) {
303                     // @@@
304
// Don't treat this as a fatal error for now. One known issue
305
// is that all .xml, even web.xml, is processed within the
306
// appclient container during startup. In that case, there
307
// are issues with finding .classes in .wars due to the
308
// structure of the returned client .jar and the way the
309
// classloader is formed.
310
DOLUtils.getDefaultLogger().fine
311                             ("Injection class " + targetClassName + " not found for " +
312                             injectable);
313                     return;
314                 }
315
316                 // Spec requires that we attempt to match on method before field.
317
boolean matched = false;
318
319                 // The only information we have is method name, so iterate
320
// through the methods find a match. There is no overloading
321
// allowed for injection methods, so any match is considered
322
// the only possible match.
323

324                 String JavaDoc setterMethodName = TypeUtil.
325                         propertyNameToSetterMethod(injectTargetName);
326
327                 // method can have any access type so use getDeclaredMethods()
328
for(Method JavaDoc next : targetClazz.getDeclaredMethods()) {
329                     if( next.getName().equals(setterMethodName)) {
330
331                         target.setMethodName(next.getName());
332                         if( injectable.getInjectResourceType() == null ) {
333                             Class JavaDoc[] paramTypes = next.getParameterTypes();
334                             if (paramTypes.length == 1) {
335                                 String JavaDoc resourceType = paramTypes[0].getName();
336                                 injectable.setInjectResourceType(resourceType);
337                             }
338                         }
339                         matched = true;
340                         break;
341                     }
342                 }
343
344                if( !matched ) {
345
346                     // In the case of injection fields, inject target name ==
347
// field name. Field can have any access type.
348
try {
349                         Field JavaDoc f = targetClazz.getDeclaredField(injectTargetName);
350                         target.setFieldName(injectTargetName);
351                         if( injectable.getInjectResourceType() == null ) {
352                             String JavaDoc resourceType = f.getType().getName();
353                             injectable.setInjectResourceType(resourceType);
354                         }
355                         matched = true;
356                     } catch(NoSuchFieldException JavaDoc nsfe) {
357                         String JavaDoc msg = "No matching injection setter method or " +
358                                 "injection field found for injection property " +
359                                 injectTargetName + " on class " + targetClassName +
360                                 " for component dependency " + injectable;
361
362                         throw new RuntimeException JavaDoc(msg, nsfe);
363                     }
364                 }
365             }
366         }
367     }
368
369     protected void acceptWithoutCL(InjectionCapable injectable) {
370         // Special processing for UserTransaction and ORB types.
371
if("javax.transaction.UserTransaction".equals(injectable.getInjectResourceType())) {
372             // Map the logical reference to the spec-defined java:comp
373
// entry for UserTransaction.
374
if (injectable instanceof ResourceReferenceDescriptor) {
375                 ResourceReferenceDescriptor resRef =
376                     (ResourceReferenceDescriptor) injectable;
377                 resRef.setJndiName("java:comp/UserTransaction");
378             }
379         } else if("org.omg.CORBA.ORB".equals(injectable.getInjectResourceType())) {
380             // Map the logical reference to the spec-defined java:comp
381
// entry for ORB.
382
if (injectable instanceof ResourceReferenceDescriptor) {
383                 ResourceReferenceDescriptor resRef =
384                     (ResourceReferenceDescriptor) injectable;
385                 resRef.setJndiName("java:comp/ORB");
386             }
387         }
388     }
389
390     /**
391      * @return the bundleDescriptor we are visiting
392      */

393     protected BundleDescriptor getBundleDescriptor() {
394         return bundleDescriptor;
395     }
396 }
397
Popular Tags