1 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 44 45 public class WebAnnotationSet { 46 47 48 50 51 54 public static void loadApplicationAnnotations(Context context) { 55 56 loadApplicationListenerAnnotations(context); 57 loadApplicationFilterAnnotations(context); 58 loadApplicationServletAnnotations(context); 59 60 61 } 62 63 64 66 67 70 protected static void loadApplicationListenerAnnotations(Context context) { 71 String [] applicationListeners = context.findApplicationListeners(); 72 for (int i = 0; i < applicationListeners.length; i++) { 73 loadClassAnnotation(context, applicationListeners[i]); 74 } 75 } 76 77 78 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 92 protected static void loadApplicationServletAnnotations(Context context) { 93 94 ClassLoader classLoader = context.getLoader().getClassLoader(); 95 StandardWrapper wrapper = null; 96 Class 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 e) { 110 } catch (NoClassDefFoundError e) { 112 } 114 115 if (classClass == null) { 116 continue; 117 } 118 119 loadClassAnnotation(context, wrapper.getServletClass()); 120 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 139 protected static void loadClassAnnotation(Context context, String fileString) { 140 141 ClassLoader classLoader = context.getLoader().getClassLoader(); 142 Class classClass = null; 143 144 try { 145 classClass = classLoader.loadClass(fileString); 146 } catch (ClassNotFoundException e) { 147 } catch (NoClassDefFoundError e) { 149 } 151 152 if (classClass == null) { 153 return; 154 } 155 156 158 if (classClass.isAnnotationPresent(Resource.class)) { 159 Resource annotation = (Resource) 160 classClass.getAnnotation(Resource.class); 161 addResource(context, annotation); 162 } 163 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 209 240 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 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 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 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 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 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 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 |