KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.Project;
28
29 /**
30  * Obtains or sets the value of a counter.
31  *
32  * <p> When used in its base form
33  * (where only the counter name is provided), the counter value will be
34  * printed to the output stream. When the value is provided, the counter
35  * will be set to the value provided. When a property name is provided,
36  * the property will be filled with the value of the counter. You may
37  * not specify to both get and set the value of the counter in the same
38  * Task.
39  * </p>
40  * <P>
41  * The user performing this task must have Perforce &quot;review&quot; permissions
42  * as defined by Perforce protections in order for this task to succeed.
43 </P>
44
45  * Example Usage:<br>
46  * &lt;p4counter name="${p4.counter}" property=${p4.change}"/&gt;
47  *
48  * @ant.task category="scm"
49  */

50
51 public class P4Counter extends P4Base {
52     // CheckStyle:VisibilityModifier OFF - bc
53
/**
54      * name of the counter
55      */

56     public String JavaDoc counter = null;
57     /**
58      * name of an optional property
59      */

60     public String JavaDoc property = null;
61     /**
62      * flag telling whether the value of the counter should be set
63      */

64     public boolean shouldSetValue = false;
65     /**
66      * flag telling whether a property should be set
67      */

68     public boolean shouldSetProperty = false;
69     /**
70      * new value for the counter
71      */

72     public int value = 0;
73
74     // CheckStyle:VisibilityModifier ON
75

76     /**
77      * The name of the counter; required
78      * @param counter name of the counter
79      */

80     public void setName(String JavaDoc counter) {
81         this.counter = counter;
82     }
83
84     /**
85      * The new value for the counter; optional.
86      * @param value new value for the counter
87      */

88     public void setValue(int value) {
89         this.value = value;
90         shouldSetValue = true;
91     }
92
93     /**
94      * A property to be set with the value of the counter
95      * @param property the name of a property to set with the value
96      * of the counter
97      */

98     public void setProperty(String JavaDoc property) {
99         this.property = property;
100         shouldSetProperty = true;
101     }
102
103     /**
104      * again, properties are mutable in this tsk
105      * @throws BuildException if the required parameters are not supplied.
106      */

107     public void execute() throws BuildException {
108
109         if ((counter == null) || counter.length() == 0) {
110             throw new BuildException("No counter specified to retrieve");
111         }
112
113         if (shouldSetValue && shouldSetProperty) {
114             throw new BuildException("Cannot both set the value of the property and retrieve the "
115                 + "value of the property.");
116         }
117
118         String JavaDoc command = "counter " + P4CmdOpts + " " + counter;
119         if (!shouldSetProperty) {
120             // NOTE kirk@radik.com 04-April-2001 -- If you put in the -s, you
121
// have to start running through regular expressions here. Much easier
122
// to just not include the scripting information than to try to add it
123
// and strip it later.
124
command = "-s " + command;
125         }
126         if (shouldSetValue) {
127             command += " " + value;
128         }
129
130         if (shouldSetProperty) {
131             final Project myProj = getProject();
132
133             P4Handler handler = new P4HandlerAdapter() {
134                 public void process(String JavaDoc line) {
135                     log("P4Counter retrieved line \"" + line + "\"", Project.MSG_VERBOSE);
136                     try {
137                         value = Integer.parseInt(line);
138                         myProj.setProperty(property, "" + value);
139                     } catch (NumberFormatException JavaDoc nfe) {
140                         throw new BuildException("Perforce error. "
141                         + "Could not retrieve counter value.");
142                     }
143                 }
144             };
145
146             execP4Command(command, handler);
147         } else {
148             execP4Command(command, new SimpleP4OutputHandler(this));
149         }
150     }
151 }
152
Popular Tags