KickJava   Java API By Example, From Geeks To Geeks.

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


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001-2003, 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 javax.management.InstanceNotFoundException JavaDoc;
40 import javax.management.JMException JavaDoc;
41 import javax.management.MBeanRegistrationException JavaDoc;
42 import javax.management.MBeanServer JavaDoc;
43 import javax.management.Notification JavaDoc;
44 import javax.management.NotificationBroadcasterSupport JavaDoc;
45 import javax.management.ObjectName JavaDoc;
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 /**
59  * @author Niclas Olofsson
60  * @author <a HREF="mailto:jcyip@thoughtworks.com">Jason Yip</a>
61  */

62 public class ProjectController extends NotificationBroadcasterSupport JavaDoc
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 JavaDoc SEQUENCE_LOCK = new Object JavaDoc();
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 JavaDoc notification = new Notification JavaDoc("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 JavaDoc notification = new Notification JavaDoc("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 JavaDoc 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 JavaDoc label) {
146         log("setting label to [" + label + "]");
147         project.setLabel(label);
148     }
149
150     public String JavaDoc getLabel() {
151         return project.getLabel();
152     }
153
154     public void setLabelIncrementer(String JavaDoc 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 JavaDoc 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 JavaDoc getLabelIncrementer() {
172         return project.getLabelIncrementer().getClass().getName();
173     }
174
175     public void setLastBuild(String JavaDoc date) throws CruiseControlException {
176         log("setting last build to [" + date + "]");
177         project.setLastBuild(date);
178     }
179
180     public String JavaDoc getLastBuild() {
181         return project.getLastBuild();
182     }
183
184     public void setLastSuccessfulBuild(String JavaDoc date)
185         throws CruiseControlException {
186         log("setting last successful build to [" + date + "]");
187         project.setLastSuccessfulBuild(date);
188     }
189
190     public String JavaDoc getLastSuccessfulBuild() {
191         return project.getLastSuccessfulBuild();
192     }
193
194     public String JavaDoc getBuildStartTime() {
195         String JavaDoc buildStartTime = project.getBuildStartTime();
196         return buildStartTime == null ? "" : buildStartTime;
197     }
198
199     public void setLogDir(String JavaDoc logdir) throws CruiseControlException {
200         log("setting log dir to [" + logdir + "]");
201         project.getLog().setDir(logdir);
202     }
203
204     public String JavaDoc getLogDir() {
205         return project.getLogDir();
206     }
207
208     public void setProjectName(String JavaDoc name) {
209         log("setting project name to [" + name + "]");
210         project.setName(name);
211     }
212
213     public String JavaDoc 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 JavaDoc getStatus() {
227         return project.getStatusWithQueuePosition();
228     }
229
230     private void log(String JavaDoc message) {
231         LOG.info(project.getName() + " Controller: " + message);
232     }
233
234     public void register(MBeanServer JavaDoc server) throws JMException JavaDoc {
235         ObjectName JavaDoc projectName = new ObjectName JavaDoc("CruiseControl Project:name=" + project.getName());
236
237         // Need to attempt to unregister the old mbean with the same name since
238
// CruiseControlControllerJMXAdaptor keeps calling every time a change
239
// is made to the config.xml file via JMX.
240
try {
241             server.unregisterMBean(projectName);
242         } catch (InstanceNotFoundException JavaDoc noProblem) {
243         } catch (MBeanRegistrationException JavaDoc noProblem) {
244         }
245
246         server.registerMBean(this, projectName);
247     }
248
249 }
250
Popular Tags