1 22 package org.jboss.invocation.http.server; 23 24 import java.io.Serializable ; 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 28 import javax.management.JMException ; 29 import javax.management.ObjectName ; 30 31 import org.jboss.mx.util.JMXExceptionDecoder; 32 import org.jboss.ha.framework.interfaces.HARMIResponse; 33 import org.jboss.ha.framework.server.HATarget; 34 import org.jboss.ha.framework.interfaces.LoadBalancePolicy; 35 import org.jboss.ha.framework.interfaces.GenericClusteringException; 36 import org.jboss.invocation.http.interfaces.HttpInvokerProxyHA; 37 import org.jboss.invocation.Invocation; 38 import org.jboss.invocation.Invoker; 39 import org.jboss.invocation.InvokerHA; 40 import org.jboss.system.Registry; 41 42 47 public class HttpInvokerHA extends HttpInvoker 48 implements InvokerHA 49 { 50 protected HashMap targetMap = new HashMap (); 51 52 54 protected void startService() 55 throws Exception 56 { 57 ObjectName name = super.getServiceName(); 59 Registry.bind(name, this); 60 super.checkInvokerURL(); 62 log.debug("Bound HttpHA invoker for JMX node"); 63 } 64 65 protected void stopService() 66 { 67 ObjectName name = super.getServiceName(); 69 Registry.unbind(name); 70 log.debug("Unbound HttpHA invoker for JMX node"); 71 } 72 73 protected void destroyService() 74 { 75 Registry.unbind(serviceName); 77 } 78 79 public void registerBean(ObjectName targetName, HATarget target) throws Exception 80 { 81 Integer hash = new Integer (targetName.hashCode()); 82 log.debug("Registered targetName("+targetName+"), hash="+hash 83 + ", target="+target); 84 if (targetMap.containsKey(hash)) 85 { 86 throw new IllegalStateException ("Duplicate targetName("+targetName 87 + ") hashCode: "+hash); 88 } 89 targetMap.put(hash, target); 90 } 91 92 public void unregisterBean(ObjectName targetName) throws Exception 93 { 94 Integer hash = new Integer (targetName.hashCode()); 95 targetMap.remove(hash); 96 log.debug("Unregistered targetName("+targetName+"), hash="+hash); 97 } 98 99 public Invoker createProxy(ObjectName targetName, LoadBalancePolicy policy, 100 String proxyFamilyName) 101 throws Exception 102 { 103 Integer hash = new Integer (targetName.hashCode()); 104 HATarget target = (HATarget) targetMap.get(hash); 105 if (target == null) 106 { 107 throw new IllegalStateException ("The targetName("+targetName 108 + "), hashCode("+hash+") not found"); 109 } 110 Invoker proxy = new HttpInvokerProxyHA(target.getReplicants(), target.getCurrentViewId (), 111 policy, proxyFamilyName); 112 return proxy; 113 } 114 115 public Serializable getStub() 116 { 117 return super.getInvokerURL(); 118 } 119 120 123 public Object invoke(Invocation invocation) 124 throws Exception 125 { 126 ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); 127 try 128 { 129 Integer nameHash = (Integer ) invocation.getObjectName(); 130 ObjectName mbean = (ObjectName ) Registry.lookup(nameHash); 131 132 Object [] args = {invocation}; 134 String [] sig = {"org.jboss.invocation.Invocation"}; 135 Object rtn = super.getServer().invoke(mbean, 136 "invoke", args, sig); 137 138 Long clientViewId = (Long ) invocation.getValue("CLUSTER_VIEW_ID"); 140 HARMIResponse rsp = new HARMIResponse(); 141 HATarget target = (HATarget) targetMap.get(nameHash); 142 if (target == null) 143 { 144 throw new IllegalStateException ("The name for hashCode("+nameHash+") was not found"); 145 } 146 if (clientViewId.longValue() != target.getCurrentViewId()) 147 { 148 rsp.newReplicants = new ArrayList (target.getReplicants()); 149 rsp.currentViewId = target.getCurrentViewId(); 150 } 151 rsp.response = rtn; 152 153 return rsp; 155 } 156 catch (Exception e) 157 { 158 e = (Exception ) JMXExceptionDecoder.decode(e); 160 if( e instanceof JMException ) 162 e = new GenericClusteringException (GenericClusteringException.COMPLETED_NO, e.getMessage()); 163 164 if( log.isTraceEnabled() ) 166 log.trace("operation failed", e); 167 throw e; 168 } 169 finally 170 { 171 Thread.currentThread().setContextClassLoader(oldCl); 172 } 173 } 174 } 175 | Popular Tags |