KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > tools > ant > 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.tools.ant.util;
23
24 import java.io.IOException JavaDoc;
25 import java.rmi.registry.LocateRegistry JavaDoc;
26 import java.rmi.registry.Registry JavaDoc;
27 import java.util.Hashtable JavaDoc;
28
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.MBeanServerFactory JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.management.remote.JMXConnectorServer JavaDoc;
33 import javax.management.remote.JMXConnectorServerFactory JavaDoc;
34 import javax.management.remote.JMXServiceURL JavaDoc;
35 import javax.management.remote.rmi.RMIConnectorServer JavaDoc;
36
37 /**
38  * Start a JMX server
39  *
40  * @author ddesjardins - eBMWebsourcing
41  */

42 public class ServerUtil {
43     
44     private final static String JavaDoc PETALS_DOMAIN = "Petals";
45     
46     private final static String JavaDoc TYPE = "type";
47     
48     private final static String JavaDoc NAME = "name";
49     
50     private final static String JavaDoc SERVICE_TYPE = "service";
51     
52     private final static String JavaDoc ADMIN_NAME = "Admin";
53     
54     private final static String JavaDoc INSTALLATION_NAME = "Installation";
55     
56     private final static String JavaDoc DEPLOYMENT_NAME = "Deployment";
57
58     /**
59      * Boolean to know if the server is started
60      */

61     public static boolean isStarted = false;
62
63     /**
64      * JMX connector
65      */

66     private static JMXConnectorServer JavaDoc cs;
67
68     /**
69      * Start the JMX server
70      *
71      * @param port port of the server
72      * @throws Exception
73      */

74     public static int start(int port) throws Exception JavaDoc {
75         if (isStarted == false) {
76             Registry JavaDoc registry = LocateRegistry.getRegistry(port);
77             try {
78                 registry.list();
79             }
80             catch (Exception JavaDoc e){
81                 LocateRegistry.createRegistry(port);
82             }
83             MBeanServer JavaDoc beanServer = MBeanServerFactory.newMBeanServer();
84             
85             Hashtable JavaDoc<String JavaDoc, String JavaDoc> attributes = new Hashtable JavaDoc<String JavaDoc, String JavaDoc>();
86             attributes.put(NAME, DEPLOYMENT_NAME);
87             attributes.put(TYPE, SERVICE_TYPE);
88             beanServer.registerMBean(new DeploymentServiceMoc(beanServer), new ObjectName JavaDoc(PETALS_DOMAIN, attributes));
89             
90             attributes.clear();
91             attributes.put(NAME, INSTALLATION_NAME);
92             attributes.put(TYPE, SERVICE_TYPE);
93             beanServer.registerMBean(new InstallationServiceMoc(beanServer), new ObjectName JavaDoc(PETALS_DOMAIN, attributes));
94             
95             attributes.clear();
96             attributes.put(NAME, ADMIN_NAME);
97             attributes.put(TYPE, SERVICE_TYPE);
98             beanServer.registerMBean(new AdminServiceMoc(beanServer), new ObjectName JavaDoc(PETALS_DOMAIN, attributes));
99             
100             String JavaDoc sJmxURL = "service:jmx:rmi:///jndi/rmi://127.0.0.1:" + port + "/management/rmi-jmx-connector";
101             JMXServiceURL JavaDoc jmxUrl = new JMXServiceURL JavaDoc(sJmxURL);
102             RMIConnectorServer JavaDoc connectorServer = (RMIConnectorServer JavaDoc) JMXConnectorServerFactory.newJMXConnectorServer(jmxUrl, null, beanServer);
103             cs = connectorServer;
104             cs.start();
105             // Start the server
106
isStarted = true;
107         }
108         return port;
109     }
110
111     /**
112      * Stop the JMX
113      *
114      * @throws IOException Server
115      *
116      */

117     public static void stop() throws IOException JavaDoc {
118         if (isStarted) {
119             isStarted = false;
120             cs.stop();
121         }
122     }
123 }
124
Popular Tags