001: /*
002: * @(#) TSHandler
003: *
004: * JOTM: Java Open Transaction Manager
005: *
006: * This module was originally developed by
007: * - INRIA inside the ObjectWeb Consortium(http://www.objectweb.org)
008: *
009: * The original code and portions created by INRIA are
010: * Copyright (C) 2002 - INRIA (www.inria.fr)
011: * All rights reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions are met:
015: *
016: * -Redistributions of source code must retain the above copyright notice, this
017: * list of conditions and the following disclaimer.
018: *
019: * -Redistributions in binary form must reproduce the above copyright notice,
020: * this list of conditions and the following disclaimer in the documentation
021: * and/or other materials provided with the distribution.
022: *
023: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
024: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
025: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
026: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
027: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
028: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
029: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
030: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
031: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
032: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
033: * POSSIBILITY OF SUCH DAMAGE.
034: *
035: * --------------------------------------------------------------------------
036: * $Id: TSHandler.java,v 1.3 2004/05/14 17:49:12 tonyortiz Exp $
037: * --------------------------------------------------------------------------
038: */
039: package org.objectweb.jotm.jta.jeremie;
040:
041: import org.objectweb.jeremie.services.handler.api.Service;
042: import org.objectweb.jotm.TransactionContext;
043: import org.objectweb.jonathan.apis.kernel.Context;
044: import org.objectweb.jonathan.apis.kernel.JonathanException;
045: import org.objectweb.jonathan.presentation.api.MarshallerFactory;
046: import org.objectweb.jonathan.presentation.api.Marshaller;
047: import org.objectweb.jonathan.presentation.api.UnMarshaller;
048: import org.objectweb.jonathan.resources.api.Chunk;
049: import org.objectweb.jonathan.helpers.MessageHelpers;
050: import org.omg.IOP.ServiceContext;
051:
052: public class TSHandler implements Service {
053:
054: private JotmTransactionSender sender = null;
055: private JotmTransactionReceiver receiver = null;
056: private MarshallerFactory mf;
057: private int service_id = 0;
058:
059: /**
060: * Builds a new Jeremie transaction service handler instance.
061: * @param c unused
062: * @param used_components the components used to initialize the new TSHandler.
063: * @exception JonathanException if something goes wrong.
064: */
065: public TSHandler(Context c, Object[] used_components)
066: throws JonathanException {
067: int sid = ((Integer) used_components[0]).intValue();
068: if (sid != Integer.MAX_VALUE) {
069: service_id = sid;
070: }
071: try {
072: sender = (JotmTransactionSender) used_components[1];
073: receiver = (JotmTransactionReceiver) used_components[2];
074: mf = (MarshallerFactory) used_components[3];
075: } catch (Exception e) {
076: throw new JonathanException(e);
077: }
078: }
079:
080: /**
081: * Returns a request context.
082: * @return a service context.
083: */
084: public ServiceContext getRequestContext(int id, boolean r,
085: byte[] key, Context k) {
086: if (sender == null) {
087: return null;
088: }
089: TransactionContext ctx = sender.sending_request();
090: return encodeContext(ctx);
091: }
092:
093: /**
094: * Returns a reply context.
095: * @return a service context.
096: */
097: public ServiceContext getReplyContext(int id, Context k) {
098: if (receiver == null) {
099: return null;
100: }
101: TransactionContext ctx = receiver.sending_reply();
102: return encodeContext(ctx);
103: }
104:
105: /**
106: * This method is called by the services handler to let the operations
107: * related to the target service be performed on request arrival.
108: * @param context the service context of the request;
109: */
110: public void handleRequestContext(ServiceContext context, int id,
111: boolean r, byte[] key, Context k) {
112: if (receiver == null || context == null) {
113: return;
114: }
115: TransactionContext ctx = decodeContext(context);
116: if (ctx != null) {
117: receiver.received_request(ctx);
118: }
119: }
120:
121: /**
122: * This method is called by the services handler to let the operations
123: * related to the target service be performed on reply arrival.
124: * @param context the service context of the reply;
125: */
126: public void handleReplyContext(ServiceContext context, int id,
127: Context k) {
128: if (sender == null || context == null) {
129: return;
130: }
131: TransactionContext ctx = decodeContext(context);
132: if (ctx != null) {
133: sender.received_reply(ctx);
134: }
135: }
136:
137: /**
138: * Encodes a Transaction Service propagation context into an IOP service
139: * context.
140: * @param ctx the propagation context to be encoded.
141: * @return the resulting IOP service context.
142: */
143: private ServiceContext encodeContext(TransactionContext ctx) {
144: if (ctx == null) {
145: return null;
146: }
147: Marshaller marshaller = mf.newMarshaller();
148: byte[] byteArray = null;
149: try {
150: marshaller.writeValue(ctx);
151: byteArray = MessageHelpers.copy(marshaller);
152: marshaller.close();
153: } catch (Exception e) {
154: System.err.println("TSHandler.encodeContext: exception");
155: }
156: return new ServiceContext(service_id, byteArray);
157: }
158:
159: /**
160: * Decodes a Transaction Service propagation context from an IOP service
161: * context.
162: *
163: * @param ctx the IOP service context containing the encoded propagation
164: * context.
165: * @return the decoded propagation context.
166: */
167: private TransactionContext decodeContext(ServiceContext sc) {
168: if (sc == null || sc.context_data == null
169: || sc.context_data.length == 0) {
170: return null;
171: }
172: TransactionContext ctx = null;
173: byte[] byteArray = sc.context_data;
174: UnMarshaller unmarshaller = mf.newUnMarshaller(new Chunk(
175: byteArray, 0, byteArray.length), 0);
176: try {
177: ctx = (TransactionContext) unmarshaller.readValue();
178: unmarshaller.close();
179: } catch (Exception e) {
180: System.err.println("TSHandler.decodeContext: exception");
181: System.err.println(e.toString() + "\n");
182: }
183: return ctx;
184: }
185:
186: }
|