View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-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.plus.annotation;
15  
16  import java.util.HashMap;
17  
18  import javax.servlet.ServletException;
19  
20  import org.eclipse.jetty.servlet.ServletHolder;
21  import org.eclipse.jetty.util.log.Log;
22  
23  
24  /**
25   * RunAsCollection
26   *
27   *
28   */
29  public class RunAsCollection
30  {
31      public static final String RUNAS_COLLECTION = "org.eclipse.jetty.runAsCollection";
32      private HashMap<String, RunAs> _runAsMap = new HashMap<String, RunAs>();//map of classname to run-as
33    
34      
35      
36      public void add (RunAs runAs)
37      {
38          if ((runAs==null) || (runAs.getTargetClassName()==null)) 
39              return;
40          
41          if (Log.isDebugEnabled())
42              Log.debug("Adding run-as for class="+runAs.getTargetClassName());
43          _runAsMap.put(runAs.getTargetClassName(), runAs);
44      }
45  
46      public RunAs getRunAs (Object o)
47      throws ServletException
48      {
49          if (o==null)
50              return null;
51          
52          return (RunAs)_runAsMap.get(o.getClass().getCanonicalName());
53      }
54      
55      public void setRunAs(Object o)
56      throws ServletException
57      {
58          if (o == null)
59              return;
60          
61          if (!ServletHolder.class.isAssignableFrom(o.getClass()))
62              return;
63        
64          RunAs runAs = (RunAs)_runAsMap.get(o.getClass().getName());
65          if (runAs == null)
66              return;
67          
68          runAs.setRunAs((ServletHolder)o);
69      }
70  
71  }