KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jotm > ots > OTSServerTransactionInterceptor


1 /*
2  * @(#) OTSServerTransactionInterceptor.java 1.0 02/07/15
3  *
4  * JOTM: Java Open Transaction Manager
5  *
6  * This module was originally developed by
7  * - INRIA inside the ObjectWeb Consortium(http://www.objectweb.org)
8  *
9  * The original code and portions created by INRIA are
10  * Copyright (C) 2002 - INRIA (www.inria.fr)
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions are met:
15  *
16  * -Redistributions of source code must retain the above copyright notice, this
17  * list of conditions and the following disclaimer.
18  *
19  * -Redistributions in binary form must reproduce the above copyright notice,
20  * this list of conditions and the following disclaimer in the documentation
21  * and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * --------------------------------------------------------------------------
36  * $Id: OTSServerTransactionInterceptor.java,v 1.15 2004/12/13 19:50:05 tonyortiz Exp $
37  * --------------------------------------------------------------------------
38  */

39 package org.objectweb.jotm.ots;
40
41 // java import
42
import java.util.Collections JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.Map JavaDoc;
45
46 import org.objectweb.jotm.Current;
47 import org.objectweb.jotm.InternalTransactionContext;
48 import org.objectweb.jotm.TransactionContext;
49 import org.omg.IOP.ServiceContext JavaDoc;
50 import org.omg.PortableInterceptor.ForwardRequest JavaDoc;
51 import org.omg.PortableInterceptor.ORBInitInfo JavaDoc;
52 import org.omg.PortableInterceptor.ServerRequestInfo JavaDoc;
53 import org.omg.PortableInterceptor.ServerRequestInterceptor JavaDoc;
54
55 /**
56  * Class <code>OTSServerTransactionInterceptor</code> is a Server Interceptor for OTS Java Server
57  * of JOTM. This Interceptor translate the Standart OTS Propagation Context to a Internal JOTM
58  * Transaction context
59  *
60  * @author Guillaume Riviere (Guillaume.Riviere@inrialpes.fr)
61  * @version 1.0, 13/09/2002
62  */

63 public class OTSServerTransactionInterceptor extends OTSInterceptor implements ServerRequestInterceptor JavaDoc {
64
65     /**
66      * current object
67      */

68     private static Current current = null;
69
70     /**
71      * Transaction context
72      */

73     private Map JavaDoc contexts = Collections.synchronizedMap(new HashMap JavaDoc());
74     // private TransactionContext txCtx = null;
75

76     /**
77      * interceptor name
78      */

79     private String JavaDoc interceptorName = "OTSServerTransactionInteceptor";
80
81     /**
82      * constructor
83      */

84     public OTSServerTransactionInterceptor(ORBInitInfo JavaDoc info) {
85     super(info);
86     }
87
88     /**
89      * get the name of this interceptor
90      * @return name
91      */

92     public String JavaDoc name() {
93     return interceptorName;
94     }
95
96     /**
97      * Receive request context
98      * @param jri JServerRequestInfo the server request information
99      * @exception IOException if an exception occur with the ObjectOutput
100      */

101     public void receive_request_service_contexts(ServerRequestInfo JavaDoc jri) throws ForwardRequest JavaDoc {
102         try {
103             TransactionContext txCtx = decodeCorbaPropagationContext(jri.get_request_service_context(TX_CTX_ID)) ;
104
105             if ( txCtx != null ) {
106                 Object JavaDoc key = new Integer JavaDoc(jri.request_id());
107                 contexts.put (key, txCtx);
108             }
109         } catch (org.omg.CORBA.BAD_PARAM JavaDoc b) {
110             // else we do nothing -> no transaction context for this call
111
} catch (Exception JavaDoc e) {
112             throw new ForwardRequest JavaDoc();
113         }
114     }
115
116     /**
117      * Receive request
118      * @param jri JServerRequestInfo the server request information
119      * @exception IOException if an exception occur with the ObjectOutput
120      */

121     public void receive_request(ServerRequestInfo JavaDoc jri) throws ForwardRequest JavaDoc {
122         if (current == null) {
123             current = Current.getCurrent();
124         }
125
126         if (current != null) {
127             Object JavaDoc key = new Integer JavaDoc(jri.request_id());
128             InternalTransactionContext txCtx = (InternalTransactionContext) contexts.remove(key);
129
130             // put into the the Current object (false for the server side context)
131
current.setPropagationContext(txCtx, false);
132         }
133     }
134
135     /**
136      * send reply with context
137      * @param jri JServerRequestInfo the server request information
138      * @exception IOException if an exception occur with the ObjectOutput
139      */

140     public void send_reply(ServerRequestInfo JavaDoc jri) {
141
142         if (current == null) {
143             current = Current.getCurrent();
144         }
145     
146         if (current != null) {
147             try {
148                 // get the Transaction Context (null if there is no transaction)
149
TransactionContext txCtx = current.getPropagationContext(true);
150                 ServiceContext JavaDoc pContext = null ;
151
152                 if (txCtx != null) {
153                     
154                     // get the TransactionContext and build the Corba PropagtionContext
155
pContext = buildCorbaPropagationContext(txCtx);
156                     jri.add_reply_service_context(pContext, true);
157                     current.setPropagationContext(null, false);
158                 } else {
159                     
160                   // if no active global transaction, the container does not include a tx context
161
// in the request message
162
}
163             } catch (Exception JavaDoc e) {
164             }
165         }
166     }
167
168     public void send_exception(ServerRequestInfo JavaDoc jri) throws ForwardRequest JavaDoc {
169         if (current == null) {
170             current = Current.getCurrent();
171         }
172
173         if (current != null) {
174             try {
175                 // get the Transaction Context (null if there is no transaction)
176
TransactionContext txCtx = current.getPropagationContext(true);
177                 ServiceContext JavaDoc pContext = null ;
178
179                 if (txCtx != null) {
180                     
181                     // get the TransactionContext and build the Corba PropagtionContext
182
pContext = buildCorbaPropagationContext(txCtx);
183                     jri.add_reply_service_context(pContext, true);
184                     current.setPropagationContext(null, false);
185                 } else {
186                 
187                     // if no active global transaction, the container does not include a tx context
188
// in the request message
189
}
190             } catch (Exception JavaDoc e) {
191             }
192         }
193     }
194
195     public void send_other(ServerRequestInfo JavaDoc jri) throws ForwardRequest JavaDoc {
196     }
197     
198     public void destroy() {
199     }
200     
201 }
202
Popular Tags