001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 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: ConnectorFactory.java 9148 2006-07-12 06:26:36Z durieuxp $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.jmx;
025:
026: import java.util.HashSet;
027: import java.util.Properties;
028: import java.util.Set;
029: import javax.naming.Context;
030: import javax.naming.InitialContext;
031: import javax.naming.NamingException;
032: import javax.naming.NameClassPair;
033: import javax.naming.NamingEnumeration;
034: import javax.rmi.PortableRemoteObject;
035:
036: // JOnAS Log
037: import org.objectweb.jonas.common.Log;
038:
039: // Monolog
040: import org.objectweb.util.monolog.api.Logger;
041: import org.objectweb.util.monolog.api.BasicLevel;
042:
043: /**
044: * Manages the current "jonas provided" RMIConnector used to communicate with the MBEanServer.
045: * @author Bruno Michel and Guillaume Riviere
046: */
047: public class ConnectorFactory {
048:
049: private static String currentRMIConnectorName = null;
050: private static RMIConnector rmic = null; // current RMIConnector
051: private static Context context = null;
052: private static Logger logger = Log
053: .getLogger("objectweb.org.jonas.jmx");
054:
055: /**
056: * @return Context the current application context, create an initial context
057: * if there is no current context.
058: */
059: public static Context getContext()
060: throws javax.naming.NamingException {
061: if (context == null) {
062: context = new InitialContext();
063: }
064: return context;
065: }
066:
067: /**
068: * @return String the name of the current RMI connector.
069: * If no RMI connector is selected, returns the first RMI connector
070: * found with the getRMIConnectorsNames method.
071: * Return null if no RMI connector is available.
072: */
073: public static String getCurrentRMIConnectorName() {
074: try {
075: if (currentRMIConnectorName == null) { // may be not set yet
076: HashSet connectorNames = (HashSet) getRMIConnectorsNames();
077: if (!connectorNames.isEmpty()) {
078: // Pick up the first connector name in the list
079: String firstName = (String) connectorNames
080: .toArray()[0];
081: setCurrentRMIConnectorName(firstName);
082: } // else, currentRMIConnector rests null
083: }
084: return currentRMIConnectorName;
085: } catch (javax.naming.NamingException ne) {
086: return null;
087: }
088: }
089:
090: /**
091: * Set the currentRMIConnectorName to the specified value, then lookup for the RMI connector
092: */
093: public static void setCurrentRMIConnectorName(String name)
094: throws NamingException {
095: currentRMIConnectorName = name;
096: // The rmi connector name may have changed, so we lookup for the corresponding rmi connector
097: lookupRMIConnector();
098: }
099:
100: /**
101: * Set the currentRMIConnectorName to null
102: */
103: public static void resetCurrentRMIConnectorName() {
104: currentRMIConnectorName = null;
105: }
106:
107: /**
108: * @return Set a set containning all RMI connector names available in the current context.
109: */
110: public static Set getRMIConnectorsNames()
111: throws javax.naming.NamingException {
112: try {
113: HashSet res = new HashSet();
114: Context ctx = getContext();
115: // looking for all object registered in this registry
116: for (NamingEnumeration e = ctx.list(""); e
117: .hasMoreElements();) {
118: String name = ((NameClassPair) e.nextElement())
119: .getName();
120: // we select only rmi connectors (start with 'RMIConnector')
121: if (name.startsWith("RMIConnector"))
122: res.add(name);
123: }
124: return res;
125: } catch (javax.naming.NamingException ne) {
126: throw new javax.naming.NamingException(
127: "Cannot enumerates the names bound in the named context: '"
128: + getJonasNamingServiceURL()
129: + "' (registry probably not launched).");
130: }
131: }
132:
133: /**
134: * @return String the value of the PROVIDER_URL property in the current context.
135: * If a javax.naming.NamingException is catch, the exception message is returned.
136: */
137: public static String getJonasNamingServiceURL() {
138: try {
139: return (String) getContext().getEnvironment().get(
140: Context.PROVIDER_URL);
141: } catch (javax.naming.NamingException ne) {
142: return ne.getMessage();
143: }
144: }
145:
146: /**
147: * Create a new naming context based on the given env. properties
148: * @param env properties to create a new naming context
149: */
150: public static void setNamingEnvCtx(Properties env)
151: throws javax.naming.NamingException {
152: context = new InitialContext(env);
153: rmic = null;
154: currentRMIConnectorName = null;
155: }
156:
157: /**
158: * Sets the PROVIDER_URL property to the specified value. Then, sets the current RMI connector to null.
159: */
160: public static void setJonasNamingServiceURL(String url)
161: throws javax.naming.NamingException {
162:
163: // Properties for the new naming context
164: Properties p = new Properties();
165:
166: // Current context
167: Context ctx = getContext();
168: // Re-use the same values for the INITIAL_CONTEXT_FACTORY and URL_PKG_PREFIXES
169: p.put(Context.INITIAL_CONTEXT_FACTORY, ctx.getEnvironment()
170: .get(Context.INITIAL_CONTEXT_FACTORY));
171: p.put(Context.URL_PKG_PREFIXES, ctx.getEnvironment().get(
172: Context.URL_PKG_PREFIXES));
173: // Use the url parameter for the PROVIDER_URL property
174: p.put(Context.PROVIDER_URL, url);
175:
176: try {
177: context = new InitialContext(p);
178: } catch (NamingException e) {
179: if (logger.isLoggable(BasicLevel.DEBUG)) {
180: logger.log(BasicLevel.DEBUG, "Cannot create context : "
181: + e);
182: logger.log(BasicLevel.DEBUG, "Environment used :");
183: logger
184: .log(
185: BasicLevel.DEBUG,
186: Context.INITIAL_CONTEXT_FACTORY
187: + " = "
188: + p
189: .get(Context.INITIAL_CONTEXT_FACTORY));
190: logger.log(BasicLevel.DEBUG, Context.URL_PKG_PREFIXES
191: + " = " + p.get(Context.URL_PKG_PREFIXES));
192: logger.log(BasicLevel.DEBUG, Context.PROVIDER_URL
193: + " = " + p.get(Context.PROVIDER_URL));
194: }
195: throw e;
196: }
197:
198: rmic = null;
199: currentRMIConnectorName = null;
200:
201: }
202:
203: /**
204: * @return RMIConnector the current RMI connector, if null, lookup for a new one.
205: */
206: public static RMIConnector getRMIConnector() throws NamingException {
207: if (rmic == null) {
208: lookupRMIConnector();
209: }
210: if (rmic == null) {
211: logger.log(BasicLevel.DEBUG, "Null RMIConnector");
212: }
213: return rmic;
214: }
215:
216: /**
217: * Lookup for the RMI connector registered with the currentRMIConnectorName JNDI name.
218: */
219: public static void lookupRMIConnector() throws NamingException {
220: String curr = getCurrentRMIConnectorName();
221: if (curr != null) {
222: rmic = (RMIConnector) PortableRemoteObject.narrow(
223: getContext().lookup(curr), RMIConnector.class);
224: }
225: }
226: }
|