001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.catalina.startup;
019:
020: import javax.annotation.Resource;
021: import javax.annotation.Resources;
022: import javax.annotation.security.DeclareRoles;
023: import javax.annotation.security.RunAs;
024:
025: import org.apache.catalina.Container;
026: import org.apache.catalina.Context;
027: import org.apache.catalina.core.StandardWrapper;
028: import org.apache.catalina.deploy.ContextEnvironment;
029: import org.apache.catalina.deploy.ContextResource;
030: import org.apache.catalina.deploy.ContextResourceEnvRef;
031: import org.apache.catalina.deploy.ContextService;
032: import org.apache.catalina.deploy.FilterDef;
033: import org.apache.catalina.deploy.MessageDestinationRef;
034:
035: /**
036: * <p><strong>AnnotationSet</strong> for processing the annotations of the web application
037: * classes (<code>/WEB-INF/classes</code> and <code>/WEB-INF/lib</code>).</p>
038: *
039: * @author Fabien Carrion
040: * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
041: */
042:
043: public class WebAnnotationSet {
044:
045: // --------------------------------------------------------- Public Methods
046:
047: /**
048: * Process the annotations on a context.
049: */
050: public static void loadApplicationAnnotations(Context context) {
051:
052: loadApplicationListenerAnnotations(context);
053: loadApplicationFilterAnnotations(context);
054: loadApplicationServletAnnotations(context);
055:
056: }
057:
058: // -------------------------------------------------------- protected Methods
059:
060: /**
061: * Process the annotations for the listeners.
062: */
063: protected static void loadApplicationListenerAnnotations(
064: Context context) {
065: String[] applicationListeners = context
066: .findApplicationListeners();
067: for (int i = 0; i < applicationListeners.length; i++) {
068: loadClassAnnotation(context, applicationListeners[i]);
069: }
070: }
071:
072: /**
073: * Process the annotations for the filters.
074: */
075: protected static void loadApplicationFilterAnnotations(
076: Context context) {
077: FilterDef[] filterDefs = context.findFilterDefs();
078: for (int i = 0; i < filterDefs.length; i++) {
079: loadClassAnnotation(context, (filterDefs[i])
080: .getFilterClass());
081: }
082: }
083:
084: /**
085: * Process the annotations for the servlets.
086: */
087: protected static void loadApplicationServletAnnotations(
088: Context context) {
089:
090: ClassLoader classLoader = context.getLoader().getClassLoader();
091: StandardWrapper wrapper = null;
092: Class classClass = null;
093:
094: Container[] children = context.findChildren();
095: for (int i = 0; i < children.length; i++) {
096: if (children[i] instanceof StandardWrapper) {
097:
098: wrapper = (StandardWrapper) children[i];
099: if (wrapper.getServletClass() == null) {
100: continue;
101: }
102:
103: try {
104: classClass = classLoader.loadClass(wrapper
105: .getServletClass());
106: } catch (ClassNotFoundException e) {
107: // We do nothing
108: } catch (NoClassDefFoundError e) {
109: // We do nothing
110: }
111:
112: if (classClass == null) {
113: continue;
114: }
115:
116: loadClassAnnotation(context, wrapper.getServletClass());
117: /* Process RunAs annotation which can be only on servlets.
118: * Ref JSR 250, equivalent to the run-as element in
119: * the deployment descriptor
120: */
121: if (classClass.isAnnotationPresent(RunAs.class)) {
122: RunAs annotation = (RunAs) classClass
123: .getAnnotation(RunAs.class);
124: wrapper.setRunAs(annotation.value());
125: }
126: }
127: }
128:
129: }
130:
131: /**
132: * Process the annotations on a context for a given className.
133: */
134: protected static void loadClassAnnotation(Context context,
135: String fileString) {
136:
137: ClassLoader classLoader = context.getLoader().getClassLoader();
138: Class classClass = null;
139:
140: try {
141: classClass = classLoader.loadClass(fileString);
142: } catch (ClassNotFoundException e) {
143: // We do nothing
144: } catch (NoClassDefFoundError e) {
145: // We do nothing
146: }
147:
148: if (classClass == null) {
149: return;
150: }
151:
152: // Initialize the annotations
153:
154: if (classClass.isAnnotationPresent(Resource.class)) {
155: Resource annotation = (Resource) classClass
156: .getAnnotation(Resource.class);
157: addResource(context, annotation);
158: }
159: /* Process Resources annotation.
160: * Ref JSR 250
161: */
162: if (classClass.isAnnotationPresent(Resources.class)) {
163: Resources annotation = (Resources) classClass
164: .getAnnotation(Resources.class);
165: for (int i = 0; annotation.value() != null
166: && i < annotation.value().length; i++) {
167: addResource(context, annotation.value()[i]);
168: }
169: }
170: /* Process EJB annotation.
171: * Ref JSR 224, equivalent to the ejb-ref or ejb-local-ref
172: * element in the deployment descriptor.
173: if (classClass.isAnnotationPresent(EJB.class)) {
174: EJB annotation = (EJB)
175: classClass.getAnnotation(EJB.class);
176:
177: if ((annotation.mappedName().length() == 0) ||
178: annotation.mappedName().equals("Local")) {
179:
180: ContextLocalEjb ejb = new ContextLocalEjb();
181:
182: ejb.setName(annotation.name());
183: ejb.setType(annotation.beanInterface().getCanonicalName());
184: ejb.setDescription(annotation.description());
185:
186: ejb.setHome(annotation.beanName());
187:
188: context.getNamingResources().addLocalEjb(ejb);
189:
190: } else if (annotation.mappedName().equals("Remote")) {
191:
192: ContextEjb ejb = new ContextEjb();
193:
194: ejb.setName(annotation.name());
195: ejb.setType(annotation.beanInterface().getCanonicalName());
196: ejb.setDescription(annotation.description());
197:
198: ejb.setHome(annotation.beanName());
199:
200: context.getNamingResources().addEjb(ejb);
201:
202: }
203:
204: }
205: */
206: /* Process WebServiceRef annotation.
207: * Ref JSR 224, equivalent to the service-ref element in
208: * the deployment descriptor.
209: * The service-ref registration is not implemented
210: if (classClass.isAnnotationPresent(WebServiceRef.class)) {
211: WebServiceRef annotation = (WebServiceRef)
212: classClass.getAnnotation(WebServiceRef.class);
213:
214: ContextService service = new ContextService();
215:
216: service.setName(annotation.name());
217: service.setWsdlfile(annotation.wsdlLocation());
218:
219: service.setType(annotation.type().getCanonicalName());
220:
221: if (annotation.value() == null)
222: service.setServiceinterface(annotation.type().getCanonicalName());
223:
224: if (annotation.type().getCanonicalName().equals("Service"))
225: service.setServiceinterface(annotation.type().getCanonicalName());
226:
227: if (annotation.value().getCanonicalName().equals("Endpoint"))
228: service.setServiceendpoint(annotation.type().getCanonicalName());
229:
230: service.setPortlink(annotation.type().getCanonicalName());
231:
232: context.getNamingResources().addService(service);
233:
234:
235: }
236: */
237: /* Process DeclareRoles annotation.
238: * Ref JSR 250, equivalent to the security-role element in
239: * the deployment descriptor
240: */
241: if (classClass.isAnnotationPresent(DeclareRoles.class)) {
242: DeclareRoles annotation = (DeclareRoles) classClass
243: .getAnnotation(DeclareRoles.class);
244: for (int i = 0; annotation.value() != null
245: && i < annotation.value().length; i++) {
246: context.addSecurityRole(annotation.value()[i]);
247: }
248: }
249:
250: }
251:
252: /**
253: * Process a Resource annotation to set up a Resource.
254: * Ref JSR 250, equivalent to the resource-ref,
255: * message-destination-ref, env-ref, resource-env-ref
256: * or service-ref element in the deployment descriptor.
257: */
258: protected static void addResource(Context context,
259: Resource annotation) {
260:
261: if (annotation.type().getCanonicalName().equals(
262: "java.lang.String")
263: || annotation.type().getCanonicalName().equals(
264: "java.lang.Character")
265: || annotation.type().getCanonicalName().equals(
266: "java.lang.Integer")
267: || annotation.type().getCanonicalName().equals(
268: "java.lang.Boolean")
269: || annotation.type().getCanonicalName().equals(
270: "java.lang.Double")
271: || annotation.type().getCanonicalName().equals(
272: "java.lang.Byte")
273: || annotation.type().getCanonicalName().equals(
274: "java.lang.Short")
275: || annotation.type().getCanonicalName().equals(
276: "java.lang.Long")
277: || annotation.type().getCanonicalName().equals(
278: "java.lang.Float")) {
279:
280: // env-ref element
281: ContextEnvironment resource = new ContextEnvironment();
282:
283: resource.setName(annotation.name());
284: resource.setType(annotation.type().getCanonicalName());
285:
286: resource.setDescription(annotation.description());
287:
288: resource.setValue(annotation.mappedName());
289:
290: context.getNamingResources().addEnvironment(resource);
291:
292: } else if (annotation.type().getCanonicalName().equals(
293: "javax.xml.rpc.Service")) {
294:
295: // service-ref element
296: ContextService service = new ContextService();
297:
298: service.setName(annotation.name());
299: service.setWsdlfile(annotation.mappedName());
300:
301: service.setType(annotation.type().getCanonicalName());
302: service.setDescription(annotation.description());
303:
304: context.getNamingResources().addService(service);
305:
306: } else if (annotation.type().getCanonicalName().equals(
307: "javax.sql.DataSource")
308: || annotation.type().getCanonicalName().equals(
309: "javax.jms.ConnectionFactory")
310: || annotation.type().getCanonicalName().equals(
311: "javax.jms.QueueConnectionFactory")
312: || annotation.type().getCanonicalName().equals(
313: "javax.jms.TopicConnectionFactory")
314: || annotation.type().getCanonicalName().equals(
315: "javax.mail.Session")
316: || annotation.type().getCanonicalName().equals(
317: "java.net.URL")
318: || annotation.type().getCanonicalName().equals(
319: "javax.resource.cci.ConnectionFactory")
320: || annotation.type().getCanonicalName().equals(
321: "org.omg.CORBA_2_3.ORB")
322: || annotation.type().getCanonicalName().endsWith(
323: "ConnectionFactory")) {
324:
325: // resource-ref element
326: ContextResource resource = new ContextResource();
327:
328: resource.setName(annotation.name());
329: resource.setType(annotation.type().getCanonicalName());
330:
331: if (annotation.authenticationType() == Resource.AuthenticationType.CONTAINER) {
332: resource.setAuth("Container");
333: } else if (annotation.authenticationType() == Resource.AuthenticationType.APPLICATION) {
334: resource.setAuth("Application");
335: }
336:
337: resource.setScope(annotation.shareable() ? "Shareable"
338: : "Unshareable");
339: resource.setProperty("mappedName", annotation.mappedName());
340: resource.setDescription(annotation.description());
341:
342: context.getNamingResources().addResource(resource);
343:
344: } else if (annotation.type().getCanonicalName().equals(
345: "javax.jms.Queue")
346: || annotation.type().getCanonicalName().equals(
347: "javax.jms.Topic")) {
348:
349: // message-destination-ref
350: MessageDestinationRef resource = new MessageDestinationRef();
351:
352: resource.setName(annotation.name());
353: resource.setType(annotation.type().getCanonicalName());
354:
355: resource.setUsage(annotation.mappedName());
356: resource.setDescription(annotation.description());
357:
358: context.getNamingResources().addMessageDestinationRef(
359: resource);
360:
361: } else if (annotation.type().getCanonicalName().equals(
362: "javax.resource.cci.InteractionSpec")
363: || annotation.type().getCanonicalName().equals(
364: "javax.transaction.UserTransaction") || true) {
365:
366: // resource-env-ref
367: ContextResourceEnvRef resource = new ContextResourceEnvRef();
368:
369: resource.setName(annotation.name());
370: resource.setType(annotation.type().getCanonicalName());
371:
372: resource.setProperty("mappedName", annotation.mappedName());
373: resource.setDescription(annotation.description());
374:
375: context.getNamingResources().addResourceEnvRef(resource);
376:
377: }
378:
379: }
380:
381: }
|