KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > jmx > RmiConnector


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Marc Wick.
20  * Contributor(s): ______________________.
21  */

22
23 package org.continuent.sequoia.controller.jmx;
24
25 import java.net.InetAddress JavaDoc;
26 import java.net.UnknownHostException JavaDoc;
27 import java.rmi.Remote JavaDoc;
28 import java.rmi.registry.LocateRegistry JavaDoc;
29 import java.rmi.server.UnicastRemoteObject JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.util.Hashtable JavaDoc;
32
33 import javax.management.Notification JavaDoc;
34 import javax.management.remote.JMXAuthenticator JavaDoc;
35 import javax.management.remote.JMXConnectorServer JavaDoc;
36 import javax.management.remote.rmi.RMIConnectorServer JavaDoc;
37 import javax.naming.Context JavaDoc;
38
39 import org.continuent.sequoia.common.authentication.PasswordAuthenticator;
40 import org.continuent.sequoia.common.i18n.Translate;
41 import org.continuent.sequoia.common.jmx.JmxException;
42 import org.continuent.sequoia.common.jmx.notifications.JmxNotification;
43 import org.continuent.sequoia.common.log.Trace;
44 import org.continuent.sequoia.common.net.RMISSLClientSocketFactory;
45 import org.continuent.sequoia.common.net.RMISSLServerSocketFactory;
46 import org.continuent.sequoia.common.net.SSLConfiguration;
47 import org.continuent.sequoia.common.net.SocketFactoryFactory;
48
49 /**
50  * This class defines a RmiConnector
51  *
52  * @author <a HREF="mailto:marc.wick@monte-bre.ch">Marc Wick </a>
53  * @version 1.0
54  */

55 public class RmiConnector
56 {
57   static Trace logger = Trace
58                                         .getLogger("org.continuent.sequoia.controller.jmx");
59
60   private String JavaDoc controllerName;
61   private String JavaDoc hostName;
62   private int port;
63   private JMXAuthenticator JavaDoc authenticator;
64   private SSLConfiguration sslConfig;
65
66   /**
67    * we have to keep a reference to the server to avoid it from being garbage
68    * collected otherwise the client will throw a java.rmi.NoSuchObjectException
69    * (problem experienced with ssl connections)
70    */

71   private JMXConnectorServer JavaDoc connection;
72   private Remote JavaDoc rmiRegistry;
73
74   /**
75    * Creates a new <code>RmiConnector.java</code> object
76    *
77    * @param controllerName for reference when sending notification
78    * @param hostName the name of the host we bind to, if null the default
79    * InetAddress.getLocalHost().getHostName() is used
80    * @param port the port the rmi registry is listening on
81    * @param authenticator the jmxauthenticator used for the connection
82    * @param sslConfig ssl configuration
83    * @throws JmxException the name of the localhost could not be determined
84    */

85   public RmiConnector(String JavaDoc controllerName, String JavaDoc hostName, int port,
86       JMXAuthenticator JavaDoc authenticator, SSLConfiguration sslConfig)
87       throws JmxException
88   {
89     if (hostName != null)
90     {
91       this.hostName = hostName;
92     }
93     else
94     {
95       try
96       {
97         /** TODO: dssmith - determine applicability of getLocalHost() */
98         this.hostName = InetAddress.getLocalHost().getHostName();
99       }
100       catch (UnknownHostException JavaDoc ex)
101       {
102         throw new JmxException(ex);
103       }
104     }
105     this.controllerName = controllerName;
106     this.port = port;
107     this.authenticator = authenticator;
108     this.sslConfig = sslConfig;
109   }
110
111   /**
112    * Returns the authenticator value.
113    *
114    * @return Returns the authenticator.
115    */

116   public JMXAuthenticator JavaDoc getAuthenticator()
117   {
118     return authenticator;
119   }
120
121   /**
122    * Sets the authenticator value.
123    *
124    * @param authenticator The authenticator to set.
125    */

126   public void setAuthenticator(JMXAuthenticator JavaDoc authenticator)
127   {
128     this.authenticator = authenticator;
129   }
130
131   /**
132    * Returns the port value.
133    *
134    * @return Returns the port.
135    */

136   public int getPort()
137   {
138     return port;
139   }
140
141   /**
142    * Sets the port value.
143    *
144    * @param port The port to set.
145    */

146   public void setPort(int port)
147   {
148     this.port = port;
149   }
150
151   /**
152    * Returns the sslConfig value.
153    *
154    * @return Returns the sslConfig.
155    */

156   public SSLConfiguration getSslConfig()
157   {
158     return sslConfig;
159   }
160
161   /**
162    * Sets the sslConfig value.
163    *
164    * @param sslConfig The sslConfig to set.
165    */

166   public void setSslConfig(SSLConfiguration sslConfig)
167   {
168     this.sslConfig = sslConfig;
169   }
170
171   /**
172    * Returns the connection value.
173    *
174    * @return Returns the connection.
175    */

176   public JMXConnectorServer JavaDoc getConnection()
177   {
178     return connection;
179   }
180
181   /**
182    * start the rmi connector and the rmi naming service
183    *
184    * @throws JmxException an exception
185    */

186   public void start() throws JmxException
187   {
188     createNamingService();
189     createJRMPAdaptor();
190   }
191
192   /**
193    * stop the rmi connector and the rmi registry
194    *
195    * @throws JmxException an exception
196    */

197   public void stop() throws JmxException
198   {
199     try
200     {
201       if (connection != null)
202         connection.stop();
203       if (rmiRegistry != null)
204         UnicastRemoteObject.unexportObject(rmiRegistry, true);
205     }
206     catch (Exception JavaDoc e)
207     {
208       throw new JmxException(e);
209     }
210     finally
211     {
212       connection = null;
213       rmiRegistry = null;
214     }
215   }
216
217   /**
218    * Create naming service and starts rmi
219    *
220    * @throws JmxException if creation fails
221    */

222   private void createNamingService() throws JmxException
223   {
224     try
225     {
226       // create and start the naming service
227
logger.info(Translate.get("jmx.create.naming.service", new String JavaDoc[]{""
228           + port}));
229       rmiRegistry = LocateRegistry.createRegistry(port);
230     }
231     catch (Exception JavaDoc e)
232     {
233       throw new JmxException(e);
234     }
235   }
236
237   private void createJRMPAdaptor() throws JmxException
238   {
239     try
240     {
241       // create the JRMP adaptator
242
logger.info(Translate.get("jmx.create.jrmp.adaptor", "" + port));
243
244       // Set the jndi name with which it will be registered
245
// JNDI properties
246
logger.debug(Translate.get("jmx.prepare.jndi"));
247
248       // see paragraph "Connector addresses based on directory entries"
249
// in javax.management.remote.rmi package summary
250

251       javax.management.remote.JMXServiceURL JavaDoc address = new javax.management.remote.JMXServiceURL JavaDoc(
252           "rmi", hostName, 0, "/jndi/jrmp");
253
254       java.util.Map JavaDoc environment = new java.util.HashMap JavaDoc();
255       environment.put(Context.INITIAL_CONTEXT_FACTORY,
256           "com.sun.jndi.rmi.registry.RegistryContextFactory");
257       environment.put(Context.PROVIDER_URL, "rmi://" + hostName + ":" + port);
258
259       if (authenticator == null)
260       {
261         authenticator = PasswordAuthenticator.NO_AUTHENICATION;
262       }
263
264       if (authenticator != null)
265       {
266         environment.put(JMXConnectorServer.AUTHENTICATOR, authenticator);
267       }
268
269       // ssl enabled ?
270
if (sslConfig != null)
271       {
272         logger.info(Translate.get("jmx.create.jrmp.ssl.enabled"));
273
274         RMISSLClientSocketFactory csf = new RMISSLClientSocketFactory();
275         RMISSLServerSocketFactory ssf = new RMISSLServerSocketFactory(
276             SocketFactoryFactory.createServerFactory(sslConfig));
277         environment.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE,
278             csf);
279         environment.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE,
280             ssf);
281       }
282
283       connection = javax.management.remote.JMXConnectorServerFactory
284           .newJMXConnectorServer(address, environment, MBeanServerManager
285               .getInstance());
286
287       connection.start();
288     }
289     catch (Exception JavaDoc e)
290     {
291       throw new JmxException(e);
292     }
293   }
294
295   /**
296    * @return Returns the controllerName.
297    */

298   public String JavaDoc getControllerName()
299   {
300     return controllerName;
301   }
302
303   /**
304    * @return Returns the hostName.
305    */

306   public String JavaDoc getHostName()
307   {
308     return hostName;
309   }
310
311   private Date JavaDoc myDate;
312   private long time;
313   private JmxNotification sequoiaNotification;
314   private Notification JavaDoc notification;
315   private static long sequence = 0;
316
317   /**
318    * This method sends notification to all client registered to an instance of
319    * the <code>RmiConnector</code> class. The <code>JmxNotification</code>
320    * class is used here to create an object with all the information gathered in
321    * parameters, and then is serialized in xml for interaction on the client
322    * side.
323    *
324    * @see JmxNotification
325    * @param mbean the mbean that is generating the notification
326    * @param type the type as seen in <code>SequoiaNotificationList</code>
327    * @param priority notification level as seen in
328    * <code>SequoiaNotificationList</code>
329    * @param description a string description of the notification
330    * @param data a hashtable of data that can be used to give more information
331    * on the notification
332    */

333   public synchronized void sendNotification(AbstractStandardMBean mbean,
334       String JavaDoc type, String JavaDoc priority, String JavaDoc description, Hashtable JavaDoc data)
335   {
336
337     myDate = new Date JavaDoc();
338     time = myDate.getTime();
339
340     sequoiaNotification = new JmxNotification(priority, "" + sequence, type,
341         description, "" + time, controllerName, mbean.getClass().getName(),
342         "mbeanName", hostName, "" + port, data);
343     notification = new Notification JavaDoc(type, mbean, sequence, myDate.getTime(),
344         description);
345     notification.setUserData(sequoiaNotification.toString());
346     mbean.sendNotification(notification);
347   }
348
349 }
Popular Tags