KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aspects > remoting > ClusteredRemoting


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

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 JavaDoc;
41
42 import java.util.HashMap JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.Map JavaDoc;
45
46 /**
47  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
48  * @version $Revision: 37406 $
49  */

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 JavaDoc objectId, Object JavaDoc obj, String JavaDoc partitionName, LoadBalancePolicy lb, InvokerLocator locator)
56    throws Exception JavaDoc
57    {
58       String JavaDoc proxyFamilyName = objectId.toString() + locator.getProtocol() + partitionName;
59       HAPartition partition = (HAPartition) new InitialContext JavaDoc().lookup("/HAPartition/" + partitionName);
60
61       HATarget target = null;
62       Map JavaDoc families = null;
63       InstanceAdvisor advisor = null;
64       Class JavaDoc 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 JavaDoc) 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 JavaDoc();
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       // LB policy should be AS_IS so that remoting framework downloads the classes
115
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 JavaDoc object)
134    {
135       try
136       {
137          ClassProxy proxy = (ClassProxy) object;
138          InstanceAdvisor advisor = proxy._getInstanceAdvisor();
139
140          String JavaDoc oid = (String JavaDoc) 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 JavaDoc families = (Map JavaDoc) advisor.getMetaData().getMetaData(CLUSTERED_REMOTING, CLUSTER_FAMILIES);
147          Iterator JavaDoc it = families.values().iterator();
148          while (it.hasNext())
149          {
150             HATarget target = (HATarget) it.next();
151             target.destroy();
152          }
153       }
154       catch (Exception JavaDoc ignored)
155       {
156          log.trace("Ignored exception unregistering a clustered object", ignored);
157       }
158    }
159 }
160
Popular Tags