001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: /*
019: * Portions of this software are based upon public domain software
020: * originally written at the National Center for Supercomputing Applications,
021: * University of Illinois, Urbana-Champaign.
022: */
023:
024: package org.apache.tools.ant.taskdefs.optional.perforce;
025:
026: import org.apache.tools.ant.BuildException;
027: import org.apache.tools.ant.Project;
028:
029: /**
030: * Obtains or sets the value of a counter.
031: *
032: * <p> When used in its base form
033: * (where only the counter name is provided), the counter value will be
034: * printed to the output stream. When the value is provided, the counter
035: * will be set to the value provided. When a property name is provided,
036: * the property will be filled with the value of the counter. You may
037: * not specify to both get and set the value of the counter in the same
038: * Task.
039: * </p>
040: * <P>
041: * The user performing this task must have Perforce "review" permissions
042: * as defined by Perforce protections in order for this task to succeed.
043: </P>
044:
045: * Example Usage:<br>
046: * <p4counter name="${p4.counter}" property=${p4.change}"/>
047: *
048: * @ant.task category="scm"
049: */
050:
051: public class P4Counter extends P4Base {
052: // CheckStyle:VisibilityModifier OFF - bc
053: /**
054: * name of the counter
055: */
056: public String counter = null;
057: /**
058: * name of an optional property
059: */
060: public String property = null;
061: /**
062: * flag telling whether the value of the counter should be set
063: */
064: public boolean shouldSetValue = false;
065: /**
066: * flag telling whether a property should be set
067: */
068: public boolean shouldSetProperty = false;
069: /**
070: * new value for the counter
071: */
072: public int value = 0;
073:
074: // CheckStyle:VisibilityModifier ON
075:
076: /**
077: * The name of the counter; required
078: * @param counter name of the counter
079: */
080: public void setName(String counter) {
081: this .counter = counter;
082: }
083:
084: /**
085: * The new value for the counter; optional.
086: * @param value new value for the counter
087: */
088: public void setValue(int value) {
089: this .value = value;
090: shouldSetValue = true;
091: }
092:
093: /**
094: * A property to be set with the value of the counter
095: * @param property the name of a property to set with the value
096: * of the counter
097: */
098: public void setProperty(String property) {
099: 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(
115: "Cannot both set the value of the property and retrieve the "
116: + "value of the property.");
117: }
118:
119: String command = "counter " + P4CmdOpts + " " + counter;
120: if (!shouldSetProperty) {
121: // NOTE kirk@radik.com 04-April-2001 -- If you put in the -s, you
122: // have to start running through regular expressions here. Much easier
123: // to just not include the scripting information than to try to add it
124: // and strip it later.
125: command = "-s " + command;
126: }
127: if (shouldSetValue) {
128: command += " " + value;
129: }
130:
131: if (shouldSetProperty) {
132: final Project myProj = getProject();
133:
134: P4Handler handler = new P4HandlerAdapter() {
135: public void process(String line) {
136: log("P4Counter retrieved line \"" + line + "\"",
137: Project.MSG_VERBOSE);
138: try {
139: value = Integer.parseInt(line);
140: myProj.setProperty(property, "" + value);
141: } catch (NumberFormatException nfe) {
142: throw new BuildException("Perforce error. "
143: + "Could not retrieve counter value.");
144: }
145: }
146: };
147:
148: execP4Command(command, handler);
149: } else {
150: execP4Command(command, new SimpleP4OutputHandler(this));
151: }
152: }
153: }
|