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 java.util.List;
17  
18  import javax.naming.NamingException;
19  
20  import org.eclipse.jetty.annotations.AnnotationParser.AnnotationHandler;
21  import org.eclipse.jetty.annotations.AnnotationParser.ListValue;
22  import org.eclipse.jetty.annotations.AnnotationParser.Value;
23  import org.eclipse.jetty.util.log.Log;
24  import org.eclipse.jetty.webapp.WebAppContext;
25  
26  public class ResourcesAnnotationHandler implements AnnotationHandler
27  {
28  
29      protected WebAppContext _wac;
30  
31      public ResourcesAnnotationHandler (WebAppContext wac)
32      {
33          _wac = wac;
34      }
35      
36      public void handleClass(String className, int version, int access, String signature, String superName, String[] interfaces, String annotation,
37                              List<Value> values)
38      {
39          if (values != null && values.size() == 1)
40          {
41              List<ListValue> list = (List<ListValue>)(values.get(0).getValue());
42              for (ListValue resource : list)
43              {
44                  List<Value> resourceValues = resource.getList();
45                  String name = null;
46                  String mappedName = null;
47                  for (Value v:resourceValues)
48                  {
49                      if ("name".equals(v.getName()))
50                          name = (String)v.getValue();
51                      else if ("mappedName".equals(v.getName()))
52                          mappedName = (String)v.getValue();
53                  }
54                  if (name == null)
55                      Log.warn ("Skipping Resources(Resource) annotation with no name on class "+className);
56                  else
57                  {
58                      try
59                      {
60                          //TODO don't ignore the shareable, auth etc etc
61                          if (!org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(_wac, name, mappedName))
62                              if (!org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(_wac.getServer(), name, mappedName))
63                                 Log.warn("Skipping Resources(Resource) annotation on "+className+" for name "+name+": No resource bound at "+(mappedName==null?name:mappedName));
64                      }
65                      catch (NamingException e)
66                      {
67                          Log.warn(e);
68                      }
69                  } 
70              }
71          }
72          else
73          {
74              Log.warn("Skipping empty or incorrect Resources annotation on "+className);
75          }
76      }
77  
78      public void handleField(String className, String fieldName, int access, String fieldType, String signature, Object value, String annotation,
79                              List<Value> values)
80      {
81          Log.warn ("@Resources not applicable for fields: "+className+"."+fieldName);
82      }
83  
84      public void handleMethod(String className, String methodName, int access, String params, String signature, String[] exceptions, String annotation,
85                               List<Value> values)
86      {
87          Log.warn ("@Resources not applicable for methods: "+className+"."+methodName);
88      }
89  
90  }