KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > tomcat > security > RunAsListener


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.web.tomcat.security;
23
24 import org.apache.catalina.InstanceEvent;
25 import org.apache.catalina.InstanceListener;
26 import org.apache.catalina.Wrapper;
27 import org.jboss.logging.Logger;
28 import org.jboss.metadata.WebMetaData;
29 import org.jboss.security.RunAsIdentity;
30
31 /**
32  * An InstanceListener used to push/pop the servlet run-as identity for the
33  * init/destroy lifecycle events.
34  *
35  * @author Scott.Stark@jboss.org
36  * @version $Revision: 37459 $
37  */

38 public class RunAsListener implements InstanceListener
39 {
40    /** There is no api to install an initialized listener so the
41     * WebMetaData had to be passed via a thread local.
42     */

43    public static ThreadLocal JavaDoc metaDataLocal = new ThreadLocal JavaDoc();
44
45    private static Logger log = Logger.getLogger(RunAsListener.class);
46    private WebMetaData metaData;
47
48    public RunAsListener()
49    {
50       this.metaData = (WebMetaData) metaDataLocal.get();
51    }
52
53    /**
54     * Push the run-as identity on the before init/destroy, pop it on the
55     * after init/destroy events.
56     *
57     * @param event - the type of instance event
58     */

59    public void instanceEvent(InstanceEvent event)
60    {
61       Wrapper servlet = event.getWrapper();
62       String JavaDoc type = event.getType();
63       if (servlet != null && metaData != null)
64       {
65          boolean trace = log.isTraceEnabled();
66          String JavaDoc name = servlet.getName();
67          RunAsIdentity identity = metaData.getRunAsIdentity(name);
68          if (trace)
69             log.trace(name + ", runAs: " + identity);
70          // Push the identity on the before init/destroy
71
if( type.equals(InstanceEvent.BEFORE_INIT_EVENT)
72             || type.equals(InstanceEvent.BEFORE_DESTROY_EVENT) )
73          {
74             SecurityAssociationActions.pushRunAsIdentity(identity);
75          }
76          // Pop the identity on the after init/destroy
77
else if( type.equals(InstanceEvent.AFTER_INIT_EVENT)
78             || type.equals(InstanceEvent.AFTER_DESTROY_EVENT) )
79          {
80             SecurityAssociationActions.popRunAsIdentity();
81          }
82       }
83    }
84 }
85
Popular Tags