KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.omg.CORBA.Any JavaDoc;
25 import org.omg.CORBA.LocalObject JavaDoc;
26 import org.omg.CORBA.TCKind JavaDoc;
27 import org.omg.CosTransactions.PropagationContext;
28 import org.omg.CosTransactions.PropagationContextHelper;
29 import org.omg.IOP.Codec JavaDoc;
30 import org.omg.IOP.CodecPackage.InvalidTypeForEncoding JavaDoc;
31 import org.omg.IOP.ServiceContext JavaDoc;
32 import org.omg.PortableInterceptor.ClientRequestInfo JavaDoc;
33 import org.omg.PortableInterceptor.ClientRequestInterceptor JavaDoc;
34 import org.omg.PortableInterceptor.InvalidSlot JavaDoc;
35
36 import org.jboss.iiop.CorbaORB;
37 import org.jboss.logging.Logger;
38
39 /**
40  * This implementation of
41  * <code>org.omg.PortableInterceptor.ClientRequestInterceptor</code>
42  * inserts the transactional context into outgoing requests.
43  *
44  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
45  * @version $Revision: 37459 $
46  */

47 public class TxClientInterceptor
48       extends LocalObject JavaDoc
49       implements ClientRequestInterceptor JavaDoc
50 {
51    /** @since 4.0.1 */
52    static final long serialVersionUID = -8021933521763745659L;
53
54    // Static fields -------------------------------------------------
55

56    private static final int txContextId = org.omg.IOP.TransactionService.value;
57    private static int slotId;
58    private static Codec JavaDoc codec;
59    private static org.omg.PortableInterceptor.Current JavaDoc piCurrent;
60    private static Any JavaDoc emptyAny = null;
61    private static final Logger log =
62       Logger.getLogger(TxClientInterceptor.class);
63    private static final boolean traceEnabled = log.isTraceEnabled();
64
65    // Static methods ------------------------------------------------
66

67    static void init(int slotId, Codec JavaDoc codec,
68                     org.omg.PortableInterceptor.Current JavaDoc piCurrent)
69    {
70       TxClientInterceptor.slotId = slotId;
71       TxClientInterceptor.codec = codec;
72       TxClientInterceptor.piCurrent = piCurrent;
73    }
74
75    /**
76     * Sets the transaction propagation context to be sent out with the IIOP
77     * requests generated by the current thread.
78     */

79    public static void setOutgoingPropagationContext(PropagationContext pc)
80    {
81       Any JavaDoc any = CorbaORB.getInstance().create_any();
82       PropagationContextHelper.insert(any, pc);
83       try
84       {
85          piCurrent.set_slot(slotId, any);
86       }
87       catch (InvalidSlot JavaDoc e)
88       {
89          throw new RuntimeException JavaDoc("Exception setting propagation context: "
90                                     + e);
91       }
92    }
93
94    /**
95     * Unsets the transaction propagation context associated with the current
96     * thread.
97     */

98    public static void unsetOutgoingPropagationContext()
99    {
100       try
101       {
102          piCurrent.set_slot(slotId, getEmptyAny());
103       }
104       catch (InvalidSlot JavaDoc e)
105       {
106          throw new RuntimeException JavaDoc("Exception unsetting propagation context: "
107                                     + e);
108       }
109    }
110
111    /**
112     * Auxiliary method that returns an empty Any.
113     */

114    private static Any JavaDoc getEmptyAny()
115    {
116       if (emptyAny == null)
117          emptyAny = CorbaORB.getInstance().create_any();
118       return emptyAny;
119    }
120    
121    // Constructor ---------------------------------------------------
122

123    public TxClientInterceptor()
124    {
125       // do nothing
126
}
127
128    // org.omg.PortableInterceptor.Interceptor operations ------------
129

130    public String JavaDoc name()
131    {
132       return "TxClientInterceptor";
133    }
134
135    public void destroy()
136    {
137       // do nothing
138
}
139
140    // ClientRequestInterceptor operations ---------------------------
141

142    public void send_request(ClientRequestInfo JavaDoc ri)
143    {
144       if (traceEnabled)
145          log.trace("send_request: " + ri.operation());
146       try
147       {
148          Any JavaDoc any = ri.get_slot(slotId);
149          if (any.type().kind().value() != TCKind._tk_null)
150          {
151             ServiceContext JavaDoc sc = new ServiceContext JavaDoc(txContextId,
152                                                    codec.encode_value(any));
153             ri.add_request_service_context(sc,
154                                            true /*replace existing context*/);
155          }
156       }
157       catch (InvalidSlot JavaDoc e)
158       {
159          throw new RuntimeException JavaDoc("Exception getting slot in " +
160                                     "TxServerInterceptor: " + e);
161       }
162       catch (InvalidTypeForEncoding JavaDoc e)
163       {
164          throw new RuntimeException JavaDoc(e);
165       }
166    }
167
168    public void send_poll(ClientRequestInfo JavaDoc ri)
169    {
170       // do nothing
171
}
172    
173    public void receive_reply(ClientRequestInfo JavaDoc ri)
174    {
175       // do nothing
176
}
177    
178    public void receive_exception(ClientRequestInfo JavaDoc ri)
179    {
180       // do nothing
181
}
182    
183    public void receive_other(ClientRequestInfo JavaDoc ri)
184    {
185       // do nothing
186
}
187    
188 }
189
Popular Tags