KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > proxy > generic > ProxyFactoryHA


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.proxy.generic;
23
24 import java.util.List JavaDoc;
25
26 import javax.management.AttributeChangeNotificationFilter JavaDoc;
27 import javax.management.NotificationListener JavaDoc;
28 import javax.management.AttributeChangeNotification JavaDoc;
29 import javax.management.Notification JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31
32 import org.jboss.invocation.Invoker;
33 import org.jboss.invocation.InvokerHA;
34 import org.jboss.invocation.InvokerProxyHA;
35 import org.jboss.invocation.jrmp.server.JRMPProxyFactory;
36 import org.jboss.ha.framework.interfaces.LoadBalancePolicy;
37 import org.jboss.ha.framework.interfaces.DistributedReplicantManager;
38 import org.jboss.ha.framework.interfaces.HAPartition;
39 import org.jboss.ha.framework.interfaces.RoundRobin;
40 import org.jboss.ha.framework.server.HATarget;
41 import org.jboss.logging.Logger;
42 import org.jboss.proxy.GenericProxyFactory;
43 import org.jboss.system.Registry;
44 import org.jboss.system.ServiceMBean;
45
46 /**
47  * ProxyFactory for Clustering
48  *
49  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
50  * @version $Revision: 39164 $
51  */

52 public class ProxyFactoryHA
53    extends JRMPProxyFactory
54    implements ProxyFactoryHAMBean, DistributedReplicantManager.ReplicantListener
55 {
56    protected String JavaDoc replicantName = null;
57    protected InvokerHA invokerHA;
58    protected HATarget target;
59    protected Invoker invoker;
60    protected DistributedReplicantManager drm = null;
61    protected ObjectName JavaDoc partitionObjectName;
62    protected String JavaDoc loadBalancePolicy = RoundRobin.class.getName();
63    protected NotificationListener JavaDoc listener;
64    protected int state = 0;
65
66    public ObjectName JavaDoc getPartitionObjectName()
67    {
68       return partitionObjectName;
69    }
70
71    public void setPartitionObjectName(ObjectName JavaDoc partitionObjectName)
72    {
73       this.partitionObjectName = partitionObjectName;
74    }
75    
76    public String JavaDoc getLoadBalancePolicy()
77    {
78       return loadBalancePolicy;
79    }
80
81    public void setLoadBalancePolicy(String JavaDoc loadBalancePolicy)
82    {
83       this.loadBalancePolicy = loadBalancePolicy;
84    }
85
86    public void createService() throws Exception JavaDoc
87    {
88       super.createService();
89       
90       // we register our inner-class to retrieve STATE notifications from our container
91
AttributeChangeNotificationFilter JavaDoc filter = new AttributeChangeNotificationFilter JavaDoc();
92       filter.enableAttribute("State");
93       listener = new StateChangeListener();
94       getServer().addNotificationListener(getTargetName(), listener, filter, null);
95    }
96    
97    protected void startService() throws Exception JavaDoc
98    {
99       String JavaDoc partitionName = (String JavaDoc) getServer().getAttribute(partitionObjectName, "PartitionName");
100       HAPartition partition = (HAPartition) getServer().getAttribute(partitionObjectName, "HAPartition");
101       if (partition == null)
102          throw new RuntimeException JavaDoc("Partition is not registered: " + partitionObjectName);
103       this.drm = partition.getDistributedReplicantManager ();
104       
105       replicantName = getTargetName().toString();
106       
107       invokerHA = (InvokerHA) Registry.lookup(getInvokerName());
108       if (invokerHA == null)
109          throw new RuntimeException JavaDoc("Invoker is not registered: " + getInvokerName());
110
111       int mode = HATarget.MAKE_INVOCATIONS_WAIT;
112       if (state == ServiceMBean.STARTED)
113          mode = HATarget.ENABLE_INVOCATIONS;
114       target = new HATarget(partition, replicantName, invokerHA.getStub(), mode);
115       invokerHA.registerBean(getTargetName(), target);
116
117       String JavaDoc clusterFamilyName = partitionName + "/" + getTargetName() + "/";
118       
119       // make ABSOLUTLY sure we do register with the DRM AFTER the HATarget
120
// otherwise we will refresh the *old* home in JNDI (ie before the proxy
121
// is re-generated)
122
drm.registerListener (replicantName, this);
123       
124       ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
125       Class JavaDoc clazz;
126       LoadBalancePolicy policy;
127       
128       clazz = cl.loadClass(loadBalancePolicy);
129       policy = (LoadBalancePolicy)clazz.newInstance();
130       invoker = invokerHA.createProxy(getTargetName(), policy, clusterFamilyName + "H");
131       
132       // JRMPInvokerProxyHA.colocation.add(new Integer(jmxNameHash));
133

134       super.startService();
135    }
136    
137    public void stopService() throws Exception JavaDoc
138    {
139       super.stopService();
140       
141       try
142       {
143          // JRMPInvokerProxyHA.colocation.remove(new Integer(jmxNameHash));
144
invokerHA.unregisterBean(getTargetName());
145          target.destroy();
146       }
147       catch (Exception JavaDoc ignored)
148       {
149          // ignore.
150
}
151       if (drm != null)
152          drm.unregisterListener(replicantName, this);
153    }
154
155    protected void destroyService() throws Exception JavaDoc
156    {
157       super.destroyService();
158       getServer().removeNotificationListener(getTargetName(), listener);
159    }
160
161    protected void containerIsFullyStarted ()
162    {
163       if (target != null)
164          target.setInvocationsAuthorization(HATarget.ENABLE_INVOCATIONS);
165    }
166    
167    protected void containerIsAboutToStop()
168    {
169       if (target != null)
170       {
171          target.setInvocationsAuthorization(HATarget.DISABLE_INVOCATIONS);
172          target.disable();
173       }
174    }
175
176    // synchronized keyword added when it became possible for DRM to issue
177
// concurrent replicantsChanged notifications. JBAS-2169.
178
public synchronized void replicantsChanged(String JavaDoc key,
179                                               List JavaDoc newReplicants,
180                                               int newReplicantsViewId)
181    {
182       try
183       {
184          if (invoker instanceof InvokerProxyHA)
185             ((InvokerProxyHA) invoker).updateClusterInfo(target.getReplicants(), target.getCurrentViewId());
186
187          log.debug ("Rebinding in JNDI... " + key);
188          rebind();
189       }
190       catch (Exception JavaDoc none)
191       {
192          log.debug(none);
193       }
194    }
195
196    protected void createProxy
197    (
198       Object JavaDoc cacheID,
199       String JavaDoc proxyBindingName,
200       ClassLoader JavaDoc loader,
201       Class JavaDoc[] ifaces
202    )
203    {
204       GenericProxyFactory proxyFactory = new GenericProxyFactory();
205       theProxy = proxyFactory.createProxy(cacheID, getTargetName(), invoker,
206          getJndiName(), proxyBindingName, getInterceptorClasses(), loader, ifaces);
207    }
208
209    // inner-classes
210

211    class StateChangeListener implements NotificationListener JavaDoc
212    {
213       public void handleNotification (Notification JavaDoc notification, Object JavaDoc handback)
214       {
215          if (notification instanceof AttributeChangeNotification JavaDoc)
216          {
217             AttributeChangeNotification JavaDoc notif = (AttributeChangeNotification JavaDoc) notification;
218             state = ((Integer JavaDoc)notif.getNewValue()).intValue();
219             
220             if (state == ServiceMBean.STARTED)
221             {
222                log.debug ("Started: enabling remote access to mbean " + getTargetName());
223                containerIsFullyStarted ();
224             }
225             else if (state == ServiceMBean.STOPPING)
226             {
227                log.debug ("About to stop: disabling remote access to mbean " + getTargetName());
228                containerIsAboutToStop ();
229             }
230          }
231       }
232       
233    }
234 }
235
Popular Tags