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: package org.apache.tools.ant.taskdefs.optional.perforce;
020:
021: import java.io.ByteArrayInputStream;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.OutputStream;
025:
026: import org.apache.tools.ant.BuildException;
027: import org.apache.tools.ant.taskdefs.PumpStreamHandler;
028:
029: /**
030: * base class to manage streams around the execution of the Perforce
031: * command line client
032: */
033: public abstract class P4HandlerAdapter implements P4Handler {
034: // CheckStyle:VisibilityModifier OFF - bc
035: String p4input = "";
036: private PumpStreamHandler myHandler = null;
037:
038: // CheckStyle:VisibilityModifier ON
039: /**
040: * set any data to be written to P4's stdin
041: * @param p4Input the text to write to P4's stdin
042: */
043: public void setOutput(String p4Input) {
044: this .p4input = p4Input;
045: }
046:
047: /**
048: * subclasses of P4HandlerAdapter must implement this routine
049: * processing of one line of stdout or of stderr
050: * @param line line of stdout or stderr to process
051: */
052: public abstract void process(String line);
053:
054: /**
055: * this routine gets called by the execute routine of the Execute class
056: * it connects the PumpStreamHandler to the input/output/error streams of the process.
057: * @throws BuildException if there is a error.
058: * @see org.apache.tools.ant.taskdefs.Execute#execute
059: */
060: public void start() throws BuildException {
061: if (p4input != null && p4input.length() > 0) {
062: myHandler = new PumpStreamHandler(new P4OutputStream(this ),
063: new P4OutputStream(this ), new ByteArrayInputStream(
064: p4input.getBytes()));
065: } else {
066: myHandler = new PumpStreamHandler(new P4OutputStream(this ),
067: new P4OutputStream(this ));
068: }
069: myHandler.setProcessInputStream(os);
070: myHandler.setProcessErrorStream(es);
071: myHandler.setProcessOutputStream(is);
072: myHandler.start();
073: }
074:
075: /**
076: * stops the processing of streams
077: * called from P4Base#execP4Command(String command, P4Handler handler)
078: * @see P4Base#execP4Command(String, P4Handler)
079: */
080: public void stop() {
081: myHandler.stop();
082: }
083:
084: // CheckStyle:VisibilityModifier OFF - bc
085: OutputStream os; //Input
086: InputStream is; //Output
087: InputStream es; //Error
088:
089: // CheckStyle:VisibilityModifier ON
090:
091: /**
092: * connects the handler to the input stream into Perforce
093: * used indirectly by tasks requiring to send specific standard input
094: * such as p4label, p4change
095: * @param os the stream bringing input to the p4 executable
096: * @throws IOException under unknown circumstances
097: */
098: public void setProcessInputStream(OutputStream os)
099: throws IOException {
100: this .os = os;
101: }
102:
103: /**
104: * connects the handler to the stderr of the Perforce process
105: * @param is stderr coming from Perforce
106: * @throws IOException under unknown circumstances
107: */
108: public void setProcessErrorStream(InputStream is)
109: throws IOException {
110: this .es = is;
111: }
112:
113: /**
114: * connects the handler to the stdout of the Perforce process
115: * @param is stdout coming from Perforce
116: * @throws IOException under unknown circumstances
117: */
118: public void setProcessOutputStream(InputStream is)
119: throws IOException {
120: this.is = is;
121: }
122: }
|