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.annotation.security.RunAs;
19  
20  import org.eclipse.jetty.annotations.AnnotationParser.AnnotationHandler;
21  import org.eclipse.jetty.annotations.AnnotationParser.Value;
22  import org.eclipse.jetty.plus.annotation.RunAsCollection;
23  import org.eclipse.jetty.util.Loader;
24  import org.eclipse.jetty.util.log.Log;
25  import org.eclipse.jetty.webapp.WebAppContext;
26  
27  public class RunAsAnnotationHandler implements AnnotationHandler
28  {
29      protected WebAppContext _wac;
30  
31      public RunAsAnnotationHandler (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          RunAsCollection runAsCollection = (RunAsCollection)_wac.getAttribute(RunAsCollection.RUNAS_COLLECTION);
40          Class clazz = null;
41          try
42          {
43              clazz = Loader.loadClass(null, className);
44              if (!javax.servlet.Servlet.class.isAssignableFrom(clazz))
45              {
46                  Log.debug("@RunAs annotation ignored on on-servlet class "+clazz.getName());
47                  return;
48              }
49              RunAs runAs = (RunAs)clazz.getAnnotation(RunAs.class);
50              if (runAs != null)
51              {
52                  String role = runAs.value();
53                  if (role != null)
54                  {
55                      org.eclipse.jetty.plus.annotation.RunAs ra = new org.eclipse.jetty.plus.annotation.RunAs();
56                      ra.setTargetClass(clazz);
57                      ra.setRoleName(role);
58                      runAsCollection.add(ra);
59                  }
60              }
61          }
62          catch (Exception e)
63          {
64              Log.warn(e);
65          }
66        
67      }
68  
69      public void handleField(String className, String fieldName, int access, String fieldType, String signature, Object value, String annotation,
70                              List<Value> values)
71      {
72         Log.warn ("@RunAs annotation not applicable for fields: "+className+"."+fieldName);
73      }
74  
75      public void handleMethod(String className, String methodName, int access, String params, String signature, String[] exceptions, String annotation,
76                               List<Value> values)
77      {
78          Log.warn("@RunAs annotation ignored on method: "+className+"."+methodName+" "+signature);
79      }
80  
81  }