KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > listeners > LockFileListener


1 package net.sourceforge.cruisecontrol.listeners;
2
3 import java.io.File JavaDoc;
4
5 import net.sourceforge.cruisecontrol.CruiseControlException;
6 import net.sourceforge.cruisecontrol.Listener;
7 import net.sourceforge.cruisecontrol.ProjectEvent;
8 import net.sourceforge.cruisecontrol.ProjectState;
9 import net.sourceforge.cruisecontrol.util.ValidationHelper;
10
11 public class LockFileListener implements Listener {
12
13     private String JavaDoc path;
14     private boolean needToDeleteLock = false;
15
16     public void handleEvent(ProjectEvent event) throws CruiseControlException {
17         if (!(event instanceof ProjectStateChangedEvent)) {
18             return;
19         }
20
21         ProjectState newState = ((ProjectStateChangedEvent) event).getNewState();
22         
23         if (projectGotPastBootstrapping(newState)) {
24             needToDeleteLock = true;
25         }
26         
27         if (newState.equals(ProjectState.IDLE) && needToDeleteLock) {
28             boolean deletedFile = attemptToDeleteLock();
29             if (!deletedFile) {
30                 throw new CruiseControlException("project " + event.getProjectName()
31                                 + " failed to delete lock file " + path);
32             }
33         }
34     }
35
36     boolean attemptToDeleteLock() {
37         return new File JavaDoc(path).delete();
38     }
39
40     private boolean projectGotPastBootstrapping(ProjectState newState) {
41         return newState.equals(ProjectState.MODIFICATIONSET);
42     }
43
44     public void validate() throws CruiseControlException {
45         ValidationHelper.assertIsSet(path, "lockfile", LockFileListener.class);
46     }
47
48     public void setLockFile(String JavaDoc path) {
49         this.path = path;
50     }
51
52 }
53
Popular Tags