KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > iiop > BeanCorbaServant


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.ejb3.iiop;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.security.Principal JavaDoc;
26 import java.util.HashMap JavaDoc;
27
28 import javax.transaction.Transaction JavaDoc;
29 import javax.transaction.TransactionManager JavaDoc;
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 JavaDoc;
49 import org.omg.CORBA.InterfaceDef JavaDoc;
50 import org.omg.CORBA.SystemException JavaDoc;
51 import org.omg.CORBA.ORBPackage.InvalidName JavaDoc;
52 import org.omg.CORBA.portable.InputStream JavaDoc;
53 import org.omg.CORBA.portable.InvokeHandler JavaDoc;
54 import org.omg.CORBA.portable.OutputStream JavaDoc;
55 import org.omg.CORBA.portable.ResponseHandler JavaDoc;
56 import org.omg.PortableServer.Current JavaDoc;
57 import org.omg.PortableServer.POA JavaDoc;
58 import org.omg.PortableServer.Servant JavaDoc;
59
60 /**
61  * Comment
62  *
63  * @author <a HREF="mailto:carlo.dewolf@jboss.com">Carlo de Wolf</a>
64  * @version $Revision: 55486 $
65  */

66 public class BeanCorbaServant extends Servant JavaDoc
67    implements InvokeHandler JavaDoc
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 JavaDoc interfaceDef;
75    private final String JavaDoc repositoryIds[];
76    private SASCurrent sasCurrent;
77
78    private HashMap JavaDoc<String JavaDoc, SkeletonStrategy> methodMap;
79    
80    protected BeanCorbaServant(IORFactory factory, Current poaCurrent, Container container, InterfaceDef JavaDoc interfaceDef, InterfaceAnalysis interfaceAnalysis)
81    {
82       assert factory != null;
83       assert poaCurrent != null;
84       assert container != null;
85       assert container instanceof SessionContainer; // see invoke
86
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 JavaDoc e)
100       {
101          log.warn("Can't find SASCurrent");
102          this.sasCurrent = null;
103       }
104       
105       this.methodMap = new HashMap JavaDoc<String JavaDoc, 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 JavaDoc
130    public String JavaDoc[] _all_interfaces(POA JavaDoc poa, byte[] objectId)
131    {
132       return (String JavaDoc[]) repositoryIds.clone();
133    }
134
135    /**
136     * Returns an IR object describing the bean's remote interface.
137     */

138    @Override JavaDoc
139    public org.omg.CORBA.Object JavaDoc _get_interface_def()
140    {
141       if (interfaceDef != null)
142          return interfaceDef;
143       else
144          return super._get_interface_def();
145    }
146    
147    public OutputStream JavaDoc _invoke(String JavaDoc opName, InputStream JavaDoc in, ResponseHandler JavaDoc handler) throws SystemException JavaDoc
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 JavaDoc(opName);
156       }
157
158       org.omg.CORBA_2_3.portable.OutputStream JavaDoc out;
159       try
160       {
161          Object JavaDoc id = ReferenceData.extractObjectId(poaCurrent.get_object_id());
162          log.trace("id = " + id);
163          
164          Transaction JavaDoc 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 JavaDoc name = new String JavaDoc(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 JavaDoc principal = new SimplePrincipal(name);
181                SecurityAssociation.setPrincipal(principal);
182             }
183             
184             if(credentials != null && credentials.length > 0)
185             {
186                SecurityAssociation.setCredential(new String JavaDoc(credentials, "UTF-8").toCharArray());
187             }
188             
189             if(principalName != null && principalName.length > 0)
190                log.warn("principalName = " + new String JavaDoc(principalName, "UTF-8")); // FIXME: implement principalName support
191
}
192          
193          Object JavaDoc args[] = op.readParams((org.omg.CORBA_2_3.portable.InputStream JavaDoc) in);
194          
195          Object JavaDoc retVal = invoke(tx, id, op.getMethod(), args);
196          
197          out = (org.omg.CORBA_2_3.portable.OutputStream JavaDoc) handler.createReply();
198          if(op.isNonVoid())
199             op.writeRetval(out, retVal);
200       }
201       catch(Throwable JavaDoc t)
202       {
203          // TODO: check log level before stacktrace?
204
t.printStackTrace();
205          if(t instanceof Exception JavaDoc)
206          {
207             Exception JavaDoc e = (Exception JavaDoc) t;
208             RmiIdlUtil.rethrowIfCorbaSystemException(e);
209             out = (org.omg.CORBA_2_3.portable.OutputStream JavaDoc) handler.createExceptionReply();
210             op.writeException(out, e);
211          }
212          else
213             throw new RuntimeException JavaDoc("NYI");
214       }
215       return out;
216    }
217
218    private TransactionManager JavaDoc getTransactionManager()
219    {
220       //return TxUtil.getTransactionManager();
221
return TransactionManagerLocator.getInstance().locate();
222    }
223    
224    private Object JavaDoc invoke(Object JavaDoc id, Method JavaDoc method, Object JavaDoc args[]) throws Throwable JavaDoc
225    {
226       // TODO: Support other containers beside Stateless and Stateful?
227
return ((SessionContainer) container).invoke(factory, id, method, args, null);
228    }
229    
230    private Object JavaDoc invoke(Transaction JavaDoc tx, Object JavaDoc id, Method JavaDoc method, Object JavaDoc args[]) throws Throwable JavaDoc
231    {
232       if(tx == null)
233          return invoke(id, method, args);
234       
235       // FIXME: refactor TxServerInterceptor so that it pushed the tpc into the invocation like ClientTxPropegationInterceptor
236
// this would require the localInvoke to be also refactored, so that it uses invocation instead of localInvoke.
237
TransactionManager JavaDoc tm = getTransactionManager();
238       
239       // see TxPropagationInterceptor
240
if(tm.getTransaction() != null)
241          throw new RuntimeException JavaDoc("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