KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jmx > ConnectorFactory


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ConnectorFactory.java,v 1.3 2005/04/28 08:43:24 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jmx;
27
28 import java.util.HashSet JavaDoc;
29 import java.util.Properties JavaDoc;
30 import java.util.Set JavaDoc;
31 import javax.naming.Context JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34 import javax.naming.NameClassPair JavaDoc;
35 import javax.naming.NamingEnumeration JavaDoc;
36 import javax.rmi.PortableRemoteObject JavaDoc;
37
38 // JOnAS Log
39
import org.objectweb.jonas.common.Log;
40
41 // Monolog
42
import org.objectweb.util.monolog.api.Logger;
43 import org.objectweb.util.monolog.api.BasicLevel;
44 /**
45  * Manages the current "jonas provided" RMIConnector used to communicate with the MBEanServer.
46  * @author Bruno Michel and Guillaume Riviere
47  */

48 public class ConnectorFactory {
49
50     private static String JavaDoc currentRMIConnectorName = null;
51     private static RMIConnector rmic = null; // current RMIConnector
52
private static Context JavaDoc context = null;
53     private static Logger logger = Log.getLogger("objectweb.org.jonas.jmx");
54
55     /**
56      * @return Context the current application context, create an initial context
57      * if there is no current context.
58      */

59     public static Context JavaDoc getContext() throws javax.naming.NamingException JavaDoc {
60         if (context == null) {
61             context = new InitialContext JavaDoc();
62         }
63         return context;
64     }
65
66     /**
67      * @return String the name of the current RMI connector.
68      * If no RMI connector is selected, returns the first RMI connector
69      * found with the getRMIConnectorsNames method.
70      * Return null if no RMI connector is available.
71      */

72     public static String JavaDoc getCurrentRMIConnectorName() {
73         try {
74             if (currentRMIConnectorName == null) { // may be not set yet
75
HashSet JavaDoc connectorNames = (HashSet JavaDoc)getRMIConnectorsNames();
76                 if (! connectorNames.isEmpty()) {
77                     // Pick up the first connector name in the list
78
String JavaDoc firstName = (String JavaDoc)connectorNames.toArray()[0];
79                     setCurrentRMIConnectorName(firstName);
80                 } // else, currentRMIConnector rests null
81
}
82             return currentRMIConnectorName;
83         } catch (javax.naming.NamingException JavaDoc ne) {
84             return null;
85         }
86     }
87
88     /**
89      * Set the currentRMIConnectorName to the specified value, then lookup for the RMI connector
90      */

91     public static void setCurrentRMIConnectorName(String JavaDoc name) throws NamingException JavaDoc {
92         currentRMIConnectorName = name;
93         // The rmi connector name may have changed, so we lookup for the corresponding rmi connector
94
lookupRMIConnector();
95     }
96
97     /**
98      * Set the currentRMIConnectorName to null
99      */

100     public static void resetCurrentRMIConnectorName() {
101         currentRMIConnectorName = null;
102     }
103
104     /**
105      * @return Set a set containning all RMI connector names available in the current context.
106      */

107     public static Set JavaDoc getRMIConnectorsNames() throws javax.naming.NamingException JavaDoc {
108         try {
109             HashSet JavaDoc res = new HashSet JavaDoc();
110             Context JavaDoc ctx = getContext();
111             // looking for all object registered in this registry
112
for (NamingEnumeration JavaDoc e = ctx.list(""); e.hasMoreElements(); ) {
113                 String JavaDoc name = ((NameClassPair JavaDoc)e.nextElement()).getName();
114                 // we select only rmi connectors (start with 'RMIConnector')
115
if (name.startsWith("RMIConnector"))
116                     res.add(name);
117             }
118             return res;
119         } catch (javax.naming.NamingException JavaDoc ne) {
120             throw new javax.naming.NamingException JavaDoc("Cannot enumerates the names bound in the named context: '"
121                                                    +getJonasNamingServiceURL()
122                                                    +"' (registry probably not launched).");
123         }
124     }
125
126     /**
127      * @return String the value of the PROVIDER_URL property in the current context.
128      * If a javax.naming.NamingException is catch, the exception message is returned.
129      */

130     public static String JavaDoc getJonasNamingServiceURL() {
131         try {
132             return (String JavaDoc)getContext().getEnvironment().get(Context.PROVIDER_URL);
133         } catch (javax.naming.NamingException JavaDoc ne) {
134             return ne.getMessage();
135         }
136     }
137
138     /**
139      * Create a new naming context based on the given env. properties
140      * @param env properties to create a new naming context
141      */

142     public static void setNamingEnvCtx(Properties JavaDoc env) throws javax.naming.NamingException JavaDoc {
143         context = new InitialContext JavaDoc(env);
144         rmic = null;
145         currentRMIConnectorName = null;
146     }
147
148     /**
149      * Sets the PROVIDER_URL property to the specified value. Then, sets the current RMI connector to null.
150      */

151     public static void setJonasNamingServiceURL(String JavaDoc url) throws javax.naming.NamingException JavaDoc {
152
153         // Properties for the new naming context
154
Properties JavaDoc p = new Properties JavaDoc();
155
156         // Current context
157
Context JavaDoc ctx = getContext();
158         // Re-use the same values for the INITIAL_CONTEXT_FACTORY and URL_PKG_PREFIXES
159
p.put(Context.INITIAL_CONTEXT_FACTORY, ctx.getEnvironment().get(Context.INITIAL_CONTEXT_FACTORY));
160         p.put(Context.URL_PKG_PREFIXES, ctx.getEnvironment().get(Context.URL_PKG_PREFIXES));
161         // Use the url parameter for the PROVIDER_URL property
162
p.put(Context.PROVIDER_URL, url);
163
164         try {
165             context = new InitialContext JavaDoc(p);
166         } catch (NamingException JavaDoc e) {
167             if (logger.isLoggable(BasicLevel.DEBUG)) {
168                 logger.log(BasicLevel.DEBUG, "Can' create context : " + e);
169                 logger.log(BasicLevel.DEBUG, "Environment used :");
170                 logger.log(BasicLevel.DEBUG, Context.INITIAL_CONTEXT_FACTORY + " = " + p.get(Context.INITIAL_CONTEXT_FACTORY));
171                 logger.log(BasicLevel.DEBUG, Context.URL_PKG_PREFIXES + " = " + p.get(Context.URL_PKG_PREFIXES));
172                 logger.log(BasicLevel.DEBUG, Context.PROVIDER_URL + " = " + p.get(Context.PROVIDER_URL));
173             }
174             throw e;
175         }
176
177         rmic = null;
178         currentRMIConnectorName = null;
179
180     }
181
182     /**
183      * @return RMIConnector the current RMI connector, if null, lookup for a new one.
184      */

185     public static RMIConnector getRMIConnector() throws NamingException JavaDoc {
186         if (rmic == null) {
187             lookupRMIConnector();
188         }
189         return rmic;
190     }
191
192     /**
193      * Lookup for the RMI connector registered with the currentRMIConnectorName JNDI name.
194      */

195     public static void lookupRMIConnector() throws NamingException JavaDoc {
196         String JavaDoc _currentRMIConnectorName = getCurrentRMIConnectorName();
197         if (_currentRMIConnectorName != null) {
198             rmic = (RMIConnector)PortableRemoteObject.narrow(getContext().lookup(_currentRMIConnectorName), RMIConnector.class);
199         }
200     }
201 }
202
Popular Tags