View Javadoc

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