1   //========================================================================
2   //$Id: RunAsCollection.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 java.util.HashMap;
19  
20  import javax.servlet.ServletException;
21  
22  import org.mortbay.jetty.servlet.ServletHolder;
23  import org.mortbay.log.Log;
24  
25  
26  /**
27   * RunAsCollection
28   *
29   *
30   */
31  public class RunAsCollection
32  {
33      private HashMap _runAsMap = new HashMap();//map of classname to run-as
34      
35      
36      public void add (RunAs runAs)
37      {
38          if ((runAs==null) || (runAs.getTargetClass()==null)) 
39              return;
40          
41          if (Log.isDebugEnabled())
42              Log.debug("Adding run-as for class="+runAs.getTargetClass());
43          _runAsMap.put(runAs.getTargetClass().getName(), runAs);
44      }
45  
46      public RunAs getRunAs (Object o)
47      throws ServletException
48      {
49          if (o==null)
50              return null;
51          
52          if (!(o instanceof ServletHolder))
53              return null;
54  
55          ServletHolder holder = (ServletHolder)o;
56  
57          String className = RunAs.getServletClassNameForHolder(holder);
58          return (RunAs)_runAsMap.get(className);
59      }
60      
61      public void setRunAs (Object o)
62      throws ServletException
63      {
64          if (o==null)
65              return;
66          
67          if (!(o instanceof ServletHolder))
68              return;
69  
70          ServletHolder holder = (ServletHolder)o;
71  
72          String className = RunAs.getServletClassNameForHolder(holder);
73          RunAs runAs = (RunAs)_runAsMap.get(className);
74          if (runAs == null)
75              return;
76  
77          runAs.setRunAs(holder); 
78      }
79  
80  }