1 22 package org.jboss.aspects.remoting; 23 24 import org.jboss.aop.Advised; 25 import org.jboss.aop.Dispatcher; 26 import org.jboss.aop.InstanceAdvised; 27 import org.jboss.aop.InstanceAdvisor; 28 import org.jboss.aop.proxy.ClassProxy; 29 import org.jboss.aop.proxy.ClassProxyFactory; 30 import org.jboss.aop.util.PayloadKey; 31 import org.jboss.aspects.security.SecurityClientInterceptor; 32 import org.jboss.aspects.tx.ClientTxPropagationInterceptor; 33 import org.jboss.ha.framework.interfaces.ClusteringTargetsRepository; 34 import org.jboss.ha.framework.interfaces.HAPartition; 35 import org.jboss.ha.framework.interfaces.LoadBalancePolicy; 36 import org.jboss.ha.framework.server.HATarget; 37 import org.jboss.logging.Logger; 38 import org.jboss.remoting.InvokerLocator; 39 40 import javax.naming.InitialContext ; 41 42 import java.util.HashMap ; 43 import java.util.Iterator ; 44 import java.util.Map ; 45 46 50 51 public class ClusteredRemoting implements ClusterConstants 52 { 53 private static final Logger log = Logger.getLogger(ClusteredRemoting.class); 54 55 public static ClassProxy clusterObject(Object objectId, Object obj, String partitionName, LoadBalancePolicy lb, InvokerLocator locator) 56 throws Exception 57 { 58 String proxyFamilyName = objectId.toString() + locator.getProtocol() + partitionName; 59 HAPartition partition = (HAPartition) new InitialContext ().lookup("/HAPartition/" + partitionName); 60 61 HATarget target = null; 62 Map families = null; 63 InstanceAdvisor advisor = null; 64 Class clazz; 65 66 if (obj instanceof Advised) 67 { 68 advisor = ((Advised) obj)._getInstanceAdvisor(); 69 clazz = obj.getClass(); 70 Dispatcher.singleton.registerTarget(objectId, obj); 71 } 72 else 73 { 74 clazz = obj.getClass(); 75 ClassProxy proxy = ClassProxyFactory.newInstance(obj.getClass()); 76 advisor = proxy._getInstanceAdvisor(); 77 advisor.insertInterceptor(new ForwardingInterceptor(obj)); 78 Dispatcher.singleton.registerTarget(objectId, proxy); 79 } 80 families = (Map ) advisor.getMetaData().getMetaData(CLUSTERED_REMOTING, CLUSTER_FAMILIES); 81 if (families != null) 82 { 83 target = (HATarget) families.get(proxyFamilyName); 84 if (target == null) 85 { 86 target = new HATarget(partition, proxyFamilyName, locator, HATarget.ENABLE_INVOCATIONS); 87 ClusteringTargetsRepository.initTarget(proxyFamilyName, target.getReplicants()); 88 families.put(proxyFamilyName, target); 89 } 90 } 91 else 92 { 93 families = new HashMap (); 94 target = new HATarget(partition, proxyFamilyName, locator, HATarget.ENABLE_INVOCATIONS); 95 ClusteringTargetsRepository.initTarget(proxyFamilyName, target.getReplicants()); 96 families.put(proxyFamilyName, target); 97 advisor.insertInterceptor(0, new ReplicantsManagerInterceptor(families)); 98 } 99 100 ClassProxy proxy = ClassProxyFactory.newInstance(clazz); 101 InstanceAdvisor proxyAdvisor = proxy._getInstanceAdvisor(); 102 proxyAdvisor.insertInterceptor(IsLocalInterceptor.singleton); 103 advisor.insertInterceptor(SecurityClientInterceptor.singleton); 104 advisor.insertInterceptor(ClientTxPropagationInterceptor.singleton); 105 proxyAdvisor.insertInterceptor(MergeMetaDataInterceptor.singleton); 106 proxyAdvisor.insertInterceptor(ClusterChooserInterceptor.singleton); 107 proxyAdvisor.insertInterceptor(InvokeRemoteInterceptor.singleton); 108 109 proxyAdvisor.getMetaData().addMetaData(CLUSTERED_REMOTING, 110 CLUSTER_FAMILY_WRAPPER, 111 new FamilyWrapper(proxyFamilyName, target.getReplicants()), 112 PayloadKey.AS_IS); 113 114 proxyAdvisor.getMetaData().addMetaData(CLUSTERED_REMOTING, 116 LOADBALANCE_POLICY, 117 lb, 118 PayloadKey.AS_IS); 119 120 proxyAdvisor.getMetaData().addMetaData(InvokeRemoteInterceptor.REMOTING, 121 InvokeRemoteInterceptor.SUBSYSTEM, 122 "AOP", 123 PayloadKey.AS_IS); 124 125 proxyAdvisor.getMetaData().addMetaData(Dispatcher.DISPATCHER, 126 Dispatcher.OID, 127 objectId, 128 PayloadKey.AS_IS); 129 130 return proxy; 131 } 132 133 public static void unregisterClusteredObject(Object object) 134 { 135 try 136 { 137 ClassProxy proxy = (ClassProxy) object; 138 InstanceAdvisor advisor = proxy._getInstanceAdvisor(); 139 140 String oid = (String ) advisor.getMetaData().getMetaData(Dispatcher.DISPATCHER, Dispatcher.OID); 141 InstanceAdvised registeredObject = (InstanceAdvised) Dispatcher.singleton.getRegistered(oid); 142 if (registeredObject == null) throw new NotRegisteredException(oid.toString() + " is not registered"); 143 Dispatcher.singleton.unregisterTarget(oid); 144 145 advisor = registeredObject._getInstanceAdvisor(); 146 Map families = (Map ) advisor.getMetaData().getMetaData(CLUSTERED_REMOTING, CLUSTER_FAMILIES); 147 Iterator it = families.values().iterator(); 148 while (it.hasNext()) 149 { 150 HATarget target = (HATarget) it.next(); 151 target.destroy(); 152 } 153 } 154 catch (Exception ignored) 155 { 156 log.trace("Ignored exception unregistering a clustered object", ignored); 157 } 158 } 159 } 160 | Popular Tags |