View Javadoc

1   // ========================================================================
2   // Copyright (c) 2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
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  
53                  String name = resArray[j].name();
54                  String mappedName = resArray[j].mappedName();
55                  Resource.AuthenticationType auth = resArray[j].authenticationType();
56                  Class type = resArray[j].type();
57                  boolean shareable = resArray[j].shareable();
58  
59                  if (name==null || name.trim().equals(""))
60                      throw new IllegalStateException ("Class level Resource annotations must contain a name (Common Annotations Spec Section 2.3)");
61  
62                  try
63                  {
64                      //TODO don't ignore the shareable, auth etc etc
65  
66                      if (!org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(_wac, name, mappedName))
67                          if (!org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(_wac.getServer(), name, mappedName))
68                              LOG.warn("Skipping Resources(Resource) annotation on "+clazz.getName()+" for name "+name+": No resource bound at "+(mappedName==null?name:mappedName));
69                  }
70                  catch (NamingException e)
71                  {
72                      LOG.warn(e);
73                  }
74              }
75          }
76      }    
77  
78  }