KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tm > iiop > TxServerInterceptor


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.tm.iiop;
23
24 import javax.naming.Context JavaDoc;
25 import javax.naming.InitialContext JavaDoc;
26 import javax.naming.NamingException JavaDoc;
27 import javax.transaction.Transaction JavaDoc;
28
29 import org.omg.CORBA.Any JavaDoc;
30 import org.omg.CORBA.BAD_PARAM JavaDoc;
31 import org.omg.CORBA.TCKind JavaDoc;
32 import org.omg.CORBA.LocalObject JavaDoc;
33 import org.omg.CosTransactions.PropagationContext;
34 import org.omg.CosTransactions.PropagationContextHelper;
35 import org.omg.CosTransactions.otid_t;
36 import org.omg.IOP.Codec JavaDoc;
37 import org.omg.IOP.CodecPackage.FormatMismatch JavaDoc;
38 import org.omg.IOP.CodecPackage.TypeMismatch JavaDoc;
39 import org.omg.IOP.ServiceContext JavaDoc;
40 import org.omg.PortableInterceptor.InvalidSlot JavaDoc;
41 import org.omg.PortableInterceptor.ServerRequestInfo JavaDoc;
42 import org.omg.PortableInterceptor.ServerRequestInterceptor JavaDoc;
43
44 import org.jboss.logging.Logger;
45 import org.jboss.proxy.ejb.ForeignTransaction;
46 import org.jboss.tm.TransactionPropagationContextImporter;
47 import org.jboss.tm.iiop.wrapper.OTSCoordinatorWrapper;
48 import org.jboss.tm.remoting.interfaces.Coordinator;
49 import org.jboss.tm.remoting.interfaces.TxPropagationContext;
50
51 /**
52  * This implementation of
53  * <code>org.omg.PortableInterceptor.ServerRequestInterceptor</code>
54  * retrieves the transactional context from incoming IIOP requests and
55  * makes it available to the servant methods that handle the requests,
56  * through the static method <code>getCurrentTransaction</code).
57  *
58  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
59  * @version $Revision: 37459 $
60  */

61 public class TxServerInterceptor
62       extends LocalObject JavaDoc
63       implements ServerRequestInterceptor JavaDoc
64 {
65    /** @since 4.0.1 */
66    static final long serialVersionUID = 7474707114565659371L;
67
68    // Static fields -------------------------------------------------
69

70    private static final Logger log =
71       Logger.getLogger(TxServerInterceptor.class);
72    private static final boolean traceEnabled = log.isTraceEnabled();
73
74    private static final int txContextId = org.omg.IOP.TransactionService.value;
75    private static int slotId;
76    private static Codec JavaDoc codec;
77    private static org.omg.PortableInterceptor.Current JavaDoc piCurrent = null;
78    private static TransactionPropagationContextImporter tpcImporter = null;
79    
80    // Static methods ------------------------------------------------
81

82    /**
83     * Called by <code>TxServerInterceptorInitializer</code>
84     * at ORB initialization time.
85     */

86    static void init(int slotId, Codec JavaDoc codec,
87                     org.omg.PortableInterceptor.Current JavaDoc piCurrent)
88    {
89       TxServerInterceptor.slotId = slotId;
90       TxServerInterceptor.codec = codec;
91       TxServerInterceptor.piCurrent = piCurrent;
92    }
93
94    /**
95     * Returns the transaction associated with the transaction propagation
96     * context that arrived in the current IIOP request.
97     */

98    public static Transaction JavaDoc getCurrentTransaction()
99    {
100       Transaction JavaDoc tx = null;
101       if (piCurrent != null)
102       {
103          // A non-null piCurrent means that a TxServerInterceptor was
104
// installed: check if there is a transaction propagation context
105
try
106          {
107             Any JavaDoc any = piCurrent.get_slot(slotId);
108             if (any.type().kind().value() != TCKind._tk_null)
109             {
110                // Yes, there is a TPC
111
PropagationContext pc = PropagationContextHelper.extract(any);
112                if (pc.current.coord != null)
113                {
114                   // In order to import the CosTransactions PropagationContext
115
// we need to convert it to a DTM TxPropagationContext.
116

117                   // Get the global id from the PropagationContext
118
byte[] globalId;
119                   otid_t otid = pc.current.otid;
120                   
121                   if (otid.bqual_length == 0)
122                      globalId = otid.tid;
123                   else
124                   {
125                      // Strip the branch qualifier part
126
int len = otid.tid.length - otid.bqual_length;
127                      globalId = new byte[len];
128                      System.arraycopy(otid.tid, 0, globalId, 0, len);
129                   }
130                   
131                   // Wrap the coordinator into a DTM Coordinator instance
132
Coordinator coordinatorWrapper =
133                      new OTSCoordinatorWrapper(pc.current.coord);
134                   
135                   // Create the DTM TPC
136
TxPropagationContext tpc =
137                      new TxPropagationContext(otid.formatID,
138                                               globalId,
139                                               pc.timeout,
140                                               coordinatorWrapper,
141                                               null);
142                   
143                   // Import the TPC
144
tx = getTPCImporter().importTransactionPropagationContext(tpc);
145                }
146                
147                // No transaction propagation (add the marker)
148
if (tx == null)
149                   tx = ForeignTransaction.instance;
150             }
151          }
152          catch (InvalidSlot JavaDoc e)
153          {
154             throw new RuntimeException JavaDoc("Exception getting slot in " +
155                                        "TxServerInterceptor: " + e);
156          }
157          
158       }
159       return tx;
160    }
161    
162    /**
163     * Get a reference to the transaction importer.
164     */

165    private static TransactionPropagationContextImporter getTPCImporter()
166    {
167       if (tpcImporter == null)
168       {
169          try
170          {
171             Context JavaDoc ctx = new InitialContext JavaDoc();
172             tpcImporter = (TransactionPropagationContextImporter)ctx.lookup(
173                                  "java:/TransactionPropagationContextImporter");
174          }
175          catch (NamingException JavaDoc e)
176          {
177             throw new RuntimeException JavaDoc(
178                   "java:/TransactionPropagationContextImporter lookup failed",
179                   e);
180          }
181       }
182       return tpcImporter;
183    }
184
185    // Constructor ---------------------------------------------------
186

187    public TxServerInterceptor()
188    {
189       // do nothing
190
}
191
192    // org.omg.PortableInterceptor.Interceptor operations ------------
193

194    public String JavaDoc name()
195    {
196       return "TxServerInterceptor";
197    }
198    
199    public void destroy()
200    {
201       // do nothing
202
}
203    
204    // ServerRequestInterceptor operations ---------------------------
205

206    public void receive_request_service_contexts(ServerRequestInfo JavaDoc ri)
207    {
208       if (traceEnabled)
209          log.trace("Intercepting receive_request_service_contexts, " +
210                    "operation: " + ri.operation());
211       try
212       {
213          ServiceContext JavaDoc sc = ri.get_request_service_context(txContextId);
214          Any JavaDoc any = codec.decode_value(sc.context_data,
215                                       PropagationContextHelper.type());
216          ri.set_slot(slotId, any);
217       }
218       catch (BAD_PARAM JavaDoc e)
219       {
220          // no service context with txContextId: do nothing
221
}
222       catch (FormatMismatch JavaDoc e)
223       {
224          throw new RuntimeException JavaDoc("Exception decoding context data in " +
225                                     "TxServerInterceptor: " + e);
226       }
227       catch (TypeMismatch JavaDoc e)
228       {
229          throw new RuntimeException JavaDoc("Exception decoding context data in " +
230                                     "TxServerInterceptor: " + e);
231       }
232       catch (InvalidSlot JavaDoc e)
233       {
234          throw new RuntimeException JavaDoc("Exception setting slot in " +
235                                     "TxServerInterceptor: " + e);
236       }
237    }
238    
239    public void receive_request(ServerRequestInfo JavaDoc ri)
240    {
241       // do nothing
242
}
243
244    public void send_reply(ServerRequestInfo JavaDoc ri)
245    {
246       // do nothing
247
}
248
249    public void send_exception(ServerRequestInfo JavaDoc ri)
250    {
251       // do nothing
252
}
253
254    public void send_other(ServerRequestInfo JavaDoc ri)
255    {
256       // do nothing
257
}
258 }
259
Popular Tags