View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.plus.annotation;
20  
21  import java.util.HashMap;
22  import javax.servlet.ServletException;
23  
24  import org.eclipse.jetty.servlet.ServletHolder;
25  import org.eclipse.jetty.util.log.Log;
26  import org.eclipse.jetty.util.log.Logger;
27  
28  
29  /**
30   * RunAsCollection
31   *
32   *
33   */
34  public class RunAsCollection
35  {
36      private static final Logger LOG = Log.getLogger(RunAsCollection.class);
37  
38      public static final String RUNAS_COLLECTION = "org.eclipse.jetty.runAsCollection";
39      private HashMap<String, RunAs> _runAsMap = new HashMap<String, RunAs>();//map of classname to run-as
40  
41  
42  
43      public void add (RunAs runAs)
44      {
45          if ((runAs==null) || (runAs.getTargetClassName()==null))
46              return;
47  
48          if (LOG.isDebugEnabled())
49              LOG.debug("Adding run-as for class="+runAs.getTargetClassName());
50          _runAsMap.put(runAs.getTargetClassName(), runAs);
51      }
52  
53      public RunAs getRunAs (Object o)
54      throws ServletException
55      {
56          if (o==null)
57              return null;
58  
59          return (RunAs)_runAsMap.get(o.getClass().getCanonicalName());
60      }
61  
62      public void setRunAs(Object o)
63      throws ServletException
64      {
65          if (o == null)
66              return;
67  
68          if (!ServletHolder.class.isAssignableFrom(o.getClass()))
69              return;
70  
71          RunAs runAs = (RunAs)_runAsMap.get(o.getClass().getName());
72          if (runAs == null)
73              return;
74  
75          runAs.setRunAs((ServletHolder)o);
76      }
77  
78  }