View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.annotations;
20  
21  import javax.annotation.Resource;
22  import javax.annotation.Resources;
23  import javax.naming.NamingException;
24  
25  import org.eclipse.jetty.annotations.AnnotationIntrospector.AbstractIntrospectableAnnotationHandler;
26  import org.eclipse.jetty.util.log.Log;
27  import org.eclipse.jetty.util.log.Logger;
28  import org.eclipse.jetty.webapp.WebAppContext;
29  
30  public class ResourcesAnnotationHandler extends AbstractIntrospectableAnnotationHandler
31  {
32      private static final Logger LOG = Log.getLogger(ResourcesAnnotationHandler.class);
33  
34  
35      protected WebAppContext _wac;
36  
37      public ResourcesAnnotationHandler (WebAppContext wac)
38      {
39          super(true);
40          _wac = wac;
41      }
42      
43      public void doHandle (Class<?> clazz)
44      {
45          Resources resources = (Resources)clazz.getAnnotation(Resources.class);
46          if (resources != null)
47          {
48              Resource[] resArray = resources.value();
49              if (resArray==null||resArray.length==0)
50              {
51                  LOG.warn ("Skipping empty or incorrect Resources annotation on "+clazz.getName());
52                  return;
53              }
54  
55              for (int j=0;j<resArray.length;j++)
56              {
57                  String name = resArray[j].name();
58                  String mappedName = resArray[j].mappedName();
59  
60                  if (name==null || name.trim().equals(""))
61                      throw new IllegalStateException ("Class level Resource annotations must contain a name (Common Annotations Spec Section 2.3)");
62  
63                  try
64                  {
65                      //TODO don't ignore the shareable, auth etc etc
66  
67                      if (!org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(_wac, name, mappedName))
68                          if (!org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(_wac.getServer(), name, mappedName))
69                              LOG.warn("Skipping Resources(Resource) annotation on "+clazz.getName()+" for name "+name+": No resource bound at "+(mappedName==null?name:mappedName));
70                  }
71                  catch (NamingException e)
72                  {
73                      LOG.warn(e);
74                  }
75              }
76          }
77      }    
78  
79  }