KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > scm > SubversionUpdateEventHandler


1 /*
2  * ====================================================================
3  * Copyright (c) 2004-2007 TMate Software Ltd. All rights reserved.
4  *
5  * This software is licensed as described in the file COPYING, which
6  * you should have received as part of this distribution. The terms
7  * are also available at http://svnkit.com/license.html
8  * If newer versions of this license are posted there, you may use a
9  * newer version instead, at your option.
10  * ====================================================================
11  */

12 package hudson.scm;
13
14 import org.tmatesoft.svn.core.SVNCancelException;
15 import org.tmatesoft.svn.core.wc.ISVNEventHandler;
16 import org.tmatesoft.svn.core.wc.SVNEvent;
17 import org.tmatesoft.svn.core.wc.SVNStatusType;
18 import org.tmatesoft.svn.core.wc.SVNEventAction;
19 import hudson.model.TaskListener;
20
21 import java.io.PrintStream JavaDoc;
22
23 /**
24  * Just prints out the progress of svn update/checkout operation in a way similar to
25  * the svn CLI.
26  */

27 final class SubversionUpdateEventHandler implements ISVNEventHandler {
28
29     private final PrintStream JavaDoc out;
30
31     public SubversionUpdateEventHandler(TaskListener listener) {
32         this.out = listener.getLogger();
33     }
34
35     public void handleEvent(SVNEvent event, double progress) {
36         /*
37          * Gets the current action. An action is represented by SVNEventAction.
38          * In case of an update an action can be determined via comparing
39          * SVNEvent.getAction() and SVNEventAction.UPDATE_-like constants.
40          */

41         SVNEventAction action = event.getAction();
42         String JavaDoc pathChangeType = " ";
43         if (action == SVNEventAction.UPDATE_ADD) {
44             /*
45              * the item was added
46              */

47             pathChangeType = "A";
48         } else if (action == SVNEventAction.UPDATE_DELETE) {
49             /*
50              * the item was deleted
51              */

52             pathChangeType = "D";
53         } else if (action == SVNEventAction.UPDATE_UPDATE) {
54             /*
55              * Find out in details what state the item is (after having been
56              * updated).
57              *
58              * Gets the status of file/directory item contents. It is
59              * SVNStatusType who contains information on the state of an item.
60              */

61             SVNStatusType contentsStatus = event.getContentsStatus();
62             if (contentsStatus == SVNStatusType.CHANGED) {
63                 /*
64                  * the item was modified in the repository (got the changes
65                  * from the repository
66                  */

67                 pathChangeType = "U";
68             }else if (contentsStatus == SVNStatusType.CONFLICTED) {
69                 /*
70                  * The file item is in a state of Conflict. That is, changes
71                  * received from the repository during an update, overlap with
72                  * local changes the user has in his working copy.
73                  */

74                 pathChangeType = "C";
75             } else if (contentsStatus == SVNStatusType.MERGED) {
76                 /*
77                  * The file item was merGed (those changes that came from the
78                  * repository did not overlap local changes and were merged
79                  * into the file).
80                  */

81                 pathChangeType = "G";
82             }
83         } else if (action == SVNEventAction.UPDATE_EXTERNAL) {
84             /*for externals definitions*/
85             out.println("Fetching external item into '"
86                     + event.getFile().getAbsolutePath() + "'");
87             out.println("External at revision " + event.getRevision());
88             return;
89         } else if (action == SVNEventAction.UPDATE_COMPLETED) {
90             /*
91              * Updating the working copy is completed. Prints out the revision.
92              */

93             out.println("At revision " + event.getRevision());
94             return;
95         } else if (action == SVNEventAction.ADD){
96             out.println("A " + event.getPath());
97             return;
98         } else if (action == SVNEventAction.DELETE){
99             out.println("D " + event.getPath());
100             return;
101         } else if (action == SVNEventAction.LOCKED){
102             out.println("L " + event.getPath());
103             return;
104         } else if (action == SVNEventAction.LOCK_FAILED){
105             out.println("failed to lock " + event.getPath());
106             return;
107         }
108
109         /*
110          * Now getting the status of properties of an item. SVNStatusType also
111          * contains information on the properties state.
112          */

113         SVNStatusType propertiesStatus = event.getPropertiesStatus();
114         /*
115          * At first consider properties are normal (unchanged).
116          */

117         String JavaDoc propertiesChangeType = " ";
118         if (propertiesStatus == SVNStatusType.CHANGED) {
119             /*
120              * Properties were updated.
121              */

122             propertiesChangeType = "U";
123         } else if (propertiesStatus == SVNStatusType.CONFLICTED) {
124             /*
125              * Properties are in conflict with the repository.
126              */

127             propertiesChangeType = "C";
128         } else if (propertiesStatus == SVNStatusType.MERGED) {
129             /*
130              * Properties that came from the repository were merged with the
131              * local ones.
132              */

133             propertiesChangeType = "G";
134         }
135
136         /*
137          * Gets the status of the lock.
138          */

139         String JavaDoc lockLabel = " ";
140         SVNStatusType lockType = event.getLockStatus();
141
142         if (lockType == SVNStatusType.LOCK_UNLOCKED) {
143             /*
144              * The lock is broken by someone.
145              */

146             lockLabel = "B";
147         }
148
149         if(pathChangeType.equals(" ") && propertiesChangeType.equals(" ") && lockLabel.equals(" ") && event.getPath().equals(""))
150             // nothing to display here.
151
// SVNKit always seems to send one such line.
152
return;
153
154         out.println(pathChangeType
155                 + propertiesChangeType
156                 + lockLabel
157                 + " "
158                 + event.getPath());
159     }
160
161     public void checkCancelled() throws SVNCancelException {
162         if(Thread.currentThread().isInterrupted())
163             throw new SVNCancelException();
164     }
165 }
Popular Tags