1 22 package org.jboss.ha.framework.test; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.ObjectInputStream ; 27 import java.io.ObjectOutputStream ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 31 import org.jboss.ha.framework.interfaces.ClusteringTargetsRepository; 32 import org.jboss.ha.framework.interfaces.FamilyClusterInfo; 33 import org.jboss.invocation.Invocation; 34 import org.jboss.invocation.InvocationContext; 35 import org.jboss.invocation.PayloadKey; 36 import org.jboss.invocation.ServiceUnavailableException; 37 import org.jboss.invocation.jrmp.interfaces.JRMPInvokerProxyHA; 38 39 47 48 public class ServiceUnavailableClientInterceptor extends org.jboss.proxy.Interceptor 49 { 50 51 53 54 private static final long serialVersionUID = 8830272856328720750L; 55 56 58 private String proxyFamilyName; 59 60 62 64 public ServiceUnavailableClientInterceptor () 65 { 66 } 67 68 70 72 74 public Object invoke (Invocation mi) throws Throwable 75 { 76 Object data = mi.getValue ("DO_FAIL_DURING_NEXT_CALL"); 77 78 if (data != null && 79 data instanceof java.lang.Boolean && 80 data.equals (java.lang.Boolean.TRUE)) 81 { 82 83 mi.setValue ("DO_FAIL_DURING_NEXT_CALL", Boolean.FALSE, PayloadKey.AS_IS); 85 86 if (proxyFamilyName == null) 87 { 88 proxyFamilyName = getProxyFamilyName(mi); 89 } 90 91 FamilyClusterInfo info = ClusteringTargetsRepository.getFamilyClusterInfo(proxyFamilyName); 93 List targets = info.getTargets(); 94 for (Iterator it = targets.iterator(); it.hasNext(); ) 95 info.removeDeadTarget(it.next()); 96 97 throw new ServiceUnavailableException("Service unavailable", 98 new Exception ("Test")); 99 } 100 101 return getNext().invoke(mi); 102 } 103 104 106 108 110 112 static String getProxyFamilyName(Invocation invocation) throws Exception 113 { 114 InvocationContext ctx = invocation.invocationContext; 115 JRMPInvokerProxyHA invoker = (JRMPInvokerProxyHA) ctx.getInvoker(); 116 117 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 118 ObjectOutputStream oos = new ObjectOutputStream (baos); 119 invoker.writeExternal(oos); 120 oos.close(); 121 byte[] bytes = baos.toByteArray(); 122 123 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 124 ObjectInputStream ois = new ObjectInputStream (bais); 125 Object targets = ois.readObject(); 126 Object loadBalancePolicy = ois.readObject(); 127 String proxyFamilyName = (String ) ois.readObject(); 128 ois.close(); 129 130 return proxyFamilyName; 131 } 132 } 133 | Popular Tags |