001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2006 Bull S.A.S.
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 1any 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: ClusterDaemonTools.java 9305 2006-08-02 09:11:31Z pelletib $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.cluster.daemon;
025:
026: import java.net.URI;
027: import java.util.Properties;
028:
029: import org.objectweb.jonas_clusterd.api.ClusterDaemonConfiguration;
030: import org.objectweb.jonas_clusterd.api.ClusterDaemonConfigurationException;
031: import org.objectweb.jonas_clusterd.lib.wrapper.ClusterDaemonConfigurationManagerWrapper;
032:
033: import org.objectweb.carol.rmi.util.PortNumber;
034: import org.objectweb.carol.util.configuration.CarolDefaultValues;
035: import org.objectweb.carol.util.configuration.ConfigurationRepository;
036: import org.objectweb.carol.util.configuration.ProtocolConfiguration;
037:
038: /**
039: * Commons tools
040: */
041:
042: public class ClusterDaemonTools {
043:
044: /**
045: * Current cluster configuration (loaded)
046: */
047: private static ClusterDaemonConfiguration currentConfiguration = null;
048:
049: /**
050: * constructor
051: */
052: private ClusterDaemonTools() {
053:
054: }
055:
056: /**
057: * Build the JMX url connection
058: * @param cdName cluster daemon name
059: * @return JMX url
060: * @throws ClusterDaemonException if an error occurs
061: */
062: public static String getJmxUrl(String cdName)
063: throws ClusterDaemonException {
064:
065: // Determine protocols used by Carol and their configuration, only the current one is considered
066: String serviceURL = null;
067: try {
068: ProtocolConfiguration protocolConfiguration = ConfigurationRepository
069: .getCurrentConfiguration();
070: ConfigurationRepository
071: .setCurrentConfiguration(protocolConfiguration);
072: String carolProtocol = protocolConfiguration.getName();
073: String providerUrl = protocolConfiguration.getProviderURL();
074: URI carolURL = new URI(providerUrl);
075: String host = carolURL.getHost();
076: String port = String.valueOf(carolURL.getPort());
077: String scheme = carolURL.getScheme();
078:
079: if (scheme.equals("rmi") && carolProtocol.equals("jrmp")) {
080: // Treat RMI/JRMP cas
081: String myName = "jrmpconnector_" + cdName;
082: serviceURL = "service:jmx:rmi://" + host;
083: int jrmpExportedPort = 0;
084: // Add port number of exported objects if one is set by carol.
085: String propertyName = CarolDefaultValues.SERVER_JRMP_PORT;
086: Properties p = ConfigurationRepository.getProperties();
087: if (p != null) {
088: jrmpExportedPort = PortNumber.strToint(p
089: .getProperty(propertyName, "0"),
090: propertyName);
091: }
092: if (jrmpExportedPort > 0) {
093: serviceURL += ":" + jrmpExportedPort;
094: }
095: serviceURL += "/jndi/rmi://" + host + ":" + port + "/"
096: + myName;
097: } else if (scheme.equals("rmi")
098: && carolProtocol.equals("irmi")) {
099: // Treat RMI/IRMI cas
100: String myName = "irmiconnector_" + cdName;
101: serviceURL = "service:jmx:rmi://" + host;
102: int irmiExportedPort = 0;
103: // Add port number of exported objects if one is set by carol.
104: String propertyName = CarolDefaultValues.SERVER_IRMI_PORT;
105: Properties p = ConfigurationRepository.getProperties();
106: if (p != null) {
107: irmiExportedPort = PortNumber.strToint(p
108: .getProperty(propertyName, "0"),
109: propertyName);
110: // Add 1 to this port for IRMI as the JMX object will not
111: // use IRMI to bind but JRMP methods.
112: irmiExportedPort++;
113: }
114: if (irmiExportedPort > 1) {
115: serviceURL += ":" + irmiExportedPort;
116: }
117: serviceURL += "/jndi/rmi://" + host + ":" + port + "/"
118: + myName;
119: } else if (scheme.equals("jrmi")) {
120: // Treat JEREMIE case
121: String myName = "jeremieconnector_" + cdName;
122: serviceURL = "service:jmx:rmi://" + host;
123: int jeremieExportedPort = 0;
124: // Add port number of exported objects if one is set by carol.
125: String propertyName = CarolDefaultValues.SERVER_JEREMIE_PORT;
126: Properties p = ConfigurationRepository.getProperties();
127: if (p != null) {
128: jeremieExportedPort = PortNumber.strToint(p
129: .getProperty(propertyName, "0"),
130: propertyName);
131: // Add 1 to this port for jeremie as the JMX object will not
132: // use jeremie to bind but JRMP methods.
133: jeremieExportedPort++;
134: }
135: if (jeremieExportedPort > 1024) {
136: serviceURL += ":" + jeremieExportedPort;
137: }
138: serviceURL += "/jndi/jrmi://" + host + ":" + port + "/"
139: + myName;
140:
141: } else if (scheme.equals("cmi")) {
142: // Treat CMI case
143: String myName = "cmiconnector_" + cdName;
144: serviceURL = "service:jmx:rmi://" + host;
145:
146: int jrmpExportedPort = 0;
147: // Add port number of exported objects if one is set by carol.
148: String propertyName = CarolDefaultValues.SERVER_JRMP_PORT;
149: Properties p = ConfigurationRepository.getProperties();
150: if (p != null) {
151: jrmpExportedPort = PortNumber.strToint(p
152: .getProperty(propertyName, "0"),
153: propertyName);
154: }
155: if (jrmpExportedPort > 0) {
156: serviceURL += ":" + jrmpExportedPort;
157: }
158: serviceURL += "/jndi/cmi://" + host + ":" + port + "/"
159: + myName;
160:
161: } else if (scheme.equals("iiop")) {
162: // Treat RMI/IIOP case
163: String myName = "iiopconnector_" + cdName;
164:
165: serviceURL = "service:jmx:iiop://" + host
166: + "/jndi/iiop://" + host + ":" + port + "/"
167: + myName;
168: }
169: } catch (Exception e) {
170: throw new ClusterDaemonException("Cannot get JMX URL", e);
171: }
172: return serviceURL;
173:
174: }
175:
176: /**
177: * Build the Object Name
178: * @return Object name
179: * @throws ClusterDaemonException if an error occurs
180: */
181: public static String getObjectName() throws ClusterDaemonException {
182:
183: // Determine protocols used by Carol and their configuration, only the current one is considered
184: String objName = null;
185: try {
186: ProtocolConfiguration protocolConfiguration = ConfigurationRepository
187: .getCurrentConfiguration();
188: ConfigurationRepository
189: .setCurrentConfiguration(protocolConfiguration);
190: String carolProtocol = protocolConfiguration.getName();
191: String providerUrl = protocolConfiguration.getProviderURL();
192: URI carolURL = new URI(providerUrl);
193: String scheme = carolURL.getScheme();
194:
195: if (scheme.equals("rmi") && carolProtocol.equals("jrmp")) {
196: // Treat RMI/JRMP cas
197: objName = "connectors:protocol=jrmp, name=cs_connector";
198: } else if (scheme.equals("rmi")
199: && carolProtocol.equals("irmi")) {
200: // Treat RMI/IRMI cas
201: objName = "connectors:protocol=irmi, name=cs_connector";
202: } else if (scheme.equals("jrmi")) {
203: // Treat JEREMIE case
204: objName = "connectors:protocol=jrmi, name=cs_connector";
205: } else if (scheme.equals("cmi")) {
206: // Treat CMI case
207: objName = "connectors:protocol=cmi, name=cs_connector";
208:
209: } else if (scheme.equals("iiop")) {
210: // Treat RMI/IIOP case
211: objName = "connectors:protocol=iiop, name=cs_connector";
212: }
213: } catch (Exception e) {
214: throw new ClusterDaemonException("Cannot get ObjectName", e);
215: }
216: return objName;
217:
218: }
219:
220: /**
221: * Load the cluster daemon configuration (clusterd.xml)
222: * @param confFileName configuration file to used
223: * @throws ClusterDaemonException if an error occurs
224: */
225: public static void loadClusterDaemonConfiguration(
226: String confFileName) throws ClusterDaemonException {
227: ClassLoader currentLoader = Thread.currentThread()
228: .getContextClassLoader();
229:
230: currentConfiguration = null;
231: try {
232: currentConfiguration = ClusterDaemonConfigurationManagerWrapper
233: .getClusterDaemonConfiguration(confFileName,
234: currentLoader);
235: } catch (ClusterDaemonConfigurationException e) {
236: throw new ClusterDaemonException(e);
237: }
238:
239: }
240:
241: /**
242: * @return current configuration
243: */
244: public static ClusterDaemonConfiguration getCurrentConfiguration() {
245: return currentConfiguration;
246: }
247:
248: }
|