KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.tools.ant.taskdefs.optional.perforce;
20
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.taskdefs.PumpStreamHandler;
28 /**
29  * base class to manage streams around the execution of the Perforce
30  * command line client
31  */

32 public abstract class P4HandlerAdapter implements P4Handler {
33     // CheckStyle:VisibilityModifier OFF - bc
34
String JavaDoc p4input = "";
35     private PumpStreamHandler myHandler = null;
36     // CheckStyle:VisibilityModifier ON
37
/**
38      * set any data to be written to P4's stdin
39      * @param p4Input the text to write to P4's stdin
40      */

41     public void setOutput(String JavaDoc p4Input) {
42         this.p4input = p4Input;
43     }
44     /**
45      * subclasses of P4HandlerAdapter must implement this routine
46      * processing of one line of stdout or of stderr
47      * @param line line of stdout or stderr to process
48      */

49     public abstract void process(String JavaDoc line);
50
51     /**
52      * this routine gets called by the execute routine of the Execute class
53      * it connects the PumpStreamHandler to the input/output/error streams of the process.
54      * @throws BuildException if there is a error.
55      * @see org.apache.tools.ant.taskdefs.Execute#execute
56      */

57     public void start() throws BuildException {
58         if (p4input != null && p4input.length() > 0) {
59             myHandler = new PumpStreamHandler(new P4OutputStream(this), new P4OutputStream(this),
60                 new ByteArrayInputStream JavaDoc(p4input.getBytes()));
61         } else {
62             myHandler = new PumpStreamHandler(new P4OutputStream(this), new P4OutputStream(this));
63         }
64         myHandler.setProcessInputStream(os);
65         myHandler.setProcessErrorStream(es);
66         myHandler.setProcessOutputStream(is);
67         myHandler.start();
68     }
69
70     /**
71      * stops the processing of streams
72      * called from P4Base#execP4Command(String command, P4Handler handler)
73      * @see P4Base#execP4Command(String, P4Handler)
74      */

75     public void stop() {
76         myHandler.stop();
77     }
78
79     // CheckStyle:VisibilityModifier OFF - bc
80
OutputStream JavaDoc os; //Input
81
InputStream JavaDoc is; //Output
82
InputStream JavaDoc es; //Error
83
// CheckStyle:VisibilityModifier ON
84

85     /**
86      * connects the handler to the input stream into Perforce
87      * used indirectly by tasks requiring to send specific standard input
88      * such as p4label, p4change
89      * @param os the stream bringing input to the p4 executable
90      * @throws IOException under unknown circumstances
91      */

92     public void setProcessInputStream(OutputStream JavaDoc os) throws IOException JavaDoc {
93         this.os = os;
94     }
95
96     /**
97      * connects the handler to the stderr of the Perforce process
98      * @param is stderr coming from Perforce
99      * @throws IOException under unknown circumstances
100      */

101     public void setProcessErrorStream(InputStream JavaDoc is) throws IOException JavaDoc {
102         this.es = is;
103     }
104
105     /**
106      * connects the handler to the stdout of the Perforce process
107      * @param is stdout coming from Perforce
108      * @throws IOException under unknown circumstances
109      */

110     public void setProcessOutputStream(InputStream JavaDoc is) throws IOException JavaDoc {
111         this.is = is;
112     }
113 }
114
Popular Tags