1 22 package org.jboss.invocation; 23 24 import java.io.Externalizable ; 25 import java.io.IOException ; 26 import java.io.ObjectInput ; 27 import java.io.ObjectOutput ; 28 import java.lang.reflect.UndeclaredThrowableException ; 29 30 import javax.transaction.Transaction ; 31 32 import org.jboss.invocation.Invocation; 33 import org.jboss.invocation.Invoker; 34 35 import org.jboss.proxy.Interceptor; 36 37 import org.jboss.system.Registry; 38 import org.jboss.util.id.GUID; 39 40 47 public class InvokerInterceptor 48 extends Interceptor 49 implements Externalizable 50 { 51 52 private static final long serialVersionUID = 2548120545997920357L; 53 54 55 private GUID invokerID = Invoker.ID; 56 57 58 protected Invoker remoteInvoker; 59 60 61 protected static Invoker localInvoker; 62 63 64 protected static Class invokerProxyHA; 65 66 static 67 { 68 try 69 { 70 invokerProxyHA = Class.forName("org.jboss.invocation.InvokerProxyHA"); 72 } 73 catch (Throwable ignored) 74 { 75 } 76 } 77 78 81 public static Invoker getLocal() 82 { 83 return localInvoker; 84 } 85 86 89 public static void setLocal(Invoker invoker) 90 { 91 localInvoker = invoker; 92 } 93 94 97 public InvokerInterceptor() 98 { 99 super(); 100 } 101 102 107 public boolean isLocal() 108 { 109 return invokerID.equals(Invoker.ID); 110 } 111 112 118 public boolean isLocal(Invocation invocation) 119 { 120 if (localInvoker == null) 122 return false; 123 124 if (isLocal() == false) 126 { 127 if (isClustered(invocation) == false) 129 return false; 130 } 131 132 return hasLocalTarget(invocation); 134 } 135 136 147 public boolean isClustered(Invocation invocation) 148 { 149 if (invokerProxyHA == null) 151 return false; 152 153 InvocationContext ctx = invocation.getInvocationContext(); 155 Invoker invoker = ctx.getInvoker(); 156 return invoker != null && invokerProxyHA.isAssignableFrom(invoker.getClass()); 157 } 158 159 165 public boolean hasLocalTarget(Invocation invocation) 166 { 167 return Registry.lookup(invocation.getObjectName()) != null; 168 } 169 170 174 public Object invoke(Invocation invocation) 175 throws Exception 176 { 177 if (isLocal(invocation)) 179 return invokeLocal(invocation); 180 else 181 return invokeInvoker(invocation); 182 } 183 184 191 protected Object invokeLocal(Invocation invocation) throws Exception 192 { 193 return localInvoker.invoke(invocation); 194 } 195 196 203 protected Object invokeMarshalled(Invocation invocation) throws Exception 204 { 205 MarshalledInvocation mi = new MarshalledInvocation(invocation); 206 MarshalledValue copy = new MarshalledValue(mi); 207 Invocation invocationCopy = (Invocation) copy.get(); 208 209 Transaction tx = invocation.getTransaction(); 211 invocationCopy.setTransaction(tx); 212 213 try 214 { 215 Object rtnValue = localInvoker.invoke(invocationCopy); 216 MarshalledValue mv = new MarshalledValue(rtnValue); 217 return mv.get(); 218 } 219 catch(Throwable t) 220 { 221 MarshalledValue mv = new MarshalledValue(t); 222 Throwable t2 = (Throwable ) mv.get(); 223 if( t2 instanceof Exception ) 224 throw (Exception ) t2; 225 else 226 throw new UndeclaredThrowableException (t2); 227 } 228 } 229 230 237 protected Object invokeInvoker(Invocation invocation) throws Exception 238 { 239 InvocationContext ctx = invocation.getInvocationContext(); 240 Invoker invoker = ctx.getInvoker(); 241 return invoker.invoke(invocation); 242 } 243 244 252 public void writeExternal(final ObjectOutput out) 253 throws IOException 254 { 255 out.writeObject(invokerID); 256 } 257 258 265 public void readExternal(final ObjectInput in) 266 throws IOException , ClassNotFoundException 267 { 268 invokerID = (GUID)in.readObject(); 269 } 270 } 271 | Popular Tags |