1 22 package org.jboss.proxy.ejb; 23 24 import java.security.Principal ; 25 import java.util.Map ; 26 import javax.ejb.HomeHandle ; 27 import javax.management.MBeanException ; 28 import javax.management.MBeanServer ; 29 import javax.management.ObjectName ; 30 import javax.transaction.Transaction ; 31 32 import org.omg.CORBA.BAD_OPERATION ; 33 import org.omg.CORBA.InterfaceDef ; 34 import org.omg.CORBA.ORBPackage.InvalidName ; 35 import org.omg.CORBA.portable.InvokeHandler ; 36 import org.omg.CORBA.portable.InputStream ; 37 import org.omg.CORBA.portable.OutputStream ; 38 import org.omg.CORBA.portable.ResponseHandler ; 39 import org.omg.PortableServer.POA ; 40 41 import org.jboss.iiop.CorbaORB; 42 import org.jboss.iiop.csiv2.SASCurrent; 43 import org.jboss.iiop.rmi.RmiIdlUtil; 44 import org.jboss.iiop.rmi.marshal.strategy.SkeletonStrategy; 45 import org.jboss.invocation.Invocation; 46 import org.jboss.invocation.InvocationKey; 47 import org.jboss.invocation.InvocationType; 48 import org.jboss.invocation.PayloadKey; 49 import org.jboss.invocation.iiop.ServantWithMBeanServer; 50 import org.jboss.logging.Logger; 51 import org.jboss.tm.iiop.TxServerInterceptor; 52 import org.jboss.security.SimplePrincipal; 53 54 64 public class EjbHomeCorbaServant 65 extends ServantWithMBeanServer 66 implements InvokeHandler , LocalIIOPInvoker { 67 68 71 private final ObjectName containerName; 72 73 76 private final ClassLoader containerClassLoader; 77 78 81 private final Map methodInvokerMap; 82 83 87 private final String [] repositoryIds; 88 89 92 private final InterfaceDef interfaceDef; 93 94 97 private final Logger logger; 98 99 102 private final boolean traceEnabled; 103 104 108 private HomeHandle homeHandle = null; 109 110 113 private MBeanServer mbeanServer; 114 115 119 private SASCurrent sasCurrent; 120 121 124 public EjbHomeCorbaServant(ObjectName containerName, 125 ClassLoader containerClassLoader, 126 Map methodInvokerMap, 127 String [] repositoryIds, 128 InterfaceDef interfaceDef, 129 Logger logger) 130 { 131 this.containerName = containerName; 132 this.containerClassLoader = containerClassLoader; 133 this.methodInvokerMap = methodInvokerMap; 134 this.repositoryIds = repositoryIds; 135 this.interfaceDef = interfaceDef; 136 this.logger = logger; 137 this.traceEnabled = logger.isTraceEnabled(); 138 try 139 { 140 this.sasCurrent = (SASCurrent) 141 CorbaORB.getInstance().resolve_initial_references("SASCurrent"); 142 } 143 catch (InvalidName invalidName) 144 { 145 this.sasCurrent = null; 146 } 147 } 148 149 public void setHomeHandle(HomeHandle homeHandle) 150 { 151 this.homeHandle = homeHandle; 152 } 153 154 156 159 public void setMBeanServer(MBeanServer mbeanServer) 160 { 161 this.mbeanServer = mbeanServer; 162 } 163 164 166 169 public org.omg.CORBA.Object _get_interface_def() 170 { 171 if (interfaceDef != null) 172 return interfaceDef; 173 else 174 return super._get_interface_def(); 175 } 176 177 179 183 public String [] _all_interfaces(POA poa, byte[] objectId) 184 { 185 return (String [])repositoryIds.clone(); 186 } 187 188 192 public OutputStream _invoke(String opName, 193 InputStream in, 194 ResponseHandler handler) 195 { 196 if (traceEnabled) { 197 logger.trace("EJBHome invocation: " + opName); 198 } 199 200 ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); 201 Thread.currentThread().setContextClassLoader(containerClassLoader); 202 203 try { 204 205 SkeletonStrategy op = (SkeletonStrategy) methodInvokerMap.get(opName); 206 if (op == null) { 207 logger.debug("Unable to find opname '" + opName + "' valid operations:" + methodInvokerMap.keySet()); 208 throw new BAD_OPERATION (opName); 209 } 210 211 org.omg.CORBA_2_3.portable.OutputStream out; 212 try { 213 Object retVal; 214 215 221 if (opName.equals("_get_homeHandle")) 222 { 223 retVal = homeHandle; 224 } 225 else 226 { 227 Transaction tx = TxServerInterceptor.getCurrentTransaction(); 228 SimplePrincipal principal = null; 229 char[] password = null; 230 if (sasCurrent != null) 231 { 232 byte[] username = sasCurrent.get_incoming_username(); 233 byte[] credential = sasCurrent.get_incoming_password(); 234 String name = new String (username, "UTF-8"); 235 int domainIndex = name.indexOf('@'); 236 if (domainIndex > 0) 237 name = name.substring(0, domainIndex); 238 if (name.length() == 0) 239 { 240 byte[] incomingName = 241 sasCurrent.get_incoming_principal_name(); 242 if (incomingName.length > 0) 243 { 244 name = new String (incomingName, "UTF-8"); 245 domainIndex = name.indexOf('@'); 246 if (domainIndex > 0) 247 name = name.substring(0, domainIndex); 248 principal = new SimplePrincipal(name); 249 password = name.toCharArray(); 252 } 253 } 254 else 255 { 256 principal = new SimplePrincipal(name); 257 password = new String (credential, "UTF-8").toCharArray(); 258 } 259 } 260 261 Object [] params = op.readParams( 262 (org.omg.CORBA_2_3.portable.InputStream )in); 263 Invocation inv = new Invocation(null, 264 op.getMethod(), 265 params, 266 tx, 267 principal, 268 password ); 269 inv.setValue(InvocationKey.INVOKER_PROXY_BINDING, 270 "iiop", 271 PayloadKey.AS_IS); 272 inv.setType(InvocationType.HOME); 273 retVal = mbeanServer.invoke(containerName, 274 "invoke", 275 new Object [] {inv}, 276 Invocation.INVOKE_SIGNATURE); 277 } 278 out = (org.omg.CORBA_2_3.portable.OutputStream ) 279 handler.createReply(); 280 if (op.isNonVoid()) { 281 op.writeRetval(out, retVal); 282 } 283 } 284 catch (Exception e) { 285 if (traceEnabled) { 286 logger.trace("Exception in EJBHome invocation", e); 287 } 288 if (e instanceof MBeanException ) { 289 e = ((MBeanException )e).getTargetException(); 290 } 291 RmiIdlUtil.rethrowIfCorbaSystemException(e); 292 out = (org.omg.CORBA_2_3.portable.OutputStream ) 293 handler.createExceptionReply(); 294 op.writeException(out, e); 295 } 296 return out; 297 } 298 finally { 299 Thread.currentThread().setContextClassLoader(oldCl); 300 } 301 } 302 303 305 309 public Object invoke(String opName, 310 Object [] arguments, 311 Transaction tx, 312 Principal identity, 313 Object credential) 314 throws Exception 315 { 316 if (traceEnabled) { 317 logger.trace("EJBHome local invocation: " + opName); 318 } 319 320 ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); 321 Thread.currentThread().setContextClassLoader(containerClassLoader); 322 323 try { 324 SkeletonStrategy op = 325 (SkeletonStrategy) methodInvokerMap.get(opName); 326 if (op == null) { 327 throw new BAD_OPERATION (opName); 328 } 329 330 Invocation inv = new Invocation(null, 331 op.getMethod(), 332 arguments, 333 tx, 334 null, 335 null ); 336 inv.setValue(InvocationKey.INVOKER_PROXY_BINDING, 337 "iiop", 338 PayloadKey.AS_IS); 339 inv.setType(InvocationType.HOME); 340 return mbeanServer.invoke(containerName, 341 "invoke", 342 new Object [] {inv}, 343 Invocation.INVOKE_SIGNATURE); 344 } 345 catch (MBeanException e) { 346 throw e.getTargetException(); 347 } 348 finally { 349 Thread.currentThread().setContextClassLoader(oldCl); 350 } 351 } 352 353 } 354 | Popular Tags |