001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cluster.drm;
023:
024: import java.io.Serializable;
025: import java.util.List;
026: import javax.management.Notification;
027: import javax.naming.InitialContext;
028:
029: import org.jboss.ha.framework.interfaces.DistributedReplicantManager;
030: import org.jboss.ha.framework.interfaces.DistributedReplicantManager.ReplicantListener;
031: import org.jboss.ha.framework.interfaces.HAPartition;
032: import org.jboss.logging.Logger;
033: import org.jboss.mx.util.JBossNotificationBroadcasterSupport;
034:
035: /** Tests of the DistributedReplicantManager aspect of the HAPartition service.
036:
037: @author Scott.Stark@jboss.org
038: @version $Revision: 57211 $
039: */
040: public class DRMUser extends JBossNotificationBroadcasterSupport
041: implements IReplicants, ReplicantListener {
042: protected static Logger log = Logger.getLogger(DRMUser.class);
043:
044: protected DistributedReplicantManager drm;
045: protected String category = "DRMUser";
046: protected String partitionName = "DefaultPartition";
047: protected long sequence;
048:
049: public String getPartitionName() {
050: return partitionName;
051: }
052:
053: public void setPartitionName(String partitionName) {
054: this .partitionName = partitionName;
055: }
056:
057: public String getCategory() {
058: return category;
059: }
060:
061: public void setCategory(String category) {
062: this .category = category;
063: }
064:
065: public void start() throws Exception {
066: // Lookup the parition
067: InitialContext ctx = new InitialContext();
068: String jndiName = "/HAPartition/" + partitionName;
069: HAPartition partition = (HAPartition) ctx.lookup(jndiName);
070: drm = partition.getDistributedReplicantManager();
071: log
072: .debug("Obtained DistributedReplicantManager from partition="
073: + partitionName);
074: drm.registerListener(category, this );
075: // Bind the jboss.bind.address value into the DRM
076: String address = System.getProperty("jboss.bind.address");
077: drm.add(category, address);
078: log.info("Added: " + address + " under key: " + category);
079: }
080:
081: public void stop() throws Exception {
082: drm.remove(category);
083: drm.unregisterListener(category, this );
084: }
085:
086: public Serializable lookupLocalReplicant() {
087: return drm.lookupLocalReplicant(category);
088: }
089:
090: public Serializable lookupLocalReplicant(String key) {
091: return drm.lookupLocalReplicant(key);
092: }
093:
094: public List lookupReplicants() {
095: return drm.lookupReplicants(category);
096: }
097:
098: public List lookupReplicants(String key) {
099: return drm.lookupReplicants(key);
100: }
101:
102: public void add(String key, Serializable data) throws Exception {
103: drm.add(key, data);
104: }
105:
106: public void remove(String key) throws Exception {
107: drm.remove(key);
108: }
109:
110: private synchronized long nextSequence() {
111: return sequence++;
112: }
113:
114: public void replicantsChanged(String key, List newReplicants,
115: int newReplicantsViewId) {
116: NotifyData data = new NotifyData();
117: data.key = key;
118: data.newReplicants = newReplicants;
119: data.newReplicantsViewId = newReplicantsViewId;
120: String address = System.getProperty("jboss.bind.address");
121: long id = nextSequence();
122: Notification msg = new Notification("replicantsChanged", this ,
123: id, address);
124: msg.setUserData(data);
125: log.info("replicantsChanged, " + msg);
126: super.sendNotification(msg);
127: }
128: }
|