1 37 package net.sourceforge.cruisecontrol.jmx; 38 39 import javax.management.InstanceNotFoundException ; 40 import javax.management.JMException ; 41 import javax.management.MBeanRegistrationException ; 42 import javax.management.MBeanServer ; 43 import javax.management.Notification ; 44 import javax.management.NotificationBroadcasterSupport ; 45 import javax.management.ObjectName ; 46 47 import net.sourceforge.cruisecontrol.CruiseControlException; 48 import net.sourceforge.cruisecontrol.LabelIncrementer; 49 import net.sourceforge.cruisecontrol.Project; 50 import net.sourceforge.cruisecontrol.events.BuildProgressEvent; 51 import net.sourceforge.cruisecontrol.events.BuildProgressListener; 52 import net.sourceforge.cruisecontrol.events.BuildResultEvent; 53 import net.sourceforge.cruisecontrol.events.BuildResultListener; 54 import net.sourceforge.cruisecontrol.labelincrementers.DefaultLabelIncrementer; 55 56 import org.apache.log4j.Logger; 57 58 62 public class ProjectController extends NotificationBroadcasterSupport 63 implements ProjectControllerMBean, BuildProgressListener, BuildResultListener { 64 65 private static final Logger LOG = Logger.getLogger(ProjectController.class); 66 67 private Project project; 68 private static int sequence = 0; 69 private static final Object SEQUENCE_LOCK = new Object (); 70 71 public ProjectController(Project project) { 72 this.project = project; 73 project.addBuildProgressListener(this); 74 project.addBuildResultListener(this); 75 } 76 77 private int nextSequence() { 78 synchronized (SEQUENCE_LOCK) { 79 return ++sequence; 80 } 81 } 82 83 public void handleBuildProgress(BuildProgressEvent event) { 84 log("build progress event: " + event.getState().getDescription()); 85 if (checkSourceProject(event.getProject())) { 86 Notification notification = new Notification ("cruisecontrol.progress.event", this, nextSequence()); 87 notification.setUserData(event.getState().getName()); 88 sendNotification(notification); 89 } 90 } 91 92 public void handleBuildResult(BuildResultEvent event) { 93 log("build result event: build " + String.valueOf(event.isBuildSuccessful() ? "successful" : "failed")); 94 if (checkSourceProject(event.getProject())) { 95 Notification notification = new Notification ("cruisecontrol.result.event", this, nextSequence()); 96 notification.setUserData((event.isBuildSuccessful()) ? Boolean.TRUE : Boolean.FALSE); 97 sendNotification(notification); 98 } 99 } 100 101 private boolean checkSourceProject(Project sourceProject) { 102 boolean projectsMatch = false; 103 if (project == sourceProject) { 104 projectsMatch = true; 105 } else { 106 if (sourceProject == null) { 107 LOG.warn("source project was null"); 108 } else { 109 LOG.warn("source project " + sourceProject.getName() 110 + " didn't match internal project " + project.getName()); 111 } 112 } 113 return projectsMatch; 114 } 115 116 public void pause() { 117 log("pausing"); 118 project.setPaused(true); 119 } 120 121 public void resume() { 122 log("resuming"); 123 project.setPaused(false); 124 } 125 126 public void build() { 127 log("forcing build"); 128 project.setBuildForced(true); 129 } 130 131 public void buildWithTarget(String buildTarget) { 132 log("forcing build with target \"" + buildTarget + "\""); 133 project.forceBuildWithTarget(buildTarget); 134 } 135 136 public void serialize() { 137 log("serializing"); 138 project.serializeProject(); 139 } 140 141 public boolean isPaused() { 142 return project.isPaused(); 143 } 144 145 public void setLabel(String label) { 146 log("setting label to [" + label + "]"); 147 project.setLabel(label); 148 } 149 150 public String getLabel() { 151 return project.getLabel(); 152 } 153 154 public void setLabelIncrementer(String classname) throws CruiseControlException { 155 log("setting label incrementer to [" + classname + "]"); 156 LabelIncrementer incrementer; 157 try { 158 incrementer = 159 (LabelIncrementer) Class.forName(classname).newInstance(); 160 } catch (Exception e) { 161 LOG.error( 162 "Error instantiating label incrementer." 163 + " Using DefaultLabelIncrementer.", 164 e); 165 incrementer = new DefaultLabelIncrementer(); 166 } 167 168 project.setLabelIncrementer(incrementer); 169 } 170 171 public String getLabelIncrementer() { 172 return project.getLabelIncrementer().getClass().getName(); 173 } 174 175 public void setLastBuild(String date) throws CruiseControlException { 176 log("setting last build to [" + date + "]"); 177 project.setLastBuild(date); 178 } 179 180 public String getLastBuild() { 181 return project.getLastBuild(); 182 } 183 184 public void setLastSuccessfulBuild(String date) 185 throws CruiseControlException { 186 log("setting last successful build to [" + date + "]"); 187 project.setLastSuccessfulBuild(date); 188 } 189 190 public String getLastSuccessfulBuild() { 191 return project.getLastSuccessfulBuild(); 192 } 193 194 public String getBuildStartTime() { 195 String buildStartTime = project.getBuildStartTime(); 196 return buildStartTime == null ? "" : buildStartTime; 197 } 198 199 public void setLogDir(String logdir) throws CruiseControlException { 200 log("setting log dir to [" + logdir + "]"); 201 project.getLog().setDir(logdir); 202 } 203 204 public String getLogDir() { 205 return project.getLogDir(); 206 } 207 208 public void setProjectName(String name) { 209 log("setting project name to [" + name + "]"); 210 project.setName(name); 211 } 212 213 public String getProjectName() { 214 return project.getName(); 215 } 216 217 public void setBuildInterval(long buildInterval) { 218 log("setting build interval to [" + buildInterval + "]"); 219 project.overrideBuildInterval(buildInterval); 220 } 221 222 public long getBuildInterval() { 223 return project.getBuildInterval(); 224 } 225 226 public String getStatus() { 227 return project.getStatusWithQueuePosition(); 228 } 229 230 private void log(String message) { 231 LOG.info(project.getName() + " Controller: " + message); 232 } 233 234 public void register(MBeanServer server) throws JMException { 235 ObjectName projectName = new ObjectName ("CruiseControl Project:name=" + project.getName()); 236 237 try { 241 server.unregisterMBean(projectName); 242 } catch (InstanceNotFoundException noProblem) { 243 } catch (MBeanRegistrationException noProblem) { 244 } 245 246 server.registerMBean(this, projectName); 247 } 248 249 } 250 | Popular Tags |