1 22 23 package org.jboss.ha.framework.server; 24 25 import java.net.InetAddress ; 26 import java.rmi.dgc.VMID ; 27 import java.rmi.server.UID ; 28 29 import javax.management.Attribute ; 30 import javax.management.AttributeList ; 31 import javax.management.InstanceNotFoundException ; 32 import javax.management.MBeanServer ; 33 import javax.management.ReflectionException ; 34 35 import org.jboss.logging.Logger; 36 import org.jboss.mx.util.MBeanServerLocator; 37 import org.jboss.naming.NamingServiceMBean; 38 import org.jboss.system.ServiceMBean; 39 import org.jboss.system.server.ServerConfigUtil; 40 import org.jgroups.Channel; 41 import org.jgroups.Event; 42 import org.jgroups.stack.IpAddress; 43 44 52 public class JChannelFactory extends org.jgroups.jmx.JChannelFactory 53 { 54 protected static Logger log = Logger.getLogger(JChannelFactory.class); 55 56 private InetAddress nodeAddress; 57 private String nodeName; 58 59 public Channel createMultiplexerChannel(String stack_name, String id, boolean register_for_state_transfer, String substate_id) throws Exception 60 { 61 Channel channel = super.createMultiplexerChannel(stack_name, id, register_for_state_transfer, substate_id); 62 63 IpAddress address = (IpAddress) channel.getLocalAddress(); 64 if (address == null) 65 { 66 if (this.nodeName == null || "".equals(this.nodeName)) { 68 this.nodeName = generateUniqueNodeName(); 69 } 70 java.util.HashMap staticNodeName = new java.util.HashMap (); 71 staticNodeName.put("additional_data", this.nodeName.getBytes()); 72 channel.down(new Event(Event.CONFIG, staticNodeName)); 73 74 } 75 return channel; 76 } 77 78 79 public InetAddress getNodeAddress() 80 { 81 return nodeAddress; 82 } 83 84 85 public void setNodeAddress(InetAddress nodeAddress) 86 { 87 this.nodeAddress = nodeAddress; 88 } 89 90 91 public String getNodeName() 92 { 93 return nodeName; 94 } 95 96 97 public void setNodeName(String nodeName) 98 { 99 this.nodeName = nodeName; 100 } 101 102 103 private String generateUniqueNodeName () throws Exception 104 { 105 111 114 116 String hostIP = null; 117 InetAddress address = ServerConfigUtil.fixRemoteAddress(nodeAddress); 118 if (address == null) 119 { 120 log.debug ("unable to create a GUID for this cluster, check network configuration is correctly setup (getLocalHost has returned an exception)"); 121 log.debug ("using a full GUID strategy"); 122 return new VMID ().toString(); 123 } 124 else 125 { 126 hostIP = address.getHostAddress(); 127 } 128 129 try 132 { 133 MBeanServer server = MBeanServerLocator.locateJBoss(); 134 AttributeList al = server.getAttributes(NamingServiceMBean.OBJECT_NAME, 135 new String [] {"State", "Port"}); 136 137 int status = ((Integer )((Attribute )al.get(0)).getValue()).intValue(); 138 if (status == ServiceMBean.STARTED) 139 { 140 int port = ((Integer )((Attribute )al.get(1)).getValue()).intValue(); 142 return hostIP + ":" + port; 143 } 144 else 145 { 146 log.debug("JNDI has been found but the service wasn't started so we cannot " + 147 "be entirely sure we are the only one that wants to use this PORT " + 148 "as a GUID on this host."); 149 } 150 151 } 152 catch (InstanceNotFoundException e) 153 { 154 log.debug ("JNDI not running here, cannot use this strategy to find a node GUID for the cluster"); 155 } 156 catch (ReflectionException e) 157 { 158 log.debug ("JNDI querying has returned an exception, cannot use this strategy to find a node GUID for the cluster"); 159 } 160 161 String uid = new UID ().toString(); 164 return hostIP + ":" + uid; 165 } 166 167 } 168 | Popular Tags |