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.webapp.WebAppContext;
23  
24  public class ResourcesAnnotationHandler extends AbstractIntrospectableAnnotationHandler
25  {
26  
27      protected WebAppContext _wac;
28  
29      public ResourcesAnnotationHandler (WebAppContext wac)
30      {
31          super(true);
32          _wac = wac;
33      }
34      
35      public void doHandle (Class clazz)
36      {
37          Resources resources = (Resources)clazz.getAnnotation(Resources.class);
38          if (resources != null)
39          {
40              Resource[] resArray = resources.value();
41              if (resArray==null||resArray.length==0)
42              {
43                  Log.warn ("Skipping empty or incorrect Resources annotation on "+clazz.getName());
44                  return;
45              }
46  
47              for (int j=0;j<resArray.length;j++)
48              {
49  
50                  String name = resArray[j].name();
51                  String mappedName = resArray[j].mappedName();
52                  Resource.AuthenticationType auth = resArray[j].authenticationType();
53                  Class type = resArray[j].type();
54                  boolean shareable = resArray[j].shareable();
55  
56                  if (name==null || name.trim().equals(""))
57                      throw new IllegalStateException ("Class level Resource annotations must contain a name (Common Annotations Spec Section 2.3)");
58  
59                  try
60                  {
61                      //TODO don't ignore the shareable, auth etc etc
62  
63                      if (!org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(_wac, name, mappedName))
64                          if (!org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(_wac.getServer(), name, mappedName))
65                              Log.warn("Skipping Resources(Resource) annotation on "+clazz.getName()+" for name "+name+": No resource bound at "+(mappedName==null?name:mappedName));
66                  }
67                  catch (NamingException e)
68                  {
69                      Log.warn(e);
70                  }
71              }
72          }
73      }    
74  
75  }