KickJava   Java API By Example, From Geeks To Geeks.

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


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001-2003, 2006, 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.BufferedReader JavaDoc;
40 import java.io.BufferedWriter JavaDoc;
41 import java.io.ByteArrayInputStream JavaDoc;
42 import java.io.File JavaDoc;
43 import java.io.FileNotFoundException JavaDoc;
44 import java.io.FileReader JavaDoc;
45 import java.io.FileWriter JavaDoc;
46 import java.io.IOException JavaDoc;
47 import java.io.InputStream JavaDoc;
48 import java.util.Iterator JavaDoc;
49 import java.util.List JavaDoc;
50 import java.util.Properties JavaDoc;
51
52 import javax.management.InstanceNotFoundException JavaDoc;
53 import javax.management.InvalidAttributeValueException JavaDoc;
54 import javax.management.JMException JavaDoc;
55 import javax.management.MBeanRegistrationException JavaDoc;
56 import javax.management.MBeanServer JavaDoc;
57 import javax.management.MalformedObjectNameException JavaDoc;
58 import javax.management.Notification JavaDoc;
59 import javax.management.NotificationBroadcasterSupport JavaDoc;
60 import javax.management.ObjectName JavaDoc;
61
62 import net.sourceforge.cruisecontrol.CruiseControlConfig;
63 import net.sourceforge.cruisecontrol.CruiseControlController;
64 import net.sourceforge.cruisecontrol.CruiseControlException;
65 import net.sourceforge.cruisecontrol.PluginDetail;
66 import net.sourceforge.cruisecontrol.PluginRegistry;
67 import net.sourceforge.cruisecontrol.PluginType;
68 import net.sourceforge.cruisecontrol.Project;
69 import net.sourceforge.cruisecontrol.util.Util;
70 import net.sourceforge.cruisecontrol.util.threadpool.ThreadQueue;
71
72 import org.apache.log4j.Logger;
73 import org.jdom.Element;
74
75 /**
76  *
77  * @author <a HREF="mailto:robertdw@users.sourceforge.net">Robert Watkins</a>
78  */

79 public class CruiseControlControllerJMXAdaptor extends NotificationBroadcasterSupport JavaDoc
80         implements CruiseControlControllerJMXAdaptorMBean, CruiseControlController.Listener {
81     private static final Logger LOG = Logger.getLogger(CruiseControlControllerJMXAdaptor.class);
82     private static final Object JavaDoc SEQUENCE_LOCK = new Object JavaDoc();
83     private static int sequence = 0;
84     private final CruiseControlController controller;
85     private ObjectName JavaDoc registeredName;
86     private MBeanServer JavaDoc server;
87
88     public CruiseControlControllerJMXAdaptor(CruiseControlController controlController) {
89         controller = controlController;
90         controller.addListener(this);
91     }
92
93     public Properties JavaDoc getVersionProperties() {
94         return controller.getVersionProperties();
95     }
96
97     public String JavaDoc getConfigFileName() {
98         return controller.getConfigFile() != null ? controller.getConfigFile().getAbsolutePath() : "";
99     }
100
101     public String JavaDoc getConfigFileContents() {
102
103         File JavaDoc theConfigFile = controller.getConfigFile();
104         
105         // guard clause
106
if (theConfigFile == null) {
107             return "";
108         }
109
110         StringBuffer JavaDoc theResults = new StringBuffer JavaDoc();
111
112         try {
113             BufferedReader JavaDoc theConfigFileReader = new BufferedReader JavaDoc(
114                     new FileReader JavaDoc(theConfigFile));
115
116             // approximate the size
117
theResults = new StringBuffer JavaDoc((int) theConfigFile.length());
118
119             String JavaDoc theCurrentLine = theConfigFileReader.readLine();
120             while (theCurrentLine != null) {
121                 theResults.append(theCurrentLine);
122                 theCurrentLine = theConfigFileReader.readLine();
123             }
124         } catch (FileNotFoundException JavaDoc fne) {
125
126             LOG.error("Configuration file not found", fne);
127             //throw new CruiseControlException("Configuration file not found");
128
} catch (IOException JavaDoc ioe) {
129
130             LOG.error("Error reading config file for JMX", ioe);
131             //throw new CruiseControlException("Error reading config file");
132
}
133
134         return theResults.toString();
135     }
136     
137     public void setConfigFileContents(String JavaDoc contents) throws CruiseControlException {
138         
139         File JavaDoc theConfigFile = controller.getConfigFile();
140
141         // guard clause if config file not set
142
if (theConfigFile == null) {
143             return;
144         }
145        
146         validateConfig(contents);
147         
148         try {
149             // ensure the file exists
150
theConfigFile.mkdirs();
151             theConfigFile.createNewFile();
152             
153             BufferedWriter JavaDoc out = new BufferedWriter JavaDoc(new FileWriter JavaDoc(theConfigFile));
154             out.write(contents);
155             out.close();
156         } catch (FileNotFoundException JavaDoc fne) {
157             LOG.error("Configuration file not found", fne);
158         } catch (IOException JavaDoc ioe) {
159             LOG.error("Error storing config file for JMX", ioe);
160         }
161     }
162     
163     public void validateConfig(String JavaDoc contents) throws CruiseControlException {
164
165         InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(contents.getBytes());
166         Element config = Util.parseConfig(in);
167         
168         new CruiseControlConfig().configure(config);
169     }
170     
171     public void setConfigFileName(String JavaDoc fileName) throws InvalidAttributeValueException JavaDoc {
172         try {
173             controller.setConfigFile(fileName != null ? new File JavaDoc(fileName) : null);
174         } catch (CruiseControlException e) {
175             throw new InvalidAttributeValueException JavaDoc(e.getMessage());
176         }
177     }
178
179     public List JavaDoc getProjects() {
180         return controller.getProjects();
181     }
182
183     public List JavaDoc getBusyTasks() {
184         return ThreadQueue.getBusyTaskNames();
185     }
186
187     public List JavaDoc getIdleTasks() {
188         return ThreadQueue.getIdleTaskNames();
189     }
190    
191     public PluginDetail[] getAvailableBootstrappers() {
192         return controller.getAvailableBootstrappers();
193     }
194     
195     public PluginDetail[] getAvailablePublishers() {
196         return controller.getAvailablePublishers();
197     }
198
199     public PluginDetail[] getAvailableSourceControls() {
200         return controller.getAvailableSourceControls();
201     }
202     
203     public PluginDetail[] getAvailablePlugins() {
204         return controller.getAvailablePlugins();
205     }
206     
207     public PluginType[] getAvailablePluginTypes() {
208         return controller.getAvailablePluginTypes();
209     }
210     
211     public PluginRegistry getPluginRegistry() {
212         return controller.getPluginRegistry();
213     }
214     
215     public void resume() {
216         controller.resume();
217     }
218
219     public void pause() {
220         controller.pause();
221     }
222     
223     public void reloadConfigFile() {
224         controller.reloadConfigFile();
225     }
226
227     public String JavaDoc getBuildQueueStatus() {
228         return controller.getBuildQueueStatus();
229     }
230
231     public void halt() {
232         controller.halt();
233     }
234
235     public void register(MBeanServer JavaDoc server) throws JMException JavaDoc {
236         this.server = server;
237         this.registeredName = new ObjectName JavaDoc("CruiseControl Manager:id=unique");
238         server.registerMBean(this, this.registeredName);
239         updateProjectMBeans();
240     }
241
242     private void updateProjectMBeans() {
243         LOG.debug("Updating project mbeans");
244         if (server != null) {
245             for (Iterator JavaDoc iterator = controller.getProjects().iterator(); iterator.hasNext();) {
246                 Project project = (Project) iterator.next();
247                 projectAdded(project);
248             }
249         }
250     }
251
252     public void projectAdded(Project project) {
253         try {
254             LOG.debug("Registering project mbean");
255             ProjectController projectController = new ProjectController(project);
256             projectController.register(server);
257         } catch (JMException JavaDoc e) {
258             LOG.error("Could not register project " + project.getName(), e);
259         }
260         String JavaDoc name = "CruiseControl Project:name=" + project.getName();
261         LOG.debug("Adding project " + project.getName());
262         notifyChanged("projectAdded", name);
263     }
264
265     public void projectRemoved(Project project) {
266         String JavaDoc name = "CruiseControl Project:name=" + project.getName();
267         LOG.debug("Removing project " + name);
268         try {
269             ObjectName JavaDoc projectName = new ObjectName JavaDoc(name);
270             server.unregisterMBean(projectName);
271         } catch (InstanceNotFoundException JavaDoc noProblem) {
272         } catch (MBeanRegistrationException JavaDoc noProblem) {
273         } catch (MalformedObjectNameException JavaDoc e) {
274             LOG.error("Could not unregister project " + project.getName(), e);
275         }
276         notifyChanged("projectRemoved", name);
277     }
278     
279     
280     /**
281      * Send a JMX notification that the list of projects has changed.
282      * This only needs to be done when the project list changes, not
283      * when a JMX server is registered.
284      * <p/>
285      * At the moment, we only absolutely know when a project is added or
286      * removed, but not when the configuration file is reloaded or changed.
287      */

288     private void notifyChanged(String JavaDoc event, String JavaDoc data) {
289         Notification JavaDoc notification = new Notification JavaDoc(
290                 "cruisecontrol." + event + ".event", this.registeredName,
291                 nextSequence());
292         notification.setUserData(data);
293         sendNotification(notification);
294         LOG.debug("Sent " + event + " event.");
295     }
296     
297     
298     private int nextSequence() {
299         synchronized (SEQUENCE_LOCK) {
300             return ++sequence;
301         }
302     }
303 }
304
Popular Tags