KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > jmx > CruiseControlControllerAgent


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2004, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.jmx;
38
39 import java.io.IOException JavaDoc;
40 import java.rmi.NoSuchObjectException JavaDoc;
41 import java.rmi.RemoteException JavaDoc;
42 import java.util.HashMap JavaDoc;
43 import java.util.Map JavaDoc;
44 import java.util.Iterator JavaDoc;
45
46 import javax.management.Attribute JavaDoc;
47 import javax.management.MBeanServer JavaDoc;
48 import javax.management.MBeanServerFactory JavaDoc;
49 import javax.management.ObjectName JavaDoc;
50 import javax.management.remote.JMXConnectorServer JavaDoc;
51 import javax.management.remote.JMXConnectorServerFactory JavaDoc;
52 import javax.management.remote.JMXServiceURL JavaDoc;
53 import javax.naming.Context JavaDoc;
54
55 import mx4j.tools.adaptor.http.HttpAdaptor;
56 import mx4j.tools.naming.NamingService;
57 import mx4j.tools.naming.NamingServiceMBean;
58 import net.sourceforge.cruisecontrol.CruiseControlController;
59 import net.sourceforge.cruisecontrol.util.MainArgs;
60
61 import org.apache.log4j.Logger;
62
63 /**
64  * JMX agent for a ProjectController
65  *
66  * @author <a HREF="mailto:jcyip@thoughtworks.com">Jason Yip</a>
67  */

68 public class CruiseControlControllerAgent {
69     private static final Logger LOG = Logger.getLogger(CruiseControlControllerAgent.class);
70     private static final String JavaDoc JNDI_NAME = "/jndi/jrmp";
71
72     private HttpAdaptor httpAdaptor = new HttpAdaptor();
73     private int httpPort;
74     private NamingServiceMBean rmiRegistry;
75     private JMXConnectorServer JavaDoc connectorServer;
76     private int connectorServerPort;
77     private String JavaDoc path;
78     private String JavaDoc user;
79     private String JavaDoc password;
80
81     public CruiseControlControllerAgent(CruiseControlController controller, int httpPort,
82         int connectorServerPort, String JavaDoc user, String JavaDoc password, String JavaDoc xslPath) {
83         this.httpPort = httpPort;
84         this.connectorServerPort = connectorServerPort;
85         path = xslPath;
86         CruiseControlControllerJMXAdaptor controllerAdaptor = new CruiseControlControllerJMXAdaptor(controller);
87         this.user = user;
88         this.password = password;
89
90         Iterator JavaDoc i = MBeanServerFactory.findMBeanServer(null).iterator();
91         MBeanServer JavaDoc server = i.hasNext() ? (MBeanServer JavaDoc) i.next() : MBeanServerFactory.createMBeanServer();
92         try {
93             controllerAdaptor.register(server);
94         } catch (Exception JavaDoc e) {
95             LOG.error("Problem registering CruiseControlController HttpAdaptor", e);
96         }
97         try {
98             registerHttpAdaptor(server);
99         } catch (Exception JavaDoc e) {
100             LOG.error("Problem registering HttpAdaptor", e);
101         }
102         try {
103             registerConnectorServer(server);
104         } catch (Exception JavaDoc e) {
105             LOG.error("Problem registering ConnectorServer", e);
106         }
107         try {
108             ObjectName JavaDoc name = new ObjectName JavaDoc("Logger:name=root");
109             server.registerMBean(new LoggerController(Logger.getRootLogger()), name);
110         } catch (Exception JavaDoc e) {
111             LOG.error("Problem registering LoggerController for root-Logger", e);
112         }
113
114     }
115
116     public void start() {
117         if (useHttpAdaptor()) {
118             try {
119                 LOG.info("starting httpAdaptor");
120                 httpAdaptor.start();
121             } catch (IOException JavaDoc e) {
122                 LOG.error("Exception starting httpAdaptor", e);
123             }
124         }
125         if (useConnectorServer()) {
126             try {
127                 LOG.info("starting rmiRegistry");
128                 rmiRegistry.start();
129             } catch (RemoteException JavaDoc e) {
130                 if (e.getMessage().startsWith("Port already in use")) {
131                     LOG.warn("Port " + connectorServerPort + " is already in use, so no new rmiRegistry is started");
132                 } else {
133                     LOG.error("Exception starting rmiRegistry", e);
134                 }
135             }
136             try {
137                 LOG.info("starting connectorServer");
138                 connectorServer.start();
139             } catch (Exception JavaDoc e) {
140                 if (e.getMessage().startsWith("javax.naming.NameAlreadyBoundException")) {
141                     LOG.warn("Couldn't start connectorServer since its name (" + JNDI_NAME
142                             + ") is already bound; you might need to restart your rmi registry");
143                 } else {
144                     LOG.error("Exception starting connectorServer", e);
145                 }
146             }
147         }
148     }
149
150     public void stop() {
151         if (useHttpAdaptor() && httpAdaptor.isActive()) {
152             httpAdaptor.stop();
153         }
154         if (useConnectorServer()) {
155             if (connectorServer.isActive()) {
156                 try {
157                     LOG.info("stopping connectorServer");
158                     connectorServer.stop();
159                 } catch (IOException JavaDoc e) {
160                     LOG.error("IOException stopping connectorServer", e);
161                 }
162             }
163             if (rmiRegistry.isRunning()) {
164                 try {
165                     LOG.info("stopping rmiRegistry");
166                     rmiRegistry.stop();
167                 } catch (NoSuchObjectException JavaDoc e) {
168                     LOG.error("NoSuchObjectException stopping rmiRegistry", e);
169                 }
170             }
171         }
172     }
173
174     private void registerHttpAdaptor(MBeanServer JavaDoc server) throws Exception JavaDoc {
175         if (useHttpAdaptor()) {
176             httpAdaptor.setPort(httpPort);
177             System.setProperty("cruisecontrol.jmxport", String.valueOf(httpPort));
178             httpAdaptor.setHost("0.0.0.0");
179             ObjectName JavaDoc adaptorName = new ObjectName JavaDoc("Adapter:name=HttpAdaptor,httpPort=" + httpPort);
180             server.registerMBean(httpAdaptor, adaptorName);
181             ObjectName JavaDoc processorName = new ObjectName JavaDoc("Http:name=XSLTProcessor");
182             server.createMBean("mx4j.tools.adaptor.http.XSLTProcessor", processorName, null);
183             String JavaDoc pathInJar = "net/sourceforge/cruisecontrol/jmx/xsl";
184             if (path != null && !path.equals("")) {
185                 LOG.info("Starting HttpAdaptor with customized Stylesheets");
186                 server.setAttribute(processorName, new Attribute JavaDoc("File", path));
187             } else {
188                 LOG.info("Starting HttpAdaptor with CC-Stylesheets");
189                 server.setAttribute(processorName, new Attribute JavaDoc("PathInJar", pathInJar));
190             }
191             server.setAttribute(adaptorName, new Attribute JavaDoc("ProcessorName", processorName));
192             if (user != null && password != null) {
193                 LOG.info("This CruiseControl instance is password protected");
194                 httpAdaptor.setAuthenticationMethod("basic");
195                 httpAdaptor.addAuthorization(user, password);
196             }
197         }
198     }
199
200     private boolean useHttpAdaptor() {
201         return httpPort != MainArgs.NOT_FOUND;
202     }
203
204     private void registerConnectorServer(MBeanServer JavaDoc server) throws Exception JavaDoc {
205         if (useConnectorServer()) {
206             // Create and start the naming service
207
ObjectName JavaDoc naming = new ObjectName JavaDoc("Naming:type=rmiregistry");
208             rmiRegistry = new NamingService(connectorServerPort);
209             server.registerMBean(rmiRegistry, naming);
210             System.setProperty("cruisecontrol.rmiport", String.valueOf(connectorServerPort));
211
212             JMXServiceURL JavaDoc address = new JMXServiceURL JavaDoc("rmi", "localhost", 0, JNDI_NAME);
213
214             Map JavaDoc environment = new HashMap JavaDoc();
215             final String JavaDoc registryContextFactory = "com.sun.jndi.rmi.registry.RegistryContextFactory";
216             environment.put(Context.INITIAL_CONTEXT_FACTORY, registryContextFactory);
217             environment.put(Context.PROVIDER_URL, "rmi://localhost:" + connectorServerPort);
218
219             connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(address, environment, server);
220             ObjectName JavaDoc connServerName = new ObjectName JavaDoc("ConnectorServer:name=" + JNDI_NAME);
221             server.registerMBean(connectorServer, connServerName);
222         }
223     }
224
225     private boolean useConnectorServer() {
226         return connectorServerPort != MainArgs.NOT_FOUND;
227     }
228 }
229
Popular Tags