1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.annotations;
15
16 import javax.annotation.Resource;
17 import javax.annotation.Resources;
18 import javax.naming.NamingException;
19
20 import org.eclipse.jetty.annotations.AnnotationIntrospector.AbstractIntrospectableAnnotationHandler;
21 import org.eclipse.jetty.util.log.Log;
22 import org.eclipse.jetty.util.log.Logger;
23 import org.eclipse.jetty.webapp.WebAppContext;
24
25 public class ResourcesAnnotationHandler extends AbstractIntrospectableAnnotationHandler
26 {
27 private static final Logger LOG = Log.getLogger(ResourcesAnnotationHandler.class);
28
29
30 protected WebAppContext _wac;
31
32 public ResourcesAnnotationHandler (WebAppContext wac)
33 {
34 super(true);
35 _wac = wac;
36 }
37
38 public void doHandle (Class<?> clazz)
39 {
40 Resources resources = (Resources)clazz.getAnnotation(Resources.class);
41 if (resources != null)
42 {
43 Resource[] resArray = resources.value();
44 if (resArray==null||resArray.length==0)
45 {
46 LOG.warn ("Skipping empty or incorrect Resources annotation on "+clazz.getName());
47 return;
48 }
49
50 for (int j=0;j<resArray.length;j++)
51 {
52 String name = resArray[j].name();
53 String mappedName = resArray[j].mappedName();
54
55 if (name==null || name.trim().equals(""))
56 throw new IllegalStateException ("Class level Resource annotations must contain a name (Common Annotations Spec Section 2.3)");
57
58 try
59 {
60
61
62 if (!org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(_wac, name, mappedName))
63 if (!org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(_wac.getServer(), name, mappedName))
64 LOG.warn("Skipping Resources(Resource) annotation on "+clazz.getName()+" for name "+name+": No resource bound at "+(mappedName==null?name:mappedName));
65 }
66 catch (NamingException e)
67 {
68 LOG.warn(e);
69 }
70 }
71 }
72 }
73
74 }