1 22 package org.jboss.invocation.jrmp.server; 23 24 import java.rmi.MarshalledObject ; 25 26 import javax.management.MBeanServer ; 27 import javax.management.ObjectName ; 28 import javax.management.InstanceNotFoundException ; 29 import javax.management.ReflectionException ; 30 31 import org.jboss.invocation.jrmp.interfaces.JRMPInvokerProxyHA; 32 import org.jboss.invocation.Invocation; 33 import org.jboss.invocation.Invoker; 34 import org.jboss.invocation.InvokerHA; 35 import org.jboss.invocation.MarshalledInvocation; 36 import org.jboss.system.Registry; 37 38 import org.jboss.ha.framework.interfaces.HARMIResponse; 39 import org.jboss.ha.framework.server.HATarget; 40 import org.jboss.ha.framework.interfaces.LoadBalancePolicy; 41 import org.jboss.ha.framework.interfaces.GenericClusteringException; 42 43 import java.util.ArrayList ; 44 import java.util.HashMap ; 45 46 47 56 public class JRMPInvokerHA 57 extends JRMPInvoker 58 implements InvokerHA 59 { 60 protected HashMap beanMap = new HashMap (); 61 62 protected ObjectName serviceName; 63 64 67 public JRMPInvokerHA() 68 { 69 super(); 70 } 71 72 74 protected void startService() throws Exception 75 { 76 loadCustomSocketFactories(); 77 78 if (log.isDebugEnabled()) 79 { 80 log.debug("RMI Port='" + (rmiPort == ANONYMOUS_PORT ? 81 "Anonymous" : Integer.toString(rmiPort)+"'")); 82 log.debug("Client SocketFactory='" + (clientSocketFactory == null ? 83 "Default" : clientSocketFactory.toString()+"'")); 84 log.debug("Server SocketFactory='" + (serverSocketFactory == null ? 85 "Default" : serverSocketFactory.toString()+"'")); 86 log.debug("Server SocketAddr='" + (serverAddress == null ? 87 "Default" : serverAddress+"'")); 88 log.debug("SecurityDomain='" + (sslDomain == null ? 89 "None" : sslDomain+"'")); 90 } 91 92 exportCI(); 93 Registry.bind(getServiceName(), this); 94 } 95 96 protected void stopService() throws Exception 97 { 98 unexportCI(); 99 } 100 101 103 public void registerBean(ObjectName beanName, HATarget target) throws Exception 104 { 105 Integer hash = new Integer (beanName.hashCode()); 106 log.debug("registerBean: "+beanName); 107 108 if (beanMap.containsKey(hash)) 109 { 110 throw new IllegalStateException ("Trying to register bean with the existing hashCode"); 112 } 113 beanMap.put(hash, target); 114 } 115 116 public Invoker createProxy(ObjectName beanName, LoadBalancePolicy policy, 117 String proxyFamilyName) throws Exception 118 { 119 Integer hash = new Integer (beanName.hashCode()); 120 HATarget target = (HATarget) beanMap.get(hash); 121 if (target == null) 122 { 123 throw new IllegalStateException ("The bean hashCode not found"); 124 } 125 126 String familyName = proxyFamilyName; 127 if (familyName == null) 128 familyName= target.getAssociatedPartition().getPartitionName() + "/" + beanName; 129 130 JRMPInvokerProxyHA proxy = new JRMPInvokerProxyHA(target.getReplicants(), 131 policy, 132 familyName, 133 target.getCurrentViewId ()); 134 return proxy; 135 } 136 137 public void unregisterBean(ObjectName beanName) throws Exception 138 { 139 Integer hash = new Integer (beanName.hashCode()); 140 beanMap.remove(hash); 141 } 142 143 146 public Object invoke(Invocation invocation) 147 throws Exception 148 { 149 ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); 150 151 try 152 { 153 invocation.setTransaction(importTPC(((MarshalledInvocation) invocation).getTransactionPropagationContext())); 155 156 ObjectName mbean = (ObjectName ) Registry.lookup(invocation.getObjectName()); 158 long clientViewId = ((Long )invocation.getValue("CLUSTER_VIEW_ID")).longValue(); 159 160 HATarget target = (HATarget)beanMap.get(invocation.getObjectName()); 161 if (target == null) 162 { 163 throw new GenericClusteringException(GenericClusteringException.COMPLETED_NO, 172 "target is not/no more registered on this node"); 173 } 174 175 if (!target.invocationsAllowed ()) 176 throw new GenericClusteringException(GenericClusteringException.COMPLETED_NO, 177 "invocations are currently not allowed on this target"); 178 179 Object rtn = support.getServer().invoke(mbean, 181 "invoke", 182 new Object [] { invocation }, 183 Invocation.INVOKE_SIGNATURE); 184 185 HARMIResponse rsp = new HARMIResponse(); 186 187 if (clientViewId != target.getCurrentViewId()) 188 { 189 rsp.newReplicants = new ArrayList (target.getReplicants()); 190 rsp.currentViewId = target.getCurrentViewId(); 191 } 192 rsp.response = rtn; 193 194 return new MarshalledObject (rsp); 195 } 196 catch (InstanceNotFoundException e) 197 { 198 throw new GenericClusteringException(GenericClusteringException.COMPLETED_NO, e); 199 } 200 catch (ReflectionException e) 201 { 202 throw new GenericClusteringException(GenericClusteringException.COMPLETED_NO, e); 203 } 204 catch (Exception e) 205 { 206 org.jboss.mx.util.JMXExceptionDecoder.rethrow(e); 207 208 throw new org.jboss.util.UnreachableStatementException(); 210 } 211 finally 212 { 213 Thread.currentThread().setContextClassLoader(oldCl); 214 } 215 } 216 217 public ObjectName getServiceName() 218 { 219 return (serviceName == null ? support.getServiceName() : serviceName); 220 } 221 222 public void setServiceName(ObjectName serviceName) 223 { 224 this.serviceName = serviceName; 225 } 226 227 @Override 228 public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception 229 { 230 ObjectName result = super.preRegister(server, name); 231 232 if (!result.equals(getServiceName())) 233 throw new IllegalStateException ("JMX registration (" + result + 234 ") differs from our configured service name (" + 235 getServiceName() +")"); 236 237 return result; 238 } 239 240 241 242 243 } 244 245 | Popular Tags |