KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jotm > jta > jeremie > TSHandler


1 /*
2  * @(#) TSHandler
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: TSHandler.java,v 1.3 2004/05/14 17:49:12 tonyortiz Exp $
37  * --------------------------------------------------------------------------
38  */

39 package org.objectweb.jotm.jta.jeremie;
40
41 import org.objectweb.jeremie.services.handler.api.Service;
42 import org.objectweb.jotm.TransactionContext;
43 import org.objectweb.jonathan.apis.kernel.Context;
44 import org.objectweb.jonathan.apis.kernel.JonathanException;
45 import org.objectweb.jonathan.presentation.api.MarshallerFactory;
46 import org.objectweb.jonathan.presentation.api.Marshaller;
47 import org.objectweb.jonathan.presentation.api.UnMarshaller;
48 import org.objectweb.jonathan.resources.api.Chunk;
49 import org.objectweb.jonathan.helpers.MessageHelpers;
50 import org.omg.IOP.ServiceContext JavaDoc;
51
52 public class TSHandler implements Service {
53
54     private JotmTransactionSender sender = null;
55     private JotmTransactionReceiver receiver = null;
56     private MarshallerFactory mf;
57     private int service_id = 0;
58
59    /**
60     * Builds a new Jeremie transaction service handler instance.
61     * @param c unused
62     * @param used_components the components used to initialize the new TSHandler.
63     * @exception JonathanException if something goes wrong.
64     */

65    public TSHandler(Context c, Object JavaDoc[] used_components) throws JonathanException {
66        int sid = ((Integer JavaDoc) used_components[0]).intValue();
67        if (sid != Integer.MAX_VALUE) {
68        service_id = sid;
69        }
70        try {
71        sender = (JotmTransactionSender) used_components[1];
72        receiver = (JotmTransactionReceiver) used_components[2];
73        mf = (MarshallerFactory) used_components[3];
74        } catch (Exception JavaDoc e) {
75        throw new JonathanException(e);
76        }
77    }
78    
79     /**
80      * Returns a request context.
81      * @return a service context.
82      */

83     public ServiceContext JavaDoc getRequestContext(int id, boolean r, byte[] key, Context k) {
84     if (sender == null) {
85         return null;
86     }
87     TransactionContext ctx = sender.sending_request();
88     return encodeContext(ctx);
89     }
90      
91     
92     /**
93      * Returns a reply context.
94      * @return a service context.
95      */

96     public ServiceContext JavaDoc getReplyContext(int id, Context k) {
97     if (receiver == null) {
98         return null;
99     }
100     TransactionContext ctx = receiver.sending_reply();
101     return encodeContext(ctx);
102     }
103     
104     /**
105      * This method is called by the services handler to let the operations
106      * related to the target service be performed on request arrival.
107      * @param context the service context of the request;
108      */

109     public void handleRequestContext(ServiceContext JavaDoc context, int id, boolean r, byte[] key, Context k) {
110     if (receiver == null || context == null) {
111         return;
112     }
113     TransactionContext ctx = decodeContext(context);
114     if (ctx != null) {
115         receiver.received_request(ctx);
116     }
117     }
118     
119     /**
120      * This method is called by the services handler to let the operations
121      * related to the target service be performed on reply arrival.
122      * @param context the service context of the reply;
123      */

124     public void handleReplyContext(ServiceContext JavaDoc context, int id, Context k) {
125     if (sender == null || context == null) {
126         return;
127     }
128     TransactionContext ctx = decodeContext(context);
129     if (ctx != null) {
130         sender.received_reply(ctx);
131     }
132     }
133     
134    /**
135     * Encodes a Transaction Service propagation context into an IOP service
136     * context.
137     * @param ctx the propagation context to be encoded.
138     * @return the resulting IOP service context.
139     */

140    private ServiceContext JavaDoc encodeContext(TransactionContext ctx) {
141       if (ctx == null) {
142          return null;
143       }
144       Marshaller marshaller = mf.newMarshaller();
145       byte[] byteArray = null;
146       try {
147          marshaller.writeValue(ctx);
148          byteArray = MessageHelpers.copy(marshaller);
149          marshaller.close();
150       } catch (Exception JavaDoc e) {
151          System.err.println("TSHandler.encodeContext: exception");
152       }
153       return new ServiceContext JavaDoc(service_id, byteArray);
154    }
155    
156    /**
157     * Decodes a Transaction Service propagation context from an IOP service
158     * context.
159     *
160     * @param ctx the IOP service context containing the encoded propagation
161     * context.
162     * @return the decoded propagation context.
163     */

164    private TransactionContext decodeContext(ServiceContext JavaDoc sc) {
165       if (sc == null || sc.context_data == null || sc.context_data.length == 0 ) {
166          return null;
167       }
168       TransactionContext ctx = null;
169       byte[] byteArray = sc.context_data;
170       UnMarshaller unmarshaller =
171          mf.newUnMarshaller(new Chunk(byteArray, 0, byteArray.length), 0);
172       try {
173          ctx = (TransactionContext) unmarshaller.readValue();
174          unmarshaller.close();
175       } catch (Exception JavaDoc e) {
176          System.err.println("TSHandler.decodeContext: exception");
177          System.err.println(e.toString() + "\n");
178       }
179       return ctx;
180    }
181    
182 }
183
Popular Tags