KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > kernel > plugins > dependency > KernelControllerContextAction


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.kernel.plugins.dependency;
23
24 import java.security.*;
25
26 import org.jboss.beans.metadata.spi.BeanMetaData;
27 import org.jboss.dependency.plugins.spi.action.ControllerContextAction;
28 import org.jboss.dependency.spi.ControllerContext;
29 import org.jboss.joinpoint.spi.Joinpoint;
30 import org.jboss.kernel.plugins.config.Configurator;
31 import org.jboss.kernel.spi.dependency.KernelControllerContext;
32 import org.jboss.logging.Logger;
33
34 /**
35  * KernelControllerContextAction.
36  *
37  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
38  * @version $Revision: 55931 $
39  */

40 public class KernelControllerContextAction implements ControllerContextAction
41 {
42    protected Logger log = Logger.getLogger(getClass());
43
44    /**
45     * Dispatch a joinpoint
46     *
47     * @param context the context
48     * @param joinpoint the joinpoint
49     * @return the result
50     * @throws Throwable for any error
51     */

52    protected static Object JavaDoc dispatchJoinPoint(final KernelControllerContext context, final Joinpoint joinpoint) throws Throwable JavaDoc
53    {
54       BeanMetaData metaData = context.getBeanMetaData();
55       ClassLoader JavaDoc cl = Configurator.getClassLoader(metaData);
56       AccessControlContext access = null;
57       if (context instanceof AbstractKernelControllerContext)
58       {
59          AbstractKernelControllerContext theContext = (AbstractKernelControllerContext) context;
60          access = theContext.getAccessControlContext();
61       }
62
63       // Dispatch with the bean class loader if it exists
64
ClassLoader JavaDoc tcl = Thread.currentThread().getContextClassLoader();
65       try
66       {
67          if( cl != null && access == null )
68             Thread.currentThread().setContextClassLoader(cl);
69          if (access == null)
70          {
71             return joinpoint.dispatch();
72          }
73          else
74          {
75             DispatchJoinPoint action = new DispatchJoinPoint(joinpoint);
76             try
77             {
78                return AccessController.doPrivileged(action, access);
79             }
80             catch (PrivilegedActionException e)
81             {
82                throw e.getCause();
83             }
84          }
85       }
86       finally
87       {
88          if( cl != null && access == null )
89             Thread.currentThread().setContextClassLoader(tcl);
90       }
91    }
92
93    public void install(final ControllerContext context) throws Throwable JavaDoc
94    {
95       if (System.getSecurityManager() == null || context instanceof AbstractKernelControllerContext == false)
96          installAction((KernelControllerContext) context);
97       else
98       {
99          PrivilegedExceptionAction<Object JavaDoc> action = new PrivilegedExceptionAction<Object JavaDoc>()
100          {
101             public Object JavaDoc run() throws Exception JavaDoc
102             {
103                try
104                {
105                   installAction((KernelControllerContext) context);
106                   return null;
107                }
108                catch (RuntimeException JavaDoc e)
109                {
110                   throw e;
111                }
112                catch (Exception JavaDoc e)
113                {
114                   throw e;
115                }
116                catch (Error JavaDoc e)
117                {
118                   throw e;
119                }
120                catch (Throwable JavaDoc t)
121                {
122                   throw new RuntimeException JavaDoc(t);
123                }
124             }
125          };
126          try
127          {
128             AccessController.doPrivileged(action);
129          }
130          catch (PrivilegedActionException e)
131          {
132             throw e.getCause();
133          }
134       }
135    }
136
137    public void uninstall(final ControllerContext context)
138    {
139       if (System.getSecurityManager() == null || context instanceof AbstractKernelControllerContext == false)
140          uninstallAction((KernelControllerContext) context);
141       else
142       {
143          PrivilegedAction<Object JavaDoc> action = new PrivilegedAction<Object JavaDoc>()
144          {
145             public Object JavaDoc run()
146             {
147                uninstallAction((KernelControllerContext) context);
148                return null;
149             }
150          };
151          AccessController.doPrivileged(action);
152       }
153    }
154    
155    public void installAction(KernelControllerContext context) throws Throwable JavaDoc
156    {
157    }
158
159    public void uninstallAction(KernelControllerContext context)
160    {
161    }
162 }
Popular Tags