View Javadoc

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