1   //========================================================================
2   //$Id: RunAs.java 3353 2008-07-22 10:39:41Z janb $
3   //Copyright 2006 Mort Bay Consulting Pty. Ltd.
4   //------------------------------------------------------------------------
5   //Licensed under the Apache License, Version 2.0 (the "License");
6   //you may not use this file except in compliance with the License.
7   //You may obtain a copy of the License at 
8   //http://www.apache.org/licenses/LICENSE-2.0
9   //Unless required by applicable law or agreed to in writing, software
10  //distributed under the License is distributed on an "AS IS" BASIS,
11  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  //See the License for the specific language governing permissions and
13  //limitations under the License.
14  //========================================================================
15  
16  package org.mortbay.jetty.plus.annotation;
17  
18  import javax.servlet.Servlet;
19  import javax.servlet.ServletException;
20  
21  import org.mortbay.jetty.servlet.ServletHolder;
22  
23  /**
24   * RunAs
25   *
26   * Represents a <run-as> element in web.xml, or a runAs annotation.
27   */
28  public class RunAs
29  {
30      private Class _targetClass;
31      private String _roleName;
32      
33      public RunAs()
34      {}
35      
36      
37      public void setTargetClass (Class clazz)
38      {
39          _targetClass=clazz;
40      }
41  
42      public Class getTargetClass ()
43      {
44          return _targetClass;
45      }
46      
47      public void setRoleName (String roleName)
48      {
49          _roleName = roleName;
50      }
51      
52      public String getRoleName ()
53      {
54          return _roleName;
55      }
56      
57      
58      public void setRunAs (ServletHolder holder)
59      throws ServletException
60      {
61          if (holder==null)
62              return;
63          String className = getServletClassNameForHolder(holder);
64  
65          if (className.equals(_targetClass.getName()))
66              holder.setRunAs(_roleName); 
67      }
68      
69      public static String getServletClassNameForHolder (ServletHolder holder)
70      throws ServletException
71      {
72          if (PojoServlet.class.getName().equals(holder.getClassName()))
73              return ((PojoWrapper)holder.getServlet()).getPojo().getClass().getName();
74          return holder.getClassName();
75      }
76     
77  }