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: CmiClusterFactory.java 9764 2006-10-19 07:53:13Z 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 CMI 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 CmiClusterFactory 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 CmiClusterFactory(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 CMI 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=JGroups,*");
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=JGroups declared");
104: return false;
105: }
106: // ObjectName = "<domain>:type=CMI,J2EEServer=<server>,name=JGroups,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 CMICluster protocol="
119: + protocol);
120: String clusterName = on.getKeyProperty("channel");
121:
122: // Retrieve the CmiCluster. Create it if necessary.
123: // The channel should be a unique ident for it.
124: CmiCluster cluster = (CmiCluster) myclusters.get(clusterName);
125: if (cluster == null) {
126: ObjectName clon = null;
127: try {
128: cluster = new CmiCluster(this );
129: clon = cluster.setName(clusterName);
130: } catch (JMException e) {
131: logger.log(BasicLevel.ERROR,
132: "Cannot create CMI Cluster:" + e);
133: return false;
134: }
135:
136: cluster.setProtocol(protocol);
137:
138: // Get additional info
139: String strprop = (String) proxy.getAttribute(on,
140: "PropertiesAsString");
141: StringTokenizer stk = new StringTokenizer(strprop, "{},");
142: int nb = stk.countTokens();
143: for (int i = 0; i < nb; i++) {
144: String str = stk.nextToken();
145: int ind = str.indexOf('=', 0);
146: if (ind <= 0) {
147: logger
148: .log(BasicLevel.ERROR, "Bad property: "
149: + str);
150: }
151: String key = str.substring(0, ind).trim();
152: String value = str.substring(ind + 1).trim();
153: logger.log(BasicLevel.DEBUG, key + "=" + value);
154: if (key.equals("mcast_port")) {
155: int mcastPort = (new Integer(value)).intValue();
156: cluster.setMcastPort(mcastPort);
157: }
158: if (key.equals("mcast_addr")) {
159: cluster.setMcastAddr(value);
160: }
161: // TODO Add other parameters
162: }
163:
164: // Register the MBean if not done
165: if (!mbeanServer.isRegistered(clon)) {
166: try {
167: // A MBean is registered for each Cluster
168: mbeanServer.registerMBean(cluster, clon);
169: } catch (Exception e) {
170: logger.log(BasicLevel.ERROR,
171: "Cannot register cluster:" + e);
172: return false;
173: }
174: }
175: myclusters.put(clusterName, cluster);
176: }
177:
178: // add a server to the cluster
179: return cluster.addCmiServer(serverName, proxy);
180: }
181:
182: public Collection getClusterList() {
183: return myclusters.values();
184: }
185:
186: }
|