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: $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.management.cluster;
025:
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Iterator;
029:
030: import javax.management.MalformedObjectNameException;
031: import javax.management.ObjectName;
032:
033: import org.objectweb.jonas.jmx.CatalinaObjectName;
034: import org.objectweb.jonas.management.monitoring.DomainMonitor;
035: import org.objectweb.jonas.management.monitoring.ServerProxy;
036:
037: import org.objectweb.util.monolog.api.BasicLevel;
038:
039: /**
040: * Factory for clusters used by Apache mod-JK
041: * These Clusters are built dynamically, when a new server is discovered
042: * as being part of a cluster of this type.
043: * @author durieuxp
044: */
045: public class JkClusterFactory extends ClusterFactory {
046:
047: /**
048: * There is only 1 JKCluster, defined in "worker.properties", and
049: * its memebers are the workers with type="ajp13".
050: */
051: private JkCluster cluster = null;
052:
053: /**
054: * True when worker.properties has been looked for.
055: * We decided to look at this only when the first Worker has been started.
056: */
057: private boolean configSearched = false;
058:
059: /**
060: * Constructor
061: */
062: public JkClusterFactory(DomainMonitor dm) {
063: super (dm);
064: }
065:
066: /**
067: * Look for a cluster by its name
068: * @param name fo the cluster
069: * @return cluster or null if not found
070: */
071: public BaseCluster getCluster(String name) {
072: if (cluster != null && cluster.getName().equals(name)) {
073: return cluster;
074: } else {
075: return null;
076: }
077: }
078:
079: /**
080: * A new server has been discovered.
081: * In case this server is recognized, it is added in a Cluster.
082: * If not, nothing is done.
083: * @param proxy The new ServerProxy
084: * @return True if recognized as a mod_jk worker.
085: */
086: public boolean notifyServer(ServerProxy proxy) {
087:
088: String serverName = proxy.getServerName();
089: logger.log(BasicLevel.DEBUG, serverName);
090:
091: // Check if there is an Engine with a jvmRoute defined.
092: // This jvmRoute should match a Worker defined in workers.properties
093: String workerName = null;
094: ObjectName engineOn;
095: try {
096: engineOn = CatalinaObjectName.catalinaEngine(domainName);
097: } catch (MalformedObjectNameException e1) {
098: logger.log(BasicLevel.WARN, e1);
099: return false;
100: }
101: if (proxy.isRegistered(engineOn)) {
102: logger.log(BasicLevel.DEBUG,
103: "Found 1 engine. Look for jvmRoute");
104: workerName = (String) proxy.getAttribute(engineOn,
105: "jvmRoute");
106: }
107: if (workerName == null) {
108: logger.log(BasicLevel.DEBUG, serverName
109: + " is not a JK-Cluster server");
110: return false;
111: }
112: if (!configSearched) {
113: // First worker found: try to find the "workers.properties" file.
114: try {
115: // Build a unique JkCluster, based on workers.properties info.
116: cluster = new JkCluster(this );
117: } catch (Exception e) {
118: // No config file, or error in file.
119: // => We don't build the Cluster.
120: }
121: }
122: if (cluster == null) {
123: logger.log(BasicLevel.WARN,
124: "Bad configuration: No workers.properties for "
125: + workerName);
126: return false;
127: } else {
128: configSearched = true;
129: }
130:
131: // Check if the managed server has registered a AJP1.3 connector
132: int workerPort = 0;
133: ObjectName connectorOns;
134: try {
135: connectorOns = CatalinaObjectName
136: .catalinaConnectors(domainName);
137: } catch (MalformedObjectNameException e) {
138: logger.log(BasicLevel.WARN, e);
139: return false;
140: }
141: Iterator it = proxy.queryNames(connectorOns).iterator();
142: while (it.hasNext()) {
143: ObjectName connectorOn = (ObjectName) it.next();
144: String protocol = (String) proxy.getAttribute(connectorOn,
145: "protocol");
146: if ("AJP/1.3".equals(protocol)) {
147: workerPort = ((Integer) proxy.getAttribute(connectorOn,
148: "port")).intValue();
149: logger.log(BasicLevel.DEBUG,
150: "Detected server has AJP Connector MBean with port="
151: + workerPort);
152: }
153: }
154: if (workerPort == 0) {
155: logger.log(BasicLevel.WARN,
156: "No AJP/1.3 protocol: forget it");
157: return false;
158: }
159:
160: // add Worker To Cluster
161: return cluster.addWorker(workerName, workerPort, proxy);
162: }
163:
164: public Collection getClusterList() {
165: ArrayList ret = new ArrayList();
166: if (cluster != null) {
167: ret.add(cluster);
168: }
169: return ret;
170: }
171:
172: }
|