001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2006 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: EjbHaClusterFactory.java 9758 2006-10-18 06:28:50Z durieuxp $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.management.cluster;
025:
026: import java.util.Collection;
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.Set;
030: import java.util.StringTokenizer;
031:
032: import javax.management.JMException;
033: import javax.management.MalformedObjectNameException;
034: import javax.management.ObjectName;
035:
036: import org.objectweb.jonas.management.monitoring.DomainMonitor;
037: import org.objectweb.jonas.management.monitoring.ServerProxy;
038:
039: import org.objectweb.util.monolog.api.BasicLevel;
040:
041: /**
042: * Factory for HA clusters
043: * These Clusters are built dynamically, when a new server is discovered
044: * as being part of a cluster of this type.
045: * @author durieuxp
046: */
047: public class EjbHaClusterFactory extends ClusterFactory {
048:
049: /**
050: * List of TomcatCluster objects
051: * There may be more than 1 TomcatCluster in the domain.
052: * key = clusterName, value = TomcatCluster
053: */
054: private HashMap myclusters = new HashMap();
055:
056: /**
057: * Constructor
058: */
059: public EjbHaClusterFactory(DomainMonitor dm) {
060: super (dm);
061: }
062:
063: /**
064: * Look for a cluster by its name
065: * @param name fo the cluster
066: * @return cluster or null if not found
067: */
068: public BaseCluster getCluster(String name) {
069: return (BaseCluster) myclusters.get(name);
070: }
071:
072: /**
073: * A new server has been discovered.
074: * In case this server is recognized, it is added in a Cluster.
075: * If not, nothing is done.
076: * @param proxy The new ServerProxy
077: * @return True if recognized as a tomcat server.
078: */
079: public boolean notifyServer(ServerProxy proxy) {
080:
081: String serverName = proxy.getServerName();
082: logger.log(BasicLevel.DEBUG, serverName);
083:
084: // Determine if a HA Cluster MBean exists in the server
085: // referenced by this proxy.
086: ObjectName ons;
087: try {
088: ons = ObjectName.getInstance(domainName
089: + ":type=CMI,name=JGroupsHA,*");
090: } catch (MalformedObjectNameException e1) {
091: logger
092: .log(BasicLevel.ERROR,
093: "MalformedObjectNameException");
094: return false;
095: }
096: Set set = proxy.queryNames(ons);
097: if (set == null) {
098: logger.log(BasicLevel.DEBUG, "Cannot reach " + serverName);
099: return false;
100: }
101: if (set.isEmpty()) {
102: logger.log(BasicLevel.DEBUG,
103: "No CMI Cluster with name=JGroupsHA declared");
104: return false;
105: }
106: // ObjectName = "<domain>:type=CMI,J2EEServer=<server>,name=JGroupsHA,channel=<ch>,protocol="UDP"
107: ObjectName on = null;
108: String protocol = null;
109: for (Iterator it = set.iterator(); it.hasNext();) {
110: on = (ObjectName) it.next();
111: protocol = on.getKeyProperty("protocol");
112: if ("UDP".equals(protocol) || "TCP".equals(protocol)) {
113: break;
114: }
115: }
116:
117: // Found an MBean: Get information about this cluster
118: logger.log(BasicLevel.DEBUG, "Found EjbHaCluster protocol="
119: + protocol);
120: String clusterName = on.getKeyProperty("channel");
121:
122: // Retrieve the EjbHaCluster. Create it if necessary.
123: EjbHaCluster cluster = (EjbHaCluster) myclusters
124: .get(clusterName);
125: if (cluster == null) {
126: ObjectName clon = null;
127: try {
128: cluster = new EjbHaCluster(this );
129: clon = cluster.setName(clusterName);
130: } catch (JMException e) {
131: logger.log(BasicLevel.ERROR,
132: "Cannot create EJbHa Cluster:" + e);
133: return false;
134: }
135:
136: // Get additional info
137: String strprop = (String) proxy.getAttribute(on,
138: "PropertiesAsString");
139: StringTokenizer stk = new StringTokenizer(strprop, "{},");
140: int nb = stk.countTokens();
141: for (int i = 0; i < nb; i++) {
142: String str = stk.nextToken();
143: int ind = str.indexOf('=', 0);
144: if (ind <= 0) {
145: logger
146: .log(BasicLevel.ERROR, "Bad property: "
147: + str);
148: }
149: String key = str.substring(0, ind).trim();
150: String value = str.substring(ind + 1).trim();
151: logger.log(BasicLevel.DEBUG, key + "=" + value);
152: if (key.equals("mcast_port")) {
153: int mcastPort = (new Integer(value)).intValue();
154: cluster.setMcastPort(mcastPort);
155: }
156: if (key.equals("mcast_addr")) {
157: cluster.setMcastAddr(value);
158: }
159: // TODO Add other parameters
160: }
161:
162: // Register the MBean if not done
163: if (!mbeanServer.isRegistered(clon)) {
164: try {
165: // A MBean is registered for each Cluster
166: mbeanServer.registerMBean(cluster, clon);
167: } catch (Exception e) {
168: logger.log(BasicLevel.ERROR,
169: "Cannot register cluster:" + e);
170: return false;
171: }
172: }
173: myclusters.put(clusterName, cluster);
174: }
175:
176: // add a server to the cluster
177: return cluster.addHaServer(serverName, proxy);
178: }
179:
180: public Collection getClusterList() {
181: return myclusters.values();
182: }
183:
184: }
|