1 | /*
|
---|
2 | * Copyright 2001-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | /*
|
---|
18 | * Portions of this software are based upon public domain software
|
---|
19 | * originally written at the National Center for Supercomputing Applications,
|
---|
20 | * University of Illinois, Urbana-Champaign.
|
---|
21 | */
|
---|
22 |
|
---|
23 | package org.apache.tools.ant.taskdefs.optional.perforce;
|
---|
24 |
|
---|
25 | import org.apache.tools.ant.BuildException;
|
---|
26 | import org.apache.tools.ant.Project;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Obtains or sets the value of a counter.
|
---|
30 | *
|
---|
31 | * <p> When used in its base form
|
---|
32 | * (where only the counter name is provided), the counter value will be
|
---|
33 | * printed to the output stream. When the value is provided, the counter
|
---|
34 | * will be set to the value provided. When a property name is provided,
|
---|
35 | * the property will be filled with the value of the counter. You may
|
---|
36 | * not specify to both get and set the value of the counter in the same
|
---|
37 | * Task.
|
---|
38 | * </p>
|
---|
39 | * <P>
|
---|
40 | * The user performing this task must have Perforce "review" permissions
|
---|
41 | * as defined by Perforce protections in order for this task to succeed.
|
---|
42 | </P>
|
---|
43 |
|
---|
44 | * Example Usage:<br>
|
---|
45 | * <p4counter name="${p4.counter}" property=${p4.change}"/>
|
---|
46 | *
|
---|
47 | * @ant.task category="scm"
|
---|
48 | */
|
---|
49 |
|
---|
50 | public class P4Counter extends P4Base {
|
---|
51 | /**
|
---|
52 | * name of the counter
|
---|
53 | */
|
---|
54 | public String counter = null;
|
---|
55 | /**
|
---|
56 | * name of an optional property
|
---|
57 | */
|
---|
58 | public String property = null;
|
---|
59 | /**
|
---|
60 | * flag telling whether the value of the counter should be set
|
---|
61 | */
|
---|
62 | public boolean shouldSetValue = false;
|
---|
63 | /**
|
---|
64 | * flag telling whether a property should be set
|
---|
65 | */
|
---|
66 | public boolean shouldSetProperty = false;
|
---|
67 | /**
|
---|
68 | * new value for the counter
|
---|
69 | */
|
---|
70 | public int value = 0;
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * The name of the counter; required
|
---|
74 | * @param counter name of the counter
|
---|
75 | */
|
---|
76 | public void setName(String counter) {
|
---|
77 | this.counter = counter;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * The new value for the counter; optional.
|
---|
82 | * @param value new value for the counter
|
---|
83 | */
|
---|
84 | public void setValue(int value) {
|
---|
85 | this.value = value;
|
---|
86 | shouldSetValue = true;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * A property to be set with the value of the counter
|
---|
91 | * @param property the name of a property to set with the value
|
---|
92 | * of the counter
|
---|
93 | */
|
---|
94 | public void setProperty(String property) {
|
---|
95 | this.property = property;
|
---|
96 | shouldSetProperty = true;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * again, properties are mutable in this tsk
|
---|
101 | * @throws BuildException if the required parameters are not supplied.
|
---|
102 | */
|
---|
103 | public void execute() throws BuildException {
|
---|
104 |
|
---|
105 | if ((counter == null) || counter.length() == 0) {
|
---|
106 | throw new BuildException("No counter specified to retrieve");
|
---|
107 | }
|
---|
108 |
|
---|
109 | if (shouldSetValue && shouldSetProperty) {
|
---|
110 | throw new BuildException("Cannot both set the value of the property and retrieve the "
|
---|
111 | + "value of the property.");
|
---|
112 | }
|
---|
113 |
|
---|
114 | String command = "counter " + P4CmdOpts + " " + counter;
|
---|
115 | if (!shouldSetProperty) {
|
---|
116 | // NOTE [email protected] 04-April-2001 -- If you put in the -s, you
|
---|
117 | // have to start running through regular expressions here. Much easier
|
---|
118 | // to just not include the scripting information than to try to add it
|
---|
119 | // and strip it later.
|
---|
120 | command = "-s " + command;
|
---|
121 | }
|
---|
122 | if (shouldSetValue) {
|
---|
123 | command += " " + value;
|
---|
124 | }
|
---|
125 |
|
---|
126 | if (shouldSetProperty) {
|
---|
127 | final Project myProj = getProject();
|
---|
128 |
|
---|
129 | P4Handler handler = new P4HandlerAdapter() {
|
---|
130 | public void process(String line) {
|
---|
131 | log("P4Counter retrieved line \"" + line + "\"", Project.MSG_VERBOSE);
|
---|
132 | try {
|
---|
133 | value = Integer.parseInt(line);
|
---|
134 | myProj.setProperty(property, "" + value);
|
---|
135 | } catch (NumberFormatException nfe) {
|
---|
136 | throw new BuildException("Perforce error. "
|
---|
137 | + "Could not retrieve counter value.");
|
---|
138 | }
|
---|
139 | }
|
---|
140 | };
|
---|
141 |
|
---|
142 | execP4Command(command, handler);
|
---|
143 | } else {
|
---|
144 | execP4Command(command, new SimpleP4OutputHandler(this));
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|