1 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 ; 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 65 public TSHandler(Context c, Object [] used_components) throws JonathanException { 66 int sid = ((Integer ) 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 e) { 75 throw new JonathanException(e); 76 } 77 } 78 79 83 public ServiceContext 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 96 public ServiceContext 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 109 public void handleRequestContext(ServiceContext 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 124 public void handleReplyContext(ServiceContext 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 140 private ServiceContext 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 e) { 151 System.err.println("TSHandler.encodeContext: exception"); 152 } 153 return new ServiceContext (service_id, byteArray); 154 } 155 156 164 private TransactionContext decodeContext(ServiceContext 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 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 |