1 22 package org.jboss.invocation.local; 23 24 import java.net.InetAddress ; 25 import java.security.AccessController ; 26 import java.security.PrivilegedAction ; 27 import java.security.PrivilegedExceptionAction ; 28 import java.security.PrivilegedActionException ; 29 import javax.management.ObjectName ; 30 import javax.naming.InitialContext ; 31 import javax.transaction.TransactionManager ; 32 33 import org.jboss.invocation.Invocation; 34 import org.jboss.invocation.Invoker; 35 import org.jboss.invocation.InvokerInterceptor; 36 import org.jboss.mx.util.JMXExceptionDecoder; 37 import org.jboss.proxy.TransactionInterceptor; 38 import org.jboss.system.Registry; 39 import org.jboss.system.ServiceMBeanSupport; 40 41 49 public class LocalInvoker 50 extends ServiceMBeanSupport 51 implements Invoker, LocalInvokerMBean 52 { 53 private MBeanServerAction serverAction = new MBeanServerAction(); 54 55 protected void createService() throws Exception 56 { 57 InvokerInterceptor.setLocal(this); 64 65 Registry.bind(serviceName, this); 66 } 67 68 protected void startService() throws Exception 69 { 70 InitialContext ctx = new InitialContext (); 71 try 72 { 73 74 77 TransactionManager tm = (TransactionManager ) ctx.lookup("java:/TransactionManager"); 78 TransactionInterceptor.setTransactionManager(tm); 79 } 80 finally 81 { 82 ctx.close(); 83 } 84 85 log.debug("Local invoker for JMX node started"); 86 } 87 88 protected void destroyService() 89 { 90 Registry.unbind(serviceName); 91 } 92 93 95 public String getServerHostName() 96 { 97 try 98 { 99 return InetAddress.getLocalHost().getHostName(); 100 } 101 catch (Exception ignored) 102 { 103 return null; 104 } 105 } 106 107 110 public Object invoke(Invocation invocation) throws Exception 111 { 112 ClassLoader oldCl = TCLAction.UTIL.getContextClassLoader(); 113 114 ObjectName mbean = (ObjectName ) Registry.lookup((Integer ) invocation.getObjectName()); 115 try 116 { 117 Object [] args = {invocation}; 118 Object rtnValue = serverAction.invoke(mbean, "invoke", args, 119 Invocation.INVOKE_SIGNATURE); 120 return rtnValue; 121 } 122 catch (Exception e) 123 { 124 e = (Exception ) JMXExceptionDecoder.decode(e); 125 if (log.isTraceEnabled()) 126 log.trace("Failed to invoke on mbean: " + mbean, e); 127 throw e; 128 } 129 finally 130 { 131 TCLAction.UTIL.setContextClassLoader(oldCl); 132 } 133 } 134 135 138 class MBeanServerAction implements PrivilegedExceptionAction 139 { 140 private ObjectName target; 141 String method; 142 Object [] args; 143 String [] sig; 144 145 MBeanServerAction() 146 { 147 } 148 MBeanServerAction(ObjectName target, String method, Object [] args, String [] sig) 149 { 150 this.target = target; 151 this.method = method; 152 this.args = args; 153 this.sig = sig; 154 } 155 156 public Object run() throws Exception 157 { 158 Object rtnValue = server.invoke(target, method, args, sig); 159 return rtnValue; 160 } 161 Object invoke(ObjectName target, String method, Object [] args, String [] sig) 162 throws Exception 163 { 164 SecurityManager sm = System.getSecurityManager(); 165 Object rtnValue = null; 166 if( sm == null ) 167 { 168 rtnValue = server.invoke(target, method, args, sig); 170 } 171 else 172 { 173 try 174 { 175 MBeanServerAction action = new MBeanServerAction(target, method, args, sig); 177 rtnValue = AccessController.doPrivileged(action); 178 } 179 catch (PrivilegedActionException e) 180 { 181 Exception ex = e.getException(); 182 throw ex; 183 } 184 } 185 return rtnValue; 186 } 187 } 188 189 interface TCLAction 190 { 191 class UTIL 192 { 193 static TCLAction getTCLAction() 194 { 195 return System.getSecurityManager() == null ? NON_PRIVILEGED : PRIVILEGED; 196 } 197 198 static ClassLoader getContextClassLoader() 199 { 200 return getTCLAction().getContextClassLoader(); 201 } 202 203 static ClassLoader getContextClassLoader(Thread thread) 204 { 205 return getTCLAction().getContextClassLoader(thread); 206 } 207 208 static void setContextClassLoader(ClassLoader cl) 209 { 210 getTCLAction().setContextClassLoader(cl); 211 } 212 213 static void setContextClassLoader(Thread thread, ClassLoader cl) 214 { 215 getTCLAction().setContextClassLoader(thread, cl); 216 } 217 } 218 219 TCLAction NON_PRIVILEGED = new TCLAction() 220 { 221 public ClassLoader getContextClassLoader() 222 { 223 return Thread.currentThread().getContextClassLoader(); 224 } 225 226 public ClassLoader getContextClassLoader(Thread thread) 227 { 228 return thread.getContextClassLoader(); 229 } 230 231 public void setContextClassLoader(ClassLoader cl) 232 { 233 Thread.currentThread().setContextClassLoader(cl); 234 } 235 236 public void setContextClassLoader(Thread thread, ClassLoader cl) 237 { 238 thread.setContextClassLoader(cl); 239 } 240 }; 241 242 TCLAction PRIVILEGED = new TCLAction() 243 { 244 private final PrivilegedAction getTCLPrivilegedAction = new PrivilegedAction () 245 { 246 public Object run() 247 { 248 return Thread.currentThread().getContextClassLoader(); 249 } 250 }; 251 252 public ClassLoader getContextClassLoader() 253 { 254 return (ClassLoader )AccessController.doPrivileged(getTCLPrivilegedAction); 255 } 256 257 public ClassLoader getContextClassLoader(final Thread thread) 258 { 259 return (ClassLoader )AccessController.doPrivileged(new PrivilegedAction () 260 { 261 public Object run() 262 { 263 return thread.getContextClassLoader(); 264 } 265 }); 266 } 267 268 public void setContextClassLoader(final ClassLoader cl) 269 { 270 AccessController.doPrivileged( 271 new PrivilegedAction () 272 { 273 public Object run() 274 { 275 Thread.currentThread().setContextClassLoader(cl); 276 return null; 277 } 278 } 279 ); 280 } 281 282 public void setContextClassLoader(final Thread thread, final ClassLoader cl) 283 { 284 AccessController.doPrivileged( 285 new PrivilegedAction () 286 { 287 public Object run() 288 { 289 thread.setContextClassLoader(cl); 290 return null; 291 } 292 } 293 ); 294 } 295 }; 296 297 ClassLoader getContextClassLoader(); 298 299 ClassLoader getContextClassLoader(Thread thread); 300 301 void setContextClassLoader(ClassLoader cl); 302 303 void setContextClassLoader(Thread thread, ClassLoader cl); 304 } 305 } 306 307 | Popular Tags |