KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ha > framework > server > JChannelFactory


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
23 package org.jboss.ha.framework.server;
24
25 import java.net.InetAddress JavaDoc;
26 import java.rmi.dgc.VMID JavaDoc;
27 import java.rmi.server.UID JavaDoc;
28
29 import javax.management.Attribute JavaDoc;
30 import javax.management.AttributeList JavaDoc;
31 import javax.management.InstanceNotFoundException JavaDoc;
32 import javax.management.MBeanServer JavaDoc;
33 import javax.management.ReflectionException JavaDoc;
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 /**
45  * Extension to the JGroups JChannelFactory that supports the addition
46  * of "additional_data" to the channel config. Needed until logical
47  * addresses are supported in JGroups.
48  *
49  * @author <a HREF="mailto://brian.stansberry@jboss.com">Brian Stansberry</a>
50  * @version $Revision$
51  */

52 public class JChannelFactory extends org.jgroups.jmx.JChannelFactory
53 {
54    protected static Logger log = Logger.getLogger(JChannelFactory.class);
55    
56    private InetAddress JavaDoc nodeAddress;
57    private String JavaDoc nodeName;
58
59    public Channel createMultiplexerChannel(String JavaDoc stack_name, String JavaDoc id, boolean register_for_state_transfer, String JavaDoc substate_id) throws Exception JavaDoc
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          // We push the independent name in the protocol stack before connecting to the cluster
67
if (this.nodeName == null || "".equals(this.nodeName)) {
68             this.nodeName = generateUniqueNodeName();
69          }
70          java.util.HashMap JavaDoc staticNodeName = new java.util.HashMap JavaDoc();
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 JavaDoc getNodeAddress()
80    {
81       return nodeAddress;
82    }
83
84
85    public void setNodeAddress(InetAddress JavaDoc nodeAddress)
86    {
87       this.nodeAddress = nodeAddress;
88    }
89
90
91    public String JavaDoc getNodeName()
92    {
93       return nodeName;
94    }
95
96
97    public void setNodeName(String JavaDoc nodeName)
98    {
99       this.nodeName = nodeName;
100    }
101
102
103    private String JavaDoc generateUniqueNodeName () throws Exception JavaDoc
104    {
105       // we first try to find a simple meaningful name:
106
// 1st) "local-IP:JNDI_PORT" if JNDI is running on this machine
107
// 2nd) "local-IP:JMV_GUID" otherwise
108
// 3rd) return a fully GUID-based representation
109
//
110

111       // Before anything we determine the local host IP (and NOT name as this could be
112
// resolved differently by other nodes...)
113

114       // But use the specified node address for multi-homing
115

116       String JavaDoc hostIP = null;
117       InetAddress JavaDoc 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 JavaDoc().toString();
123       }
124       else
125       {
126          hostIP = address.getHostAddress();
127       }
128
129       // 1st: is JNDI up and running?
130
//
131
try
132       {
133          MBeanServer JavaDoc server = MBeanServerLocator.locateJBoss();
134          AttributeList JavaDoc al = server.getAttributes(NamingServiceMBean.OBJECT_NAME,
135                                       new String JavaDoc[] {"State", "Port"});
136
137          int status = ((Integer JavaDoc)((Attribute JavaDoc)al.get(0)).getValue()).intValue();
138          if (status == ServiceMBean.STARTED)
139          {
140             // we can proceed with the JNDI trick!
141
int port = ((Integer JavaDoc)((Attribute JavaDoc)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 JavaDoc 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 JavaDoc 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       // 2nd: host-GUID strategy
162
//
163
String JavaDoc uid = new UID JavaDoc().toString();
164       return hostIP + ":" + uid;
165    }
166    
167 }
168
Popular Tags