KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > starteam > StarTeamCheckin


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.taskdefs.optional.starteam;
19
20 import com.starbase.starteam.Folder;
21 import com.starbase.starteam.Item;
22 import com.starbase.starteam.Status;
23 import com.starbase.starteam.View;
24 import com.starbase.starteam.ViewConfiguration;
25 import java.io.IOException JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import org.apache.tools.ant.BuildException;
28
29 /**
30  * Checks files into a StarTeam project.
31  * Optionally adds files and in the local tree that
32  * are not managed by the repository to its control.
33  * Created: Sat Dec 15 20:26:07 2001
34  *
35  * @version 1.0
36  *
37  * @ant.task name="stcheckin" category="scm" product="Starteam"
38  */

39 public class StarTeamCheckin extends TreeBasedTask {
40
41     /**
42      * Constructor for StarTeamCheckin.
43      */

44     public StarTeamCheckin() {
45         // we want this to have a false default, unlike for Checkin.
46
setRecursive(false);
47     }
48
49     private boolean createFolders = true;
50
51     /**
52      * The comment which will be stored with the checkin.
53      */

54     private String JavaDoc comment = null;
55
56     /**
57      * holder for the add Uncontrolled attribute. If true, all
58      * local files not in StarTeam will be added to the repository.
59      */

60     private boolean addUncontrolled = false;
61
62     /**
63      * Sets the value of createFolders
64      *
65      * @param argCreateFolders Value to assign to this.createFolders
66      */

67     public void setCreateFolders(boolean argCreateFolders) {
68         this.createFolders = argCreateFolders;
69     }
70
71
72     /**
73      * Get the comment attribute for this operation
74      * @return value of comment.
75      */

76     public String JavaDoc getComment() {
77         return this.comment;
78     }
79
80     /**
81      * Optional checkin comment to be saved with the file.
82      * @param comment Value to assign to comment.
83      */

84     public void setComment(String JavaDoc comment) {
85         this.comment = comment;
86     }
87
88     /**
89      * Get the value of addUncontrolled.
90      * @return value of addUncontrolled.
91      */

92     public boolean isAddUncontrolled() {
93         return this.addUncontrolled;
94     }
95
96     /**
97      * if true, any files or folders NOT in StarTeam will be
98      * added to the repository. Defaults to "false".
99      * @param addUncontrolled Value to assign to addUncontrolled.
100      */

101     public void setAddUncontrolled(boolean addUncontrolled) {
102         this.addUncontrolled = addUncontrolled;
103     }
104
105     /**
106      * This attribute tells whether unlocked files on checkin (so that
107      * other users may access them) checkout or to leave the checkout status
108      * alone (default).
109      * @see #setUnlocked(boolean)
110      */

111     private int lockStatus = Item.LockType.UNCHANGED;
112
113     /**
114      * Set to do an unlocked checkout; optional, default is false;
115      * If true, file will be unlocked so that other users may
116      * change it. If false, lock status will not change.
117      * @param v true means do an unlocked checkout
118      * false means leave status alone.
119      */

120     public void setUnlocked(boolean v) {
121         if (v) {
122             this.lockStatus = Item.LockType.UNLOCKED;
123         } else {
124             this.lockStatus = Item.LockType.UNCHANGED;
125         }
126     }
127
128     /**
129      * Override of base-class abstract function creates an
130      * appropriately configured view. For checkins this is
131      * always the current or "tip" view.
132      *
133      * @param raw the unconfigured <code>View</code>
134      * @return the snapshot <code>View</code> appropriately configured.
135      */

136     protected View createSnapshotView(View raw) {
137         return new View(raw, ViewConfiguration.createTip());
138     }
139
140     /**
141      * Implements base-class abstract function to define tests for
142      * any preconditons required by the task.
143      *
144      * @exception BuildException thrown if both rootLocalFolder
145      * and viewRootLocalFolder are defined
146      */

147     protected void testPreconditions() throws BuildException {
148     }
149     /**
150      * Implements base-class abstract function to emit to the log an
151      * entry describing the parameters that will be used by this operation.
152      *
153      * @param starteamrootFolder
154      * root folder in StarTeam for the operation
155      * @param targetrootFolder
156      * root local folder for the operation
157      * (whether specified by the user or not).
158      */

159     protected void logOperationDescription(
160         Folder starteamrootFolder, java.io.File JavaDoc targetrootFolder) {
161         log((this.isRecursive() ? "Recursive" : "Non-recursive")
162             + " Checkin from"
163             + (null == getRootLocalFolder() ? " (default): " : ": ")
164             + targetrootFolder.getAbsolutePath());
165
166         log("Checking in to: " + starteamrootFolder.getFolderHierarchy());
167         logIncludes();
168         logExcludes();
169
170         if (this.lockStatus == Item.LockType.UNLOCKED) {
171             log(" Items will be checked in unlocked.");
172         } else {
173             log(" Items will be checked in with no change in lock status.");
174         }
175
176         if (this.isForced()) {
177             log(" Items will be checked in in accordance with repository "
178                 + "status and regardless of lock status.");
179         } else {
180             log(" Items will be checked in regardless of repository status "
181                 + "only if locked.");
182         }
183
184
185     }
186
187     /**
188      * Implements base-class abstract function to perform the checkout
189      * operation on the files in each folder of the tree.
190      *
191      * @param starteamFolder the StarTeam folder to which files
192      * will be checked in
193      * @param targetFolder local folder from which files will be checked in
194      * @exception BuildException if any error occurs
195      */

196     protected void visit(Folder starteamFolder, java.io.File JavaDoc targetFolder)
197             throws BuildException {
198         try {
199             if (null != getRootLocalFolder()) {
200                 starteamFolder.setAlternatePathFragment(
201                     targetFolder.getAbsolutePath());
202             }
203
204             Folder[] foldersList = starteamFolder.getSubFolders();
205             Item[] stFiles = starteamFolder.getItems(getTypeNames().FILE);
206
207             // note, it's important to scan the items BEFORE we make the
208
// UnmatchedFileMap because that creates a bunch of NEW
209
// folders and files (unattached to repository) and we
210
// don't want to include those in our traversal.
211

212             UnmatchedFileMap ufm =
213                 new CheckinMap().init(
214                     targetFolder.getAbsoluteFile(), starteamFolder);
215
216
217             for (int i = 0, size = foldersList.length; i < size; i++) {
218                 Folder stFolder = foldersList[i];
219                 java.io.File JavaDoc subfolder =
220                     new java.io.File JavaDoc(targetFolder, stFolder.getName());
221
222                 ufm.removeControlledItem(subfolder);
223
224                 if (isRecursive()) {
225                     visit(stFolder, subfolder);
226                 }
227             }
228
229
230             for (int i = 0, size = stFiles.length; i < size; i++) {
231                 com.starbase.starteam.File stFile =
232                     (com.starbase.starteam.File) stFiles[i];
233                 processFile(stFile);
234
235                 ufm.removeControlledItem(
236                     new java.io.File JavaDoc(targetFolder, stFile.getName()));
237             }
238
239             if (this.addUncontrolled) {
240                 ufm.processUncontrolledItems();
241             }
242
243         } catch (IOException JavaDoc e) {
244             throw new BuildException(e);
245         }
246
247     }
248
249     /**
250      * provides a string showing from and to full paths for logging
251      *
252      * @param remotefile the Star Team file being processed.
253      *
254      * @return a string showing from and to full paths
255      */

256     private String JavaDoc describeCheckin(com.starbase.starteam.File remotefile) {
257         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
258         sb.append(remotefile.getFullName())
259           .append(" --> ")
260           .append(getFullRepositoryPath(remotefile));
261         return sb.toString();
262     }
263
264     /**
265      * Processes (checks-out) <code>stFiles</code>files from StarTeam folder.
266      *
267      * @param eachFile repository file to process
268      * @param targetFolder a java.io.File (Folder) to work
269      * @throws IOException when StarTeam API fails to work with files
270      */

271     private void processFile(com.starbase.starteam.File eachFile)
272         throws IOException JavaDoc {
273         String JavaDoc filename = eachFile.getName();
274
275         // If the file doesn't pass the include/exclude tests, skip it.
276
if (!shouldProcess(filename)) {
277             log("Excluding " + getFullRepositoryPath(eachFile));
278                 return;
279         }
280
281         boolean checkin = true;
282         int fileStatus = (eachFile.getStatus());
283
284         // We try to update the status once to give StarTeam
285
// another chance.
286

287         if (fileStatus == Status.MERGE || fileStatus == Status.UNKNOWN) {
288             eachFile.updateStatus(true, true);
289             fileStatus = (eachFile.getStatus());
290         }
291
292         if (fileStatus == Status.MODIFIED) {
293             log("Checking in: " + describeCheckin(eachFile));
294         } else if (fileStatus == Status.MISSING) {
295             log("Local file missing: " + describeCheckin(eachFile));
296             checkin = false;
297         } else {
298             if (isForced()) {
299                 log("Forced checkin of " + describeCheckin(eachFile)
300                     + " over status " + Status.name(fileStatus));
301             } else {
302                 log("Skipping: " + getFullRepositoryPath(eachFile)
303                     + " - status: " + Status.name(fileStatus));
304                 checkin = false;
305             }
306         }
307         if (checkin) {
308             eachFile.checkin(this.comment, this.lockStatus,
309                              this.isForced(), true, true);
310         }
311     }
312
313     /**
314      * handles the deletion of uncontrolled items
315      */

316     private class CheckinMap extends UnmatchedFileMap {
317         protected boolean isActive() {
318             return StarTeamCheckin.this.addUncontrolled;
319         }
320
321
322         /**
323          * This override adds all its members to the repository. It is assumed
324          * that this method will not be called until all the items in the
325          * corresponding folder have been processed, and that the internal map
326          * will contain only uncontrolled items.
327          */

328         void processUncontrolledItems() throws BuildException {
329             if (this.isActive()) {
330                 Enumeration JavaDoc e = this.keys();
331                 while (e.hasMoreElements()) {
332                     java.io.File JavaDoc local = (java.io.File JavaDoc) e.nextElement();
333                     Item remoteItem = (Item) this.get(local);
334                     remoteItem.update();
335
336                     // once we find a folder that isn't in the repository,
337
// we know we can add it.
338
if (local.isDirectory()) {
339                         Folder folder = (Folder) remoteItem;
340                         log("Added uncontrolled folder "
341                             + folder.getFolderHierarchy()
342                             + " from " + local.getAbsoluteFile());
343                         if (isRecursive()) {
344                             UnmatchedFileMap submap =
345                                 new CheckinMap().init(local, folder);
346                             submap.processUncontrolledItems();
347                         }
348                     } else {
349                         com.starbase.starteam.File remoteFile =
350                             (com.starbase.starteam.File) remoteItem;
351                         log("Added uncontrolled file "
352                             + TreeBasedTask.getFullRepositoryPath(remoteFile)
353                             + " from " + local.getAbsoluteFile());
354
355                     }
356                 }
357             }
358         }
359     }
360
361 }
362
Popular Tags