KickJava   Java API By Example, From Geeks To Geeks.

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


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.TransactionManager JavaDoc;
28 import org.jboss.iiop.CorbaORB;
29 import org.jboss.iiop.CorbaORBService;
30 import org.jboss.logging.Logger;
31 import org.jboss.tm.TransactionImpl;
32 import org.jboss.tm.TxUtils;
33 import org.jboss.util.NestedRuntimeException;
34 import org.omg.CORBA.Any JavaDoc;
35 import org.omg.CORBA.LocalObject JavaDoc;
36 import org.omg.CORBA_2_3.ORB JavaDoc;
37 import org.omg.CosTransactions.PropagationContext;
38 import org.omg.CosTransactions.PropagationContextHelper;
39 import org.omg.CosTransactions.TransIdentity;
40 import org.omg.CosTransactions.otid_t;
41 import org.omg.IOP.Codec JavaDoc;
42 import org.omg.IOP.ServiceContext JavaDoc;
43 import org.omg.IOP.TransactionService JavaDoc;
44 import org.omg.IOP.CodecPackage.InvalidTypeForEncoding JavaDoc;
45 import org.omg.PortableInterceptor.ClientRequestInfo JavaDoc;
46 import org.omg.PortableInterceptor.ClientRequestInterceptor JavaDoc;
47
48 /**
49  * This implementation of
50  * <code>org.omg.PortableInterceptor.ClientRequestInterceptor</code>
51  * inserts the transactional context into outgoing requests
52  * from JBoss's transaction manager.
53  *
54  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
55  * @version $Revision: 37459 $
56  */

57 public class TxServerClientInterceptor extends LocalObject JavaDoc implements ClientRequestInterceptor JavaDoc
58 {
59    /** @since 4.0.1 */
60    static final long serialVersionUID = 4716203472714459196L;
61
62    // Static fields -------------------------------------------------
63

64    private static final Logger log =
65       Logger.getLogger(TxServerClientInterceptor.class);
66    private static final boolean traceEnabled = log.isTraceEnabled();
67
68    private static final int txContextId = TransactionService.value;
69    private static Codec JavaDoc codec;
70    private static TransactionManager JavaDoc tm;
71    private static PropagationContext emptyPC;
72
73    // Static methods ------------------------------------------------
74

75    static void init(Codec JavaDoc codec)
76    {
77       TxServerClientInterceptor.codec = codec;
78    }
79    
80    static TransactionManager JavaDoc getTransactionManager()
81    {
82       if (tm == null)
83       {
84          try
85          {
86             Context JavaDoc ctx = new InitialContext JavaDoc();
87             tm = (TransactionManager JavaDoc)ctx.lookup("java:/TransactionManager");
88          }
89          catch (NamingException JavaDoc e)
90          {
91             throw new NestedRuntimeException("java:/TransactionManager lookup failed", e);
92          }
93       }
94       return tm;
95    }
96
97    static PropagationContext getEmptyPropagationContext()
98    {
99       if (emptyPC == null)
100       {
101          // According to the spec, this should all be ignored
102
// But we get NPEs if it doesn't contain some content
103
emptyPC = new PropagationContext();
104          emptyPC.parents = new TransIdentity[0];
105          emptyPC.current = new TransIdentity();
106          emptyPC.current.otid = new otid_t();
107          emptyPC.current.otid.formatID = 666;
108          emptyPC.current.otid.bqual_length = 1;
109          emptyPC.current.otid.tid = new byte[] { (byte) 1 };
110          emptyPC.implementation_specific_data = ORB.init().create_any();
111          emptyPC.implementation_specific_data.insert_boolean(false);
112       }
113       return emptyPC;
114    }
115    
116    // Constructor ---------------------------------------------------
117

118    public TxServerClientInterceptor()
119    {
120       // do nothing
121
}
122
123    // org.omg.PortableInterceptor.Interceptor operations ------------
124

125    public String JavaDoc name()
126    {
127       return "TxServerClientInterceptor";
128    }
129
130    public void destroy()
131    {
132       // do nothing
133
}
134
135    // ClientRequestInterceptor operations ---------------------------
136

137    public void send_request(ClientRequestInfo JavaDoc ri)
138    {
139       if (traceEnabled)
140          log.trace("Intercepting send_request, operation: " + ri.operation());
141       try
142       {
143          Any JavaDoc any = getTransactionPropagationContextAny();
144          if (any != null)
145          {
146             ServiceContext JavaDoc sc = new ServiceContext JavaDoc(txContextId, codec.encode_value(any));
147             ri.add_request_service_context(sc, true /*replace existing context*/);
148          }
149       }
150       catch (InvalidTypeForEncoding JavaDoc e)
151       {
152          throw new NestedRuntimeException(e);
153       }
154    }
155
156    public void send_poll(ClientRequestInfo JavaDoc ri)
157    {
158       // do nothing
159
}
160    
161    public void receive_reply(ClientRequestInfo JavaDoc ri)
162    {
163       // do nothing
164
}
165    
166    public void receive_exception(ClientRequestInfo JavaDoc ri)
167    {
168       // do nothing
169
}
170    
171    public void receive_other(ClientRequestInfo JavaDoc ri)
172    {
173       // do nothing
174
}
175    
176    protected Any JavaDoc getTransactionPropagationContextAny()
177    {
178       try
179       {
180          PropagationContext pc = null;
181          TransactionManager JavaDoc tm = getTransactionManager();
182          TransactionImpl tx = (TransactionImpl) tm.getTransaction();
183          if (!TxUtils.isUncommitted(tx))
184          {
185             if (traceEnabled)
186                log.trace("No transaction context");
187             return null;
188          }
189          
190          if (CorbaORBService.getOTSContextPropagationEnabledFlag() == true)
191          {
192             if (traceEnabled)
193                log.trace("Propagating actual OTS context");
194             pc = (PropagationContext) tx.getOTSPropagationContext();
195          }
196          else
197          {
198             if (traceEnabled)
199                log.trace("Propagating empty OTS context");
200             pc = getEmptyPropagationContext();
201          }
202
203          Any JavaDoc any = CorbaORB.getInstance().create_any();
204          PropagationContextHelper.insert(any, pc);
205          return any;
206       }
207       catch (Exception JavaDoc e)
208       {
209          throw new NestedRuntimeException("Error getting tpc", e);
210       }
211    }
212 }
213
Popular Tags