1 22 package org.jboss.ejb3.iiop; 23 24 import java.lang.reflect.Method ; 25 import java.security.Principal ; 26 import java.util.HashMap ; 27 28 import javax.transaction.Transaction ; 29 import javax.transaction.TransactionManager ; 30 31 import org.jboss.ejb3.Container; 32 import org.jboss.ejb3.SessionContainer; 33 import org.jboss.ejb3.stateful.StatefulContainer; 34 import org.jboss.ejb3.stateless.StatelessContainer; 35 import org.jboss.iiop.CorbaORB; 36 import org.jboss.iiop.csiv2.SASCurrent; 37 import org.jboss.iiop.rmi.AttributeAnalysis; 38 import org.jboss.iiop.rmi.InterfaceAnalysis; 39 import org.jboss.iiop.rmi.OperationAnalysis; 40 import org.jboss.iiop.rmi.RmiIdlUtil; 41 import org.jboss.iiop.rmi.marshal.strategy.SkeletonStrategy; 42 import org.jboss.invocation.iiop.ReferenceData; 43 import org.jboss.logging.Logger; 44 import org.jboss.security.SecurityAssociation; 45 import org.jboss.security.SimplePrincipal; 46 import org.jboss.tm.TransactionManagerLocator; 47 import org.jboss.tm.iiop.TxServerInterceptor; 48 import org.omg.CORBA.BAD_OPERATION ; 49 import org.omg.CORBA.InterfaceDef ; 50 import org.omg.CORBA.SystemException ; 51 import org.omg.CORBA.ORBPackage.InvalidName ; 52 import org.omg.CORBA.portable.InputStream ; 53 import org.omg.CORBA.portable.InvokeHandler ; 54 import org.omg.CORBA.portable.OutputStream ; 55 import org.omg.CORBA.portable.ResponseHandler ; 56 import org.omg.PortableServer.Current ; 57 import org.omg.PortableServer.POA ; 58 import org.omg.PortableServer.Servant ; 59 60 66 public class BeanCorbaServant extends Servant 67 implements InvokeHandler 68 { 69 private static final Logger log = Logger.getLogger(BeanCorbaServant.class); 70 71 private final IORFactory factory; 72 private final Current poaCurrent; 73 private final Container container; 74 private final InterfaceDef interfaceDef; 75 private final String repositoryIds[]; 76 private SASCurrent sasCurrent; 77 78 private HashMap <String , SkeletonStrategy> methodMap; 79 80 protected BeanCorbaServant(IORFactory factory, Current poaCurrent, Container container, InterfaceDef interfaceDef, InterfaceAnalysis interfaceAnalysis) 81 { 82 assert factory != null; 83 assert poaCurrent != null; 84 assert container != null; 85 assert container instanceof SessionContainer; assert interfaceDef != null; 87 assert interfaceAnalysis != null; 88 89 this.factory = factory; 90 this.poaCurrent = poaCurrent; 91 this.container = container; 92 this.interfaceDef = interfaceDef; 93 this.repositoryIds = interfaceAnalysis.getAllTypeIds(); 94 95 try 96 { 97 this.sasCurrent = (SASCurrent) CorbaORB.getInstance().resolve_initial_references("SASCurrent"); 98 } 99 catch (InvalidName e) 100 { 101 log.warn("Can't find SASCurrent"); 102 this.sasCurrent = null; 103 } 104 105 this.methodMap = new HashMap <String , SkeletonStrategy>(); 106 AttributeAnalysis[] attrs = interfaceAnalysis.getAttributes(); 107 for (int i = 0; i < attrs.length; i++) { 108 OperationAnalysis op = attrs[i].getAccessorAnalysis(); 109 110 log.debug(" " + op.getJavaName() + ": " + op.getIDLName()); 111 methodMap.put(op.getIDLName(), 112 new SkeletonStrategy(op.getMethod())); 113 op = attrs[i].getMutatorAnalysis(); 114 if (op != null) { 115 log.debug(" " + op.getJavaName() + ": " + op.getIDLName()); 116 methodMap.put(op.getIDLName(), 117 new SkeletonStrategy(op.getMethod())); 118 } 119 } 120 121 OperationAnalysis[] ops = interfaceAnalysis.getOperations(); 122 for (int i = 0; i < ops.length; i++) { 123 log.debug(" " + ops[i].getJavaName() + ": " + ops[i].getIDLName()); 124 methodMap.put(ops[i].getIDLName(), 125 new SkeletonStrategy(ops[i].getMethod())); 126 } 127 } 128 129 @Override 130 public String [] _all_interfaces(POA poa, byte[] objectId) 131 { 132 return (String []) repositoryIds.clone(); 133 } 134 135 138 @Override 139 public org.omg.CORBA.Object _get_interface_def() 140 { 141 if (interfaceDef != null) 142 return interfaceDef; 143 else 144 return super._get_interface_def(); 145 } 146 147 public OutputStream _invoke(String opName, InputStream in, ResponseHandler handler) throws SystemException 148 { 149 log.trace("invoke: " + opName); 150 151 SkeletonStrategy op = (SkeletonStrategy) methodMap.get(opName); 152 if (op == null) 153 { 154 log.debug("Unable to find opname '" + opName + "' valid operations:" + methodMap.keySet()); 155 throw new BAD_OPERATION (opName); 156 } 157 158 org.omg.CORBA_2_3.portable.OutputStream out; 159 try 160 { 161 Object id = ReferenceData.extractObjectId(poaCurrent.get_object_id()); 162 log.trace("id = " + id); 163 164 Transaction tx = TxServerInterceptor.getCurrentTransaction(); 165 log.trace("tx = " + tx); 166 167 if(sasCurrent != null) 168 { 169 byte username[] = sasCurrent.get_incoming_username(); 170 byte credentials[] = sasCurrent.get_incoming_password(); 171 byte principalName[] = sasCurrent.get_incoming_principal_name(); 172 173 if(username != null && username.length > 0) 174 { 175 String name = new String (username, "UTF-8"); 176 int domainIndex = name.lastIndexOf("@"); 177 if(domainIndex > 0) 178 name = name.substring(0, domainIndex); 179 log.debug("username = " + name); 180 Principal principal = new SimplePrincipal(name); 181 SecurityAssociation.setPrincipal(principal); 182 } 183 184 if(credentials != null && credentials.length > 0) 185 { 186 SecurityAssociation.setCredential(new String (credentials, "UTF-8").toCharArray()); 187 } 188 189 if(principalName != null && principalName.length > 0) 190 log.warn("principalName = " + new String (principalName, "UTF-8")); } 192 193 Object args[] = op.readParams((org.omg.CORBA_2_3.portable.InputStream ) in); 194 195 Object retVal = invoke(tx, id, op.getMethod(), args); 196 197 out = (org.omg.CORBA_2_3.portable.OutputStream ) handler.createReply(); 198 if(op.isNonVoid()) 199 op.writeRetval(out, retVal); 200 } 201 catch(Throwable t) 202 { 203 t.printStackTrace(); 205 if(t instanceof Exception ) 206 { 207 Exception e = (Exception ) t; 208 RmiIdlUtil.rethrowIfCorbaSystemException(e); 209 out = (org.omg.CORBA_2_3.portable.OutputStream ) handler.createExceptionReply(); 210 op.writeException(out, e); 211 } 212 else 213 throw new RuntimeException ("NYI"); 214 } 215 return out; 216 } 217 218 private TransactionManager getTransactionManager() 219 { 220 return TransactionManagerLocator.getInstance().locate(); 222 } 223 224 private Object invoke(Object id, Method method, Object args[]) throws Throwable 225 { 226 return ((SessionContainer) container).invoke(factory, id, method, args, null); 228 } 229 230 private Object invoke(Transaction tx, Object id, Method method, Object args[]) throws Throwable 231 { 232 if(tx == null) 233 return invoke(id, method, args); 234 235 TransactionManager tm = getTransactionManager(); 238 239 if(tm.getTransaction() != null) 241 throw new RuntimeException ("cannot import a transaction context when a transaction is already associated with the thread"); 242 tm.resume(tx); 243 try 244 { 245 return invoke(id, method, args); 246 } 247 finally 248 { 249 tm.suspend(); 250 } 251 } 252 } 253 | Popular Tags |