1 37 package net.sourceforge.cruisecontrol.jmx; 38 39 import java.io.BufferedReader ; 40 import java.io.BufferedWriter ; 41 import java.io.ByteArrayInputStream ; 42 import java.io.File ; 43 import java.io.FileNotFoundException ; 44 import java.io.FileReader ; 45 import java.io.FileWriter ; 46 import java.io.IOException ; 47 import java.io.InputStream ; 48 import java.util.Iterator ; 49 import java.util.List ; 50 import java.util.Properties ; 51 52 import javax.management.InstanceNotFoundException ; 53 import javax.management.InvalidAttributeValueException ; 54 import javax.management.JMException ; 55 import javax.management.MBeanRegistrationException ; 56 import javax.management.MBeanServer ; 57 import javax.management.MalformedObjectNameException ; 58 import javax.management.Notification ; 59 import javax.management.NotificationBroadcasterSupport ; 60 import javax.management.ObjectName ; 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 79 public class CruiseControlControllerJMXAdaptor extends NotificationBroadcasterSupport 80 implements CruiseControlControllerJMXAdaptorMBean, CruiseControlController.Listener { 81 private static final Logger LOG = Logger.getLogger(CruiseControlControllerJMXAdaptor.class); 82 private static final Object SEQUENCE_LOCK = new Object (); 83 private static int sequence = 0; 84 private final CruiseControlController controller; 85 private ObjectName registeredName; 86 private MBeanServer server; 87 88 public CruiseControlControllerJMXAdaptor(CruiseControlController controlController) { 89 controller = controlController; 90 controller.addListener(this); 91 } 92 93 public Properties getVersionProperties() { 94 return controller.getVersionProperties(); 95 } 96 97 public String getConfigFileName() { 98 return controller.getConfigFile() != null ? controller.getConfigFile().getAbsolutePath() : ""; 99 } 100 101 public String getConfigFileContents() { 102 103 File theConfigFile = controller.getConfigFile(); 104 105 if (theConfigFile == null) { 107 return ""; 108 } 109 110 StringBuffer theResults = new StringBuffer (); 111 112 try { 113 BufferedReader theConfigFileReader = new BufferedReader ( 114 new FileReader (theConfigFile)); 115 116 theResults = new StringBuffer ((int) theConfigFile.length()); 118 119 String theCurrentLine = theConfigFileReader.readLine(); 120 while (theCurrentLine != null) { 121 theResults.append(theCurrentLine); 122 theCurrentLine = theConfigFileReader.readLine(); 123 } 124 } catch (FileNotFoundException fne) { 125 126 LOG.error("Configuration file not found", fne); 127 } catch (IOException ioe) { 129 130 LOG.error("Error reading config file for JMX", ioe); 131 } 133 134 return theResults.toString(); 135 } 136 137 public void setConfigFileContents(String contents) throws CruiseControlException { 138 139 File theConfigFile = controller.getConfigFile(); 140 141 if (theConfigFile == null) { 143 return; 144 } 145 146 validateConfig(contents); 147 148 try { 149 theConfigFile.mkdirs(); 151 theConfigFile.createNewFile(); 152 153 BufferedWriter out = new BufferedWriter (new FileWriter (theConfigFile)); 154 out.write(contents); 155 out.close(); 156 } catch (FileNotFoundException fne) { 157 LOG.error("Configuration file not found", fne); 158 } catch (IOException ioe) { 159 LOG.error("Error storing config file for JMX", ioe); 160 } 161 } 162 163 public void validateConfig(String contents) throws CruiseControlException { 164 165 InputStream in = new ByteArrayInputStream (contents.getBytes()); 166 Element config = Util.parseConfig(in); 167 168 new CruiseControlConfig().configure(config); 169 } 170 171 public void setConfigFileName(String fileName) throws InvalidAttributeValueException { 172 try { 173 controller.setConfigFile(fileName != null ? new File (fileName) : null); 174 } catch (CruiseControlException e) { 175 throw new InvalidAttributeValueException (e.getMessage()); 176 } 177 } 178 179 public List getProjects() { 180 return controller.getProjects(); 181 } 182 183 public List getBusyTasks() { 184 return ThreadQueue.getBusyTaskNames(); 185 } 186 187 public List 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 getBuildQueueStatus() { 228 return controller.getBuildQueueStatus(); 229 } 230 231 public void halt() { 232 controller.halt(); 233 } 234 235 public void register(MBeanServer server) throws JMException { 236 this.server = server; 237 this.registeredName = new ObjectName ("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 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 e) { 258 LOG.error("Could not register project " + project.getName(), e); 259 } 260 String 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 name = "CruiseControl Project:name=" + project.getName(); 267 LOG.debug("Removing project " + name); 268 try { 269 ObjectName projectName = new ObjectName (name); 270 server.unregisterMBean(projectName); 271 } catch (InstanceNotFoundException noProblem) { 272 } catch (MBeanRegistrationException noProblem) { 273 } catch (MalformedObjectNameException e) { 274 LOG.error("Could not unregister project " + project.getName(), e); 275 } 276 notifyChanged("projectRemoved", name); 277 } 278 279 280 288 private void notifyChanged(String event, String data) { 289 Notification notification = new Notification ( 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 |