KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > kernel > admin > util > ServerUtil


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: ServerUtil.java 13:57:33 ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.kernel.admin.util;
23
24 import java.io.IOException JavaDoc;
25 import java.rmi.registry.LocateRegistry JavaDoc;
26 import java.rmi.registry.Registry JavaDoc;
27
28 import javax.management.MBeanServer JavaDoc;
29 import javax.management.MBeanServerFactory JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.management.remote.JMXConnectorServer JavaDoc;
32 import javax.management.remote.JMXConnectorServerFactory JavaDoc;
33 import javax.management.remote.JMXServiceURL JavaDoc;
34 import javax.management.remote.rmi.RMIConnectorServer JavaDoc;
35
36 /**
37  * Start a JMX server
38  *
39  * @author ddesjardins - eBMWebsourcing
40  */

41 public class ServerUtil {
42
43     /**
44      * Boolean to know if the server is started
45      */

46     public static boolean isStarted = false;
47
48     /**
49      * JMX connector
50      */

51     private static JMXConnectorServer JavaDoc cs;
52
53     /**
54      * Start the JMX server
55      *
56      * @param port port of the server
57      * @throws Exception
58      */

59     public static int start(int port) throws Exception JavaDoc {
60         if (isStarted == false) {
61             Registry JavaDoc registry = LocateRegistry.getRegistry(port);
62             try {
63                 registry.list();
64             }
65             catch (Exception JavaDoc e){
66                 LocateRegistry.createRegistry(port);
67             }
68             MBeanServer JavaDoc beanServer = MBeanServerFactory.newMBeanServer();
69             beanServer.registerMBean(new AdminServiceMoc(beanServer), new ObjectName JavaDoc("FC/Petals/jbi/admin-impl@00001:itf=service"));
70             String JavaDoc sJmxURL = "service:jmx:rmi:///jndi/rmi://127.0.0.1:" + port + "/management/rmi-jmx-connector";
71             JMXServiceURL JavaDoc jmxUrl = new JMXServiceURL JavaDoc(sJmxURL);
72             RMIConnectorServer JavaDoc connectorServer = (RMIConnectorServer JavaDoc) JMXConnectorServerFactory.newJMXConnectorServer(jmxUrl, null, beanServer);
73             cs = connectorServer;
74             cs.start();
75             // Start the server
76
isStarted = true;
77         }
78         return port;
79     }
80
81     /**
82      * Stop the JMX
83      *
84      * @throws IOException Server
85      *
86      */

87     public static void stop() throws IOException JavaDoc {
88         if (isStarted) {
89             isStarted = false;
90             cs.stop();
91         }
92     }
93 }
94
Popular Tags