00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 package org.objectweb.jeremie.libs.services.CosTransactions.handler;
00032
00033 import org.objectweb.jonathan.apis.kernel.Context;
00034 import org.objectweb.jonathan.apis.kernel.Element;
00035 import org.objectweb.jonathan.apis.kernel.JonathanException;
00036 import org.objectweb.jonathan.apis.kernel.InternalException;
00037 import org.objectweb.jonathan.apis.presentation.MarshallerFactory;
00038 import org.objectweb.jonathan.apis.presentation.Marshaller;
00039 import org.objectweb.jonathan.apis.presentation.UnMarshaller;
00040 import org.objectweb.jonathan.apis.resources.Chunk;
00041 import org.objectweb.jonathan.libs.helpers.MessageHelpers;
00042
00043 import org.objectweb.david.apis.services.handler.Service;
00044 import org.objectweb.david.libs.services.CosTransactions.handler.TSHandler;
00045
00046 import org.omg.IOP.ServiceContext;
00047 import org.omg.CosTransactions.PropagationContext;
00048 import org.omg.CosTSPortability.Sender;
00049 import org.omg.CosTSPortability.Receiver;
00050
00051 import java.io.ByteArrayInputStream;
00052 import java.io.ObjectInputStream;
00053 import java.io.ByteArrayOutputStream;
00054 import java.io.ObjectOutputStream;
00055
00056 import java.util.Properties;
00057 import java.util.Enumeration;
00058 import java.util.Vector;
00059
00065 public class JRMITSHandler extends TSHandler {
00066
00068 protected MarshallerFactory mf;
00069
00084 protected JRMITSHandler(Context c,Object[] used_components)
00085 throws JonathanException {
00086 initialize(used_components);
00087 }
00088
00089 private void initialize(Object[] used_components)
00090 throws JonathanException {
00091 int sid = ((Integer) used_components[0]).intValue();
00092 if (sid != Integer.MAX_VALUE) {
00093 service_id = sid;
00094 }
00095
00096 try {
00097 Sender sender = (Sender) used_components[1];
00098 if (sender != null) {
00099 identify_sender(sender);
00100 }
00101 Receiver receiver = (Receiver) used_components[2];
00102 if (receiver != null) {
00103 identify_receiver(receiver);
00104 }
00105 mf = (MarshallerFactory) used_components[3];
00106 } catch (Exception e) {
00107 throw new JonathanException(e);
00108 }
00109 }
00110
00118 protected ServiceContext encodeContext(PropagationContext ctx) {
00119 if (ctx == null) {
00120 return null;
00121 }
00122 Marshaller marshaller = mf.newMarshaller();
00123 byte[] byteArray = null;
00124 try {
00125 marshaller.writeValue(ctx);
00126 byteArray = MessageHelpers.copy(marshaller);
00127 marshaller.close();
00128 } catch (Exception e) {
00129 System.err.println("JRMITSHandler.encodeContext: exception");
00130 e.printStackTrace();
00131 }
00132 return new ServiceContext(service_id, byteArray);
00133 }
00134
00143 protected PropagationContext decodeContext(ServiceContext sc) {
00144 if ((sc==null) || (sc.context_data==null) || (sc.context_data.length==0)) {
00145 return null;
00146 }
00147 PropagationContext ctx = null;
00148 byte[] byteArray = sc.context_data;
00149 UnMarshaller unmarshaller =
00150 mf.newUnMarshaller(new Chunk(byteArray, 0, byteArray.length), 0);
00151 try {
00152 ctx = (PropagationContext) unmarshaller.readValue();
00153 unmarshaller.close();
00154 } catch (Exception e) {
00155 System.err.println("JRMITSHandler.decodeContext: exception");
00156 System.err.println(e.toString() + "\n");
00157 }
00158 return ctx;
00159 }
00160
00161 }