1 28 package org.objectweb.carol.rmi.jrmp.interceptor; 29 30 import java.io.IOException ; 32 import java.io.ObjectInput ; 33 import java.io.ObjectOutput ; 34 import java.util.Collection ; 35 import java.util.Iterator ; 36 37 import org.objectweb.carol.util.configuration.TraceCarol; 38 39 46 public class JServerInterceptorHelper extends JInterceptorHelper { 47 48 51 private static InheritableThreadLocal threadCtx = new InheritableThreadLocal (); 52 53 59 public static void receive_request(ObjectInput in, JServerRequestInterceptor[] sis) throws IOException { 60 try { 61 int ctxValue = in.readInt(); 62 JServerRequestInfo jsr = new JRMPServerRequestInfoImpl(); 63 if ((sis == null) || (sis.length == 0)) { 64 if (TraceCarol.isDebugRmiCarol()) { 66 TraceCarol.debugRmiCarol("JServerInterceptorHelper receive request without interceptors"); 67 } 68 getRequestServerContextFromInput(in, ctxValue, jsr); 69 } else { 70 if (TraceCarol.isDebugRmiCarol()) { 72 TraceCarol.debugRmiCarol("JServerInterceptorHelper receive request contexts"); 73 } 74 JServerRequestInfo ri = getRequestServerContextFromInput(in, ctxValue, jsr); 75 for (int i = 0; i < sis.length; i++) { 76 sis[i].receive_request(ri); 77 } 78 } 79 } catch (ClassNotFoundException cnfe) { 80 throw new IOException ("" + cnfe); 81 } 82 } 83 84 90 public static void send_reply(ObjectOutput out, JServerRequestInterceptor[] sis) throws IOException { 91 if ((sis == null) || (sis.length == 0)) { 92 if (TraceCarol.isDebugRmiCarol()) { 93 TraceCarol.debugRmiCarol("JServerInterceptorHelper send reply without context"); 94 } 95 out.writeInt(NO_CTX); 97 } else { 98 JServerRequestInfo jsr = new JRMPServerRequestInfoImpl(); 99 for (int i = 0; i < sis.length; i++) { 100 sis[i].send_reply(jsr); 101 } 102 setServerContextInOutput(out, jsr, isLocal()); 103 threadCtx.set(null); 104 } 105 out.flush(); 107 } 108 109 115 public static void send_exception(ObjectOutput out, JServerRequestInterceptor[] sis) throws IOException { 116 if ((sis == null) || (sis.length == 0)) { 117 if (TraceCarol.isDebugRmiCarol()) { 118 TraceCarol.debugRmiCarol("JServerInterceptorHelper send exception without context"); 119 } 120 out.writeInt(NO_CTX); 122 } else { 123 JServerRequestInfo jsr = new JRMPServerRequestInfoImpl(); 124 if (TraceCarol.isDebugRmiCarol()) { 125 TraceCarol.debugRmiCarol("JServerInterceptorHelper send exception contexts"); 126 } 127 for (int i = 0; i < sis.length; i++) { 128 sis[i].send_exception(jsr); 129 } 130 setServerContextInOutput(out, jsr, isLocal()); 131 threadCtx.set(null); 132 } 133 out.flush(); 135 } 136 137 142 public static void send_other(ObjectOutput out, JServerRequestInterceptor[] sis) throws IOException { 143 if ((sis == null) || (sis.length == 0)) { 144 if (TraceCarol.isDebugRmiCarol()) { 145 TraceCarol.debugRmiCarol("JServerInterceptorHelper send other with no contexts"); 146 } 147 out.writeInt(NO_CTX); 149 } else { 150 JServerRequestInfo jsr = new JRMPServerRequestInfoImpl(); 151 for (int i = 0; i < sis.length; i++) { 152 sis[i].send_other(jsr); 153 } 154 setServerContextInOutput(out, jsr, isLocal()); 155 threadCtx.set(null); 156 } 157 out.flush(); 159 } 160 161 167 public static JServerRequestInfo getRequestServerContextFromInput(ObjectInput in, int ctxValue, 168 JServerRequestInfo jsr) throws ClassNotFoundException , IOException { 169 if (ctxValue == NO_CTX) { 170 if (TraceCarol.isDebugRmiCarol()) { 171 TraceCarol.debugRmiCarol("JServerInterceptorHelper receive no context"); 172 } 173 return jsr; 174 } else if (ctxValue == REMOTE_CTX) { 175 int sz = in.readInt(); 176 for (int i = 0; i < sz; i++) { 177 jsr.add_reply_service_context((JServiceContext) in.readObject()); 178 } 179 if (TraceCarol.isDebugRmiCarol()) { 181 TraceCarol.debugRmiCarol("JServerInterceptorHelper receive remote contexts"); 182 for (Iterator i = jsr.get_all_reply_service_context().iterator(); i.hasNext();) { 184 TraceCarol.debugRmiCarol("ctx:" + i.next()); 185 } 186 } 187 return jsr; 188 } else if (ctxValue == LOCAL_CTX) { 189 setLocal(); 190 int id = in.readInt(); 192 jsr.add_all_reply_service_context((Collection ) JContextStore.getObject(id)); 193 if (TraceCarol.isDebugRmiCarol()) { 195 TraceCarol.debugRmiCarol("JServerInterceptorHelper receive local contexts id(" + id + ")"); 196 for (Iterator i = jsr.get_all_reply_service_context().iterator(); i.hasNext();) { 198 TraceCarol.debugRmiCarol("ctx:" + i.next()); 199 } 200 } 201 return jsr; 202 } else { 203 throw new IOException ("Unknow context type:" + ctxValue); 204 } 205 } 206 207 213 public static void setServerContextInOutput(ObjectOutput out, JServerRequestInfo ri, boolean locRef) 214 throws IOException { 215 if (!ri.hasContexts()) { 216 if (TraceCarol.isDebugRmiCarol()) { 217 TraceCarol.debugRmiCarol("JServerInterceptorHelper send without contexts"); 218 } 219 out.writeInt(NO_CTX); 221 } else if (locRef) { 222 Object ctx = ri.get_all_reply_service_context(); 223 int k = JContextStore.storeObject(ctx); 224 out.writeInt(LOCAL_CTX); 225 out.writeInt(k); 226 if (TraceCarol.isDebugRmiCarol()) { 227 TraceCarol.debugRmiCarol("JServerInterceptorHelper send with local contexts id(" + k + ")"); 228 } 229 } else { 231 if (TraceCarol.isDebugRmiCarol()) { 232 TraceCarol.debugRmiCarol("JServerInterceptorHelper send with remote contexts"); 233 } 234 out.writeInt(REMOTE_CTX); 236 Collection allCtx = ri.get_all_reply_service_context(); 237 out.writeInt(allCtx.size()); 238 for (Iterator i = allCtx.iterator(); i.hasNext();) { 239 out.writeObject(i.next()); 240 } 241 } 242 } 243 244 247 public static void setLocal() { 248 threadCtx.set("local"); 249 } 250 251 255 public static boolean isLocal() { 256 return (threadCtx.get() != null); 257 } 258 259 } | Popular Tags |