KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > perforce > P4Label


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 /*
19  * Portions of this software are based upon public domain software
20  * originally written at the National Center for Supercomputing Applications,
21  * University of Illinois, Urbana-Champaign.
22  */

23
24 package org.apache.tools.ant.taskdefs.optional.perforce;
25
26 import java.text.SimpleDateFormat JavaDoc;
27 import java.util.Date JavaDoc;
28 import org.apache.tools.ant.BuildException;
29 import org.apache.tools.ant.Project;
30 import org.apache.tools.ant.util.StringUtils;
31
32 /**
33  * Creates a new Perforce label and set contents to reflect current
34  * client file revisions.
35  *
36  * Label name defaults to AntLabel if none set.
37  *
38  * Example Usage:
39  * <pre>
40  * &lt;P4Label name="MyLabel-${TSTAMP}-${DSTAMP}" desc="Auto Build Label" /&gt;
41  * </pre>
42  *
43  * @ant.task category="scm"
44  */

45 public class P4Label extends P4Base {
46
47     // CheckStyle:VisibilityModifier OFF - bc
48
protected String JavaDoc name;
49     protected String JavaDoc desc;
50     protected String JavaDoc lock;
51     // CheckStyle:VisibilityModifier ON
52

53     /**
54      * The name of the label; optional, default "AntLabel"
55      * @param name the name of the label
56      */

57     public void setName(String JavaDoc name) {
58         this.name = name;
59     }
60
61     /**
62      *Label Description; optional
63      * @param desc description of the label
64      */

65     public void setDesc(String JavaDoc desc) {
66         this.desc = desc;
67     }
68
69     /**
70      * when set to "locked", Perforce will lock the label once created; optional.
71      * @param lock only admissible value "locked"
72      */

73     public void setLock(String JavaDoc lock) {
74         this.lock = lock;
75     }
76
77     /**
78      * do the work
79      * @throws BuildException if failonerror has been set to true and Perforce fails
80      */

81     public void execute() throws BuildException {
82         log("P4Label exec:", Project.MSG_INFO);
83
84         if (P4View == null || P4View.length() < 1) {
85             log("View not set, assuming //depot/...", Project.MSG_WARN);
86             P4View = "//depot/...";
87         } else {
88             P4View = StringUtils.replace(P4View, ":", "\n\t");
89             P4View = StringUtils.replace(P4View, ";", "\n\t");
90         }
91
92         if (desc == null || desc.length() < 1) {
93             log("Label Description not set, assuming 'AntLabel'",
94                 Project.MSG_WARN);
95             desc = "AntLabel";
96         }
97
98         if (lock != null && !lock.equalsIgnoreCase("locked")) {
99             log("lock attribute invalid - ignoring", Project.MSG_WARN);
100         }
101
102         if (name == null || name.length() < 1) {
103             SimpleDateFormat JavaDoc formatter
104                 = new SimpleDateFormat JavaDoc("yyyy.MM.dd-hh:mm");
105             Date JavaDoc now = new Date JavaDoc();
106             name = "AntLabel-" + formatter.format(now);
107             log("name not set, assuming '" + name + "'", Project.MSG_WARN);
108         }
109
110
111         //We have to create a unlocked label first
112
String JavaDoc newLabel =
113                 "Label: " + name
114                 + "\nDescription: " + desc
115                 + "\nOptions: unlocked"
116                 + "\nView: \n\t" + P4View;
117
118         P4Handler handler = new P4HandlerAdapter() {
119             public void process(String JavaDoc line) {
120                 log(line, Project.MSG_VERBOSE);
121             }
122         };
123
124         handler.setOutput(newLabel);
125
126         execP4Command("label -i", handler);
127
128         execP4Command("labelsync -l " + name, new P4HandlerAdapter() {
129             public void process(String JavaDoc line) {
130                 log(line, Project.MSG_VERBOSE);
131             }
132         });
133
134
135         log("Created Label " + name + " (" + desc + ") with view:\n" + P4View,
136             Project.MSG_INFO);
137
138         //Now lock if required
139
if (lock != null && lock.equalsIgnoreCase("locked")) {
140
141             log("Modifying lock status to 'locked'", Project.MSG_INFO);
142
143             final StringBuffer JavaDoc labelSpec = new StringBuffer JavaDoc();
144
145             //Read back the label spec from perforce,
146
//Replace Options
147
//Submit back to Perforce
148

149             handler = new P4HandlerAdapter() {
150                 public void process(String JavaDoc line) {
151                     log(line, Project.MSG_VERBOSE);
152
153                     if (util.match("/^Options:/", line)) {
154                         line = "Options: " + lock;
155                     }
156
157                     labelSpec.append(line + "\n");
158                 }
159             };
160
161
162             execP4Command("label -o " + name, handler);
163             log(labelSpec.toString(), Project.MSG_DEBUG);
164
165             log("Now locking label...", Project.MSG_VERBOSE);
166             handler = new P4HandlerAdapter() {
167                 public void process(String JavaDoc line) {
168                     log(line, Project.MSG_VERBOSE);
169                 }
170             };
171
172             handler.setOutput(labelSpec.toString());
173             execP4Command("label -i", handler);
174         }
175     }
176 }
177
Popular Tags