KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > startup > WebAnnotationSet


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.catalina.startup;
20
21
22 import javax.annotation.Resource;
23 import javax.annotation.Resources;
24 import javax.annotation.security.DeclareRoles;
25 import javax.annotation.security.RunAs;
26
27 import org.apache.catalina.Container;
28 import org.apache.catalina.Context;
29 import org.apache.catalina.core.StandardWrapper;
30 import org.apache.catalina.deploy.ContextEnvironment;
31 import org.apache.catalina.deploy.ContextResource;
32 import org.apache.catalina.deploy.ContextResourceEnvRef;
33 import org.apache.catalina.deploy.ContextService;
34 import org.apache.catalina.deploy.FilterDef;
35 import org.apache.catalina.deploy.MessageDestinationRef;
36
37 /**
38  * <p><strong>AnnotationSet</strong> for processing the annotations of the web application
39  * classes (<code>/WEB-INF/classes</code> and <code>/WEB-INF/lib</code>).</p>
40  *
41  * @author Fabien Carrion
42  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
43  */

44
45 public class WebAnnotationSet {
46     
47     
48     // --------------------------------------------------------- Public Methods
49

50     
51     /**
52      * Process the annotations on a context.
53      */

54     public static void loadApplicationAnnotations(Context context) {
55         
56         loadApplicationListenerAnnotations(context);
57         loadApplicationFilterAnnotations(context);
58         loadApplicationServletAnnotations(context);
59         
60         
61     }
62     
63     
64     // -------------------------------------------------------- protected Methods
65

66     
67     /**
68      * Process the annotations for the listeners.
69      */

70     protected static void loadApplicationListenerAnnotations(Context context) {
71         String JavaDoc[] applicationListeners = context.findApplicationListeners();
72         for (int i = 0; i < applicationListeners.length; i++) {
73             loadClassAnnotation(context, applicationListeners[i]);
74         }
75     }
76     
77     
78     /**
79      * Process the annotations for the filters.
80      */

81     protected static void loadApplicationFilterAnnotations(Context context) {
82         FilterDef[] filterDefs = context.findFilterDefs();
83         for (int i = 0; i < filterDefs.length; i++) {
84             loadClassAnnotation(context, (filterDefs[i]).getFilterClass());
85         }
86     }
87     
88     
89     /**
90      * Process the annotations for the servlets.
91      */

92     protected static void loadApplicationServletAnnotations(Context context) {
93         
94         ClassLoader JavaDoc classLoader = context.getLoader().getClassLoader();
95         StandardWrapper wrapper = null;
96         Class JavaDoc classClass = null;
97         
98         Container[] children = context.findChildren();
99         for (int i = 0; i < children.length; i++) {
100             if (children[i] instanceof StandardWrapper) {
101                 
102                 wrapper = (StandardWrapper) children[i];
103                 if (wrapper.getServletClass() == null) {
104                     continue;
105                 }
106                 
107                 try {
108                     classClass = classLoader.loadClass(wrapper.getServletClass());
109                 } catch (ClassNotFoundException JavaDoc e) {
110                     // We do nothing
111
} catch (NoClassDefFoundError JavaDoc e) {
112                     // We do nothing
113
}
114                 
115                 if (classClass == null) {
116                     continue;
117                 }
118                 
119                 loadClassAnnotation(context, wrapper.getServletClass());
120                 /* Process RunAs annotation which can be only on servlets.
121                  * Ref JSR 250, equivalent to the run-as element in
122                  * the deployment descriptor
123                  */

124                 if (classClass.isAnnotationPresent(RunAs.class)) {
125                     RunAs annotation = (RunAs)
126                         classClass.getAnnotation(RunAs.class);
127                     wrapper.setRunAs(annotation.value());
128                 }
129             }
130         }
131         
132         
133     }
134     
135     
136     /**
137      * Process the annotations on a context for a given className.
138      */

139     protected static void loadClassAnnotation(Context context, String JavaDoc fileString) {
140         
141         ClassLoader JavaDoc classLoader = context.getLoader().getClassLoader();
142         Class JavaDoc classClass = null;
143         
144         try {
145             classClass = classLoader.loadClass(fileString);
146         } catch (ClassNotFoundException JavaDoc e) {
147             // We do nothing
148
} catch (NoClassDefFoundError JavaDoc e) {
149             // We do nothing
150
}
151         
152         if (classClass == null) {
153             return;
154         }
155         
156         // Initialize the annotations
157

158         if (classClass.isAnnotationPresent(Resource.class)) {
159             Resource annotation = (Resource)
160                 classClass.getAnnotation(Resource.class);
161             addResource(context, annotation);
162         }
163         /* Process Resources annotation.
164          * Ref JSR 250
165          */

166         if (classClass.isAnnotationPresent(Resources.class)) {
167             Resources annotation = (Resources)
168                 classClass.getAnnotation(Resources.class);
169             for (int i = 0; annotation.value() != null && i < annotation.value().length; i++) {
170                 addResource(context, annotation.value()[i]);
171             }
172         }
173         /* Process EJB annotation.
174          * Ref JSR 224, equivalent to the ejb-ref or ejb-local-ref
175          * element in the deployment descriptor.
176         if (classClass.isAnnotationPresent(EJB.class)) {
177             EJB annotation = (EJB)
178             classClass.getAnnotation(EJB.class);
179             
180             if ((annotation.mappedName().length() == 0) ||
181                     annotation.mappedName().equals("Local")) {
182                 
183                 ContextLocalEjb ejb = new ContextLocalEjb();
184                 
185                 ejb.setName(annotation.name());
186                 ejb.setType(annotation.beanInterface().getCanonicalName());
187                 ejb.setDescription(annotation.description());
188                 
189                 ejb.setHome(annotation.beanName());
190                 
191                 context.getNamingResources().addLocalEjb(ejb);
192                 
193             } else if (annotation.mappedName().equals("Remote")) {
194                 
195                 ContextEjb ejb = new ContextEjb();
196                 
197                 ejb.setName(annotation.name());
198                 ejb.setType(annotation.beanInterface().getCanonicalName());
199                 ejb.setDescription(annotation.description());
200                 
201                 ejb.setHome(annotation.beanName());
202                 
203                 context.getNamingResources().addEjb(ejb);
204                 
205             }
206             
207         }
208          */

209         /* Process WebServiceRef annotation.
210          * Ref JSR 224, equivalent to the service-ref element in
211          * the deployment descriptor.
212          * The service-ref registration is not implemented
213         if (classClass.isAnnotationPresent(WebServiceRef.class)) {
214             WebServiceRef annotation = (WebServiceRef)
215             classClass.getAnnotation(WebServiceRef.class);
216             
217             ContextService service = new ContextService();
218             
219             service.setName(annotation.name());
220             service.setWsdlfile(annotation.wsdlLocation());
221             
222             service.setType(annotation.type().getCanonicalName());
223             
224             if (annotation.value() == null)
225                 service.setServiceinterface(annotation.type().getCanonicalName());
226             
227             if (annotation.type().getCanonicalName().equals("Service"))
228                 service.setServiceinterface(annotation.type().getCanonicalName());
229             
230             if (annotation.value().getCanonicalName().equals("Endpoint"))
231                 service.setServiceendpoint(annotation.type().getCanonicalName());
232             
233             service.setPortlink(annotation.type().getCanonicalName());
234             
235             context.getNamingResources().addService(service);
236             
237             
238         }
239          */

240         /* Process DeclareRoles annotation.
241          * Ref JSR 250, equivalent to the security-role element in
242          * the deployment descriptor
243          */

244         if (classClass.isAnnotationPresent(DeclareRoles.class)) {
245             DeclareRoles annotation = (DeclareRoles)
246                 classClass.getAnnotation(DeclareRoles.class);
247             for (int i = 0; annotation.value() != null && i < annotation.value().length; i++) {
248                 context.addSecurityRole(annotation.value()[i]);
249             }
250         }
251         
252         
253     }
254     
255     
256     /**
257      * Process a Resource annotation to set up a Resource.
258      * Ref JSR 250, equivalent to the resource-ref,
259      * message-destination-ref, env-ref, resource-env-ref
260      * or service-ref element in the deployment descriptor.
261      */

262     protected static void addResource(Context context, Resource annotation) {
263         
264         if (annotation.type().getCanonicalName().equals("java.lang.String") ||
265                 annotation.type().getCanonicalName().equals("java.lang.Character") ||
266                 annotation.type().getCanonicalName().equals("java.lang.Integer") ||
267                 annotation.type().getCanonicalName().equals("java.lang.Boolean") ||
268                 annotation.type().getCanonicalName().equals("java.lang.Double") ||
269                 annotation.type().getCanonicalName().equals("java.lang.Byte") ||
270                 annotation.type().getCanonicalName().equals("java.lang.Short") ||
271                 annotation.type().getCanonicalName().equals("java.lang.Long") ||
272                 annotation.type().getCanonicalName().equals("java.lang.Float")) {
273             
274             // env-ref element
275
ContextEnvironment resource = new ContextEnvironment();
276             
277             resource.setName(annotation.name());
278             resource.setType(annotation.type().getCanonicalName());
279             
280             resource.setDescription(annotation.description());
281             
282             resource.setValue(annotation.mappedName());
283             
284             context.getNamingResources().addEnvironment(resource);
285             
286         } else if (annotation.type().getCanonicalName().equals("javax.xml.rpc.Service")) {
287             
288             // service-ref element
289
ContextService service = new ContextService();
290             
291             service.setName(annotation.name());
292             service.setWsdlfile(annotation.mappedName());
293             
294             service.setType(annotation.type().getCanonicalName());
295             service.setDescription(annotation.description());
296             
297             context.getNamingResources().addService(service);
298             
299         } else if (annotation.type().getCanonicalName().equals("javax.sql.DataSource") ||
300                 annotation.type().getCanonicalName().equals("javax.jms.ConnectionFactory") ||
301                 annotation.type().getCanonicalName()
302                 .equals("javax.jms.QueueConnectionFactory") ||
303                 annotation.type().getCanonicalName()
304                 .equals("javax.jms.TopicConnectionFactory") ||
305                 annotation.type().getCanonicalName().equals("javax.mail.Session") ||
306                 annotation.type().getCanonicalName().equals("java.net.URL") ||
307                 annotation.type().getCanonicalName()
308                 .equals("javax.resource.cci.ConnectionFactory") ||
309                 annotation.type().getCanonicalName().equals("org.omg.CORBA_2_3.ORB") ||
310                 annotation.type().getCanonicalName().endsWith("ConnectionFactory")) {
311             
312             // resource-ref element
313
ContextResource resource = new ContextResource();
314             
315             resource.setName(annotation.name());
316             resource.setType(annotation.type().getCanonicalName());
317             
318             if (annotation.authenticationType()
319                     == Resource.AuthenticationType.CONTAINER) {
320                 resource.setAuth("Container");
321             }
322             else if (annotation.authenticationType()
323                     == Resource.AuthenticationType.APPLICATION) {
324                 resource.setAuth("Application");
325             }
326             
327             resource.setScope(annotation.shareable() ? "Shareable" : "Unshareable");
328             resource.setProperty("mappedName", annotation.mappedName());
329             resource.setDescription(annotation.description());
330             
331             context.getNamingResources().addResource(resource);
332             
333         } else if (annotation.type().getCanonicalName().equals("javax.jms.Queue") ||
334                 annotation.type().getCanonicalName().equals("javax.jms.Topic")) {
335             
336             // message-destination-ref
337
MessageDestinationRef resource = new MessageDestinationRef();
338             
339             resource.setName(annotation.name());
340             resource.setType(annotation.type().getCanonicalName());
341             
342             resource.setUsage(annotation.mappedName());
343             resource.setDescription(annotation.description());
344             
345             context.getNamingResources().addMessageDestinationRef(resource);
346             
347         } else if (annotation.type().getCanonicalName()
348                 .equals("javax.resource.cci.InteractionSpec") ||
349                 annotation.type().getCanonicalName()
350                 .equals("javax.transaction.UserTransaction") ||
351                 true) {
352             
353             // resource-env-ref
354
ContextResourceEnvRef resource = new ContextResourceEnvRef();
355             
356             resource.setName(annotation.name());
357             resource.setType(annotation.type().getCanonicalName());
358             
359             resource.setProperty("mappedName", annotation.mappedName());
360             resource.setDescription(annotation.description());
361             
362             context.getNamingResources().addResourceEnvRef(resource);
363             
364         }
365         
366         
367     }
368     
369     
370 }
371
Popular Tags