KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cluster > drm > DRMUser


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.test.cluster.drm;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.management.Notification JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28
29 import org.jboss.ha.framework.interfaces.DistributedReplicantManager;
30 import org.jboss.ha.framework.interfaces.DistributedReplicantManager.ReplicantListener;
31 import org.jboss.ha.framework.interfaces.HAPartition;
32 import org.jboss.logging.Logger;
33 import org.jboss.mx.util.JBossNotificationBroadcasterSupport;
34
35 /** Tests of the DistributedReplicantManager aspect of the HAPartition service.
36
37    @author Scott.Stark@jboss.org
38    @version $Revision: 58535 $
39 */

40 public class DRMUser extends JBossNotificationBroadcasterSupport
41    implements IReplicants, ReplicantListener
42 {
43    protected static Logger log = Logger.getLogger(DRMUser.class);
44
45    protected DistributedReplicantManager drm;
46    protected String JavaDoc category = "DRMUser";
47    protected String JavaDoc partitionName = "DefaultPartition";
48    protected long sequence;
49
50    public String JavaDoc getPartitionName()
51    {
52       return partitionName;
53    }
54    public void setPartitionName(String JavaDoc partitionName)
55    {
56       this.partitionName = partitionName;
57    }
58
59    public String JavaDoc getCategory()
60    {
61       return category;
62    }
63    public void setCategory(String JavaDoc category)
64    {
65       this.category = category;
66    }
67
68    public void start() throws Exception JavaDoc
69    {
70       // Lookup the parition
71
InitialContext JavaDoc ctx = new InitialContext JavaDoc();
72       String JavaDoc jndiName = "/HAPartition/" + partitionName;
73       HAPartition partition = (HAPartition) ctx.lookup(jndiName);
74       drm = partition.getDistributedReplicantManager();
75       log.debug("Obtained DistributedReplicantManager from partition="+partitionName);
76       drm.registerListener(category, this);
77       // Bind the jboss.bind.address value into the DRM
78
String JavaDoc address = System.getProperty("jboss.bind.address");
79       drm.add(category, address);
80       log.info("Added: "+address+" under key: "+category);
81    }
82    public void stop() throws Exception JavaDoc
83    {
84       drm.remove(category);
85       drm.unregisterListener(category, this);
86    }
87
88    public Serializable JavaDoc lookupLocalReplicant()
89    {
90       return drm.lookupLocalReplicant(category);
91    }
92    public Serializable JavaDoc lookupLocalReplicant(String JavaDoc key)
93    {
94       return drm.lookupLocalReplicant(key);
95    }
96
97    public List JavaDoc lookupReplicants()
98    {
99       return drm.lookupReplicants(category);
100    }
101    public List JavaDoc lookupReplicants(String JavaDoc key)
102    {
103       return drm.lookupReplicants(key);
104    }
105    public void add(String JavaDoc key, Serializable JavaDoc data)
106       throws Exception JavaDoc
107    {
108       drm.add(key, data);
109    }
110    public void remove(String JavaDoc key)
111       throws Exception JavaDoc
112    {
113       drm.remove(key);
114    }
115    private synchronized long nextSequence()
116    {
117       return sequence ++;
118    }
119
120    public void replicantsChanged(String JavaDoc key, List JavaDoc newReplicants, int newReplicantsViewId)
121    {
122       NotifyData data = new NotifyData();
123       data.key = key;
124       data.newReplicants = newReplicants;
125       data.newReplicantsViewId = newReplicantsViewId;
126       String JavaDoc address = System.getProperty("jboss.bind.address");
127       long id = nextSequence();
128       Notification JavaDoc msg = new Notification JavaDoc("replicantsChanged", this, id, address);
129       msg.setUserData(data);
130       log.info("replicantsChanged, "+msg);
131       super.sendNotification(msg);
132    }
133 }
134
Popular Tags