1 22 package org.jboss.invocation.unified.server; 23 24 import java.rmi.MarshalledObject ; 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 28 import javax.management.MBeanServer ; 29 import javax.management.ObjectName ; 30 import org.jboss.ha.framework.interfaces.GenericClusteringException; 31 import org.jboss.ha.framework.interfaces.HARMIResponse; 32 import org.jboss.ha.framework.interfaces.LoadBalancePolicy; 33 import org.jboss.ha.framework.server.HATarget; 34 import org.jboss.invocation.Invocation; 35 import org.jboss.invocation.Invoker; 36 import org.jboss.invocation.InvokerHA; 37 import org.jboss.invocation.unified.interfaces.UnifiedInvokerHAProxy; 38 import org.jboss.mx.util.JMXExceptionDecoder; 39 import org.jboss.remoting.InvocationRequest; 40 import org.jboss.system.Registry; 41 42 45 public class UnifiedInvokerHA extends UnifiedInvoker implements InvokerHA 46 { 47 private HashMap beanMap = new HashMap (); 48 49 public UnifiedInvokerHA() 50 { 51 super(); 52 setSubSystem("invokerha"); 53 } 54 55 protected void jmxBind() 56 { 57 Registry.bind(getServiceName(), this); 58 } 59 60 public java.io.Serializable getStub() 61 { 62 return getInvoker().getLocator(); 63 } 64 65 public void registerBean(ObjectName beanName, HATarget target) throws Exception 66 { 67 Integer hash = new Integer (beanName.hashCode()); 68 69 if(beanMap.containsKey(hash)) 70 { 71 throw new IllegalStateException ("Trying to register bean (" + beanName + ") with an hash code that already exists"); 72 } 73 beanMap.put(hash, target); 74 } 75 76 public Invoker createProxy(ObjectName beanName, LoadBalancePolicy policy, String proxyFamilyName) 77 throws Exception 78 { 79 Integer hash = new Integer (beanName.hashCode()); 80 HATarget target = (HATarget) beanMap.get(hash); 81 if(target == null) 82 { 83 throw new IllegalStateException ("The bean hashCode not found"); 84 } 85 86 String familyName = proxyFamilyName; 87 if(familyName == null) 88 { 89 familyName = target.getAssociatedPartition().getPartitionName() + "/" + beanName; 90 } 91 92 UnifiedInvokerHAProxy proxy = new UnifiedInvokerHAProxy(getInvoker().getLocator(), getStrictRMIException(), 93 target.getReplicants(), 94 policy, proxyFamilyName, target.getCurrentViewId()); 95 return proxy; 96 97 } 98 99 public void unregisterBean(ObjectName beanName) throws Exception 100 { 101 Integer hash = new Integer (beanName.hashCode()); 102 beanMap.remove(hash); 103 } 104 105 113 public Object invoke(InvocationRequest invocationReq) throws Throwable 114 { 115 Invocation invocation = (Invocation) invocationReq.getParameter(); 116 Thread currentThread = Thread.currentThread(); 117 ClassLoader oldCl = currentThread.getContextClassLoader(); 118 ObjectName mbean = null; 119 try 120 { 121 mbean = (ObjectName ) Registry.lookup(invocation.getObjectName()); 122 123 124 long clientViewId = ((Long ) invocation.getValue("CLUSTER_VIEW_ID")).longValue(); 125 HATarget target = (HATarget) beanMap.get(invocation.getObjectName()); 126 if(target == null) 127 { 128 throw new GenericClusteringException(GenericClusteringException.COMPLETED_NO, 137 "target is not/no more registered on this node"); 138 } 139 140 if(!target.invocationsAllowed()) 141 { 142 throw new GenericClusteringException(GenericClusteringException.COMPLETED_NO, 143 "invocations are currently not allowed on this target"); 144 } 145 146 147 Object obj = getServer().invoke(mbean, 149 "invoke", 150 new Object []{invocation}, 151 Invocation.INVOKE_SIGNATURE); 152 153 154 155 HARMIResponse haResponse = new HARMIResponse(); 156 157 if(clientViewId != target.getCurrentViewId()) 158 { 159 haResponse.newReplicants = new ArrayList (target.getReplicants()); 160 haResponse.currentViewId = target.getCurrentViewId(); 161 } 162 haResponse.response = obj; 163 164 165 166 return new MarshalledObject (haResponse); 167 } 168 catch(Exception e) 169 { 170 Throwable th = JMXExceptionDecoder.decode(e); 171 if(log.isTraceEnabled()) 172 { 173 log.trace("Failed to invoke on mbean: " + mbean, th); 174 } 175 176 if(th instanceof Exception ) 177 { 178 e = (Exception ) th; 179 } 180 181 throw e; 182 } 183 finally 184 { 185 currentThread.setContextClassLoader(oldCl); 186 Thread.interrupted(); } 188 189 } 190 191 @Override 192 public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception 193 { 194 ObjectName result = super.preRegister(server, name); 195 log.info("Service name is " + getServiceName()); 196 return result; 197 } 198 199 200 201 } | Popular Tags |