KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > jmx > JMXMain


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1999-2004 Gerald Brose
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) 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  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */

21
22 package org.jacorb.notification.jmx;
23
24 import java.util.Map JavaDoc;
25
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.MBeanServerFactory JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.StandardMBean JavaDoc;
30 import javax.management.remote.JMXConnectorServer JavaDoc;
31 import javax.management.remote.JMXConnectorServerFactory JavaDoc;
32 import javax.management.remote.JMXServiceURL JavaDoc;
33
34 import org.tanukisoftware.wrapper.WrapperListener;
35 import org.tanukisoftware.wrapper.WrapperManager;
36 import org.tanukisoftware.wrapper.jmx.WrapperManagerMBean;
37 import org.tanukisoftware.wrapper.jmx.WrapperManagerTestingMBean;
38
39 /**
40  * @author Alphonse Bendt
41  * @version $Id: JMXMain.java,v 1.2 2004/08/19 15:23:05 alphonse.bendt Exp $
42  */

43 public class JMXMain implements WrapperListener
44 {
45     private ObjectName JavaDoc notificationServiceName;
46     
47     private MBeanServer JavaDoc server;
48
49     private JMXConnectorServer JavaDoc cntorServer;
50
51     private Thread JavaDoc jmxConnectorRunner;
52
53     private JMXMain() throws Exception JavaDoc {
54         notificationServiceName = ObjectName.getInstance("NotificationService:mbean=EventChannelFactory");
55     }
56     
57     
58     public Integer JavaDoc start(String JavaDoc[] args)
59     {
60         init(args);
61
62         return null;
63     }
64
65     public int stop(int code)
66     {
67         try
68         {
69             server.invoke(notificationServiceName, "stop", null, null);
70             cntorServer.stop();
71         } catch (Exception JavaDoc e)
72         {
73             return 1;
74         }
75         return 0;
76     }
77
78     public void controlEvent(int event)
79     {
80         if (WrapperManager.isControlledByNativeWrapper())
81         {
82             // The Wrapper will take care of this event
83
}
84         else
85         {
86             // We are not being controlled by the Wrapper, so
87
// handle the event ourselves.
88

89             if ((event == WrapperManager.WRAPPER_CTRL_C_EVENT)
90                     || (event == WrapperManager.WRAPPER_CTRL_CLOSE_EVENT)
91                     || (event == WrapperManager.WRAPPER_CTRL_SHUTDOWN_EVENT))
92             {
93                 org.tanukisoftware.wrapper.WrapperManager.stop(0);
94             }
95         }
96     }
97
98     private void init(String JavaDoc[] args)
99     {
100         try
101         {
102             server = MBeanServerFactory.createMBeanServer();
103
104             JMXServiceURL JavaDoc address = getServiceURL();
105
106             // The environment map, null in this case
107
Map JavaDoc environment = null;
108
109             // Create the JMXCconnectorServer
110
cntorServer =
111                 JMXConnectorServerFactory.newJMXConnectorServer(address, environment,
112                     server);
113
114             registerNotificationService();
115
116             registerWrapperManager();
117
118             //registerWrapperManagerTesting();
119

120             // Start the JMXConnectorServer
121
jmxConnectorRunner = new Thread JavaDoc()
122             {
123                 public void run()
124                 {
125                     try
126                     {
127                         cntorServer.start();
128                     } catch (Exception JavaDoc e)
129                     {
130                         e.printStackTrace();
131                     }
132                 }
133             };
134
135             jmxConnectorRunner.setName("JMX Connector Runner");
136
137             jmxConnectorRunner.start();
138         } catch (Exception JavaDoc e)
139         {
140             e.printStackTrace();
141             
142             throw new RuntimeException JavaDoc();
143         }
144     }
145
146     private JMXServiceURL JavaDoc getServiceURL() throws Exception JavaDoc
147     {
148         ObjectName JavaDoc namingName = ObjectName.getInstance("naming:type=rmiregistry");
149         server.createMBean("mx4j.tools.naming.NamingService", namingName, null);
150         System.err.println("Starting NamingService");
151         
152         server.invoke(namingName, "start", null, null);
153         int namingPort = ((Integer JavaDoc) server.getAttribute(namingName, "Port")).intValue();
154
155         String JavaDoc jndiPath = "/jmxconnector";
156
157         JMXServiceURL JavaDoc address = new JMXServiceURL JavaDoc(
158                 "service:jmx:rmi://localhost/jndi/rmi://localhost:" + namingPort + jndiPath);
159         return address;
160     }
161
162     private void registerNotificationService() throws Exception JavaDoc
163     {
164         System.err.println("Registering NotificationService MBean");
165         
166         StandardMBean JavaDoc bean = new StandardMBean JavaDoc(new EventChannelFactoryControl(),
167                 EventChannelFactoryMBean.class);
168
169         server.registerMBean(bean, notificationServiceName);
170     }
171
172     private void registerWrapperManager() throws Exception JavaDoc
173     {
174         if (!WrapperManager.isControlledByNativeWrapper()) {
175             return;
176         }
177         
178         System.err.println("Registering WrapperMBean");
179         
180         ObjectName JavaDoc wrapperManagerName = ObjectName
181                 .getInstance("JavaServiceWrapper:service=WrapperManager");
182         StandardMBean JavaDoc wrapperManagerBean = new StandardMBean JavaDoc(
183                 new org.tanukisoftware.wrapper.jmx.WrapperManager(), WrapperManagerMBean.class);
184         server.registerMBean(wrapperManagerBean, wrapperManagerName);
185     }
186
187     private void registerWrapperManagerTesting() throws Exception JavaDoc
188     {
189         ObjectName JavaDoc wrapperManagerTestingName = ObjectName
190                 .getInstance("JavaServiceWrapper:service=WrapperManagerTesting");
191         StandardMBean JavaDoc wrapperManagerTestingBean = new StandardMBean JavaDoc(
192                 new org.tanukisoftware.wrapper.jmx.WrapperManagerTesting(),
193                 WrapperManagerTestingMBean.class);
194         server.registerMBean(wrapperManagerTestingBean, wrapperManagerTestingName);
195     }
196
197     public static void main(String JavaDoc[] args) throws Exception JavaDoc
198     {
199         WrapperManager.start(new JMXMain(), args);
200     }
201 }
Popular Tags