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. package org.apache.tools.ant.taskdefs.optional.perforce;
  18. import java.io.ByteArrayInputStream;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.OutputStream;
  22. import org.apache.tools.ant.BuildException;
  23. import org.apache.tools.ant.taskdefs.PumpStreamHandler;
  24. /**
  25. * base class to manage streams around the execution of the Perforce
  26. * command line client
  27. */
  28. public abstract class P4HandlerAdapter implements P4Handler {
  29. String p4input = "";
  30. private PumpStreamHandler myHandler = null;
  31. /**
  32. * set any data to be written to P4's stdin
  33. * @param p4Input the text to write to P4's stdin
  34. */
  35. public void setOutput(String p4Input) {
  36. this.p4input = p4Input;
  37. }
  38. /**
  39. * subclasses of P4HandlerAdapter must implement this routine
  40. * processing of one line of stdout or of stderr
  41. * @param line line of stdout or stderr to process
  42. */
  43. public abstract void process(String line);
  44. /**
  45. * this routine gets called by the execute routine of the Execute class
  46. * it connects the PumpStreamHandler to the input/output/error streams of the process.
  47. * @throws BuildException
  48. * @see org.apache.tools.ant.taskdefs.Execute#execute
  49. */
  50. public void start() throws BuildException {
  51. if (p4input != null && p4input.length() > 0) {
  52. myHandler = new PumpStreamHandler(new P4OutputStream(this), new P4OutputStream(this),
  53. new ByteArrayInputStream(p4input.getBytes()));
  54. } else {
  55. myHandler = new PumpStreamHandler(new P4OutputStream(this), new P4OutputStream(this));
  56. }
  57. myHandler.setProcessInputStream(os);
  58. myHandler.setProcessErrorStream(es);
  59. myHandler.setProcessOutputStream(is);
  60. myHandler.start();
  61. }
  62. /**
  63. * stops the processing of streams
  64. * called from P4Base#execP4Command(String command, P4Handler handler)
  65. * @see P4Base#execP4Command(String, P4Handler)
  66. */
  67. public void stop() {
  68. myHandler.stop();
  69. }
  70. OutputStream os; //Input
  71. InputStream is; //Output
  72. InputStream es; //Error
  73. /**
  74. * connects the handler to the input stream into Perforce
  75. * used indirectly by tasks requiring to send specific standard input
  76. * such as p4label, p4change
  77. * @param os the stream bringing input to the p4 executable
  78. * @throws IOException under unknown circumstances
  79. */
  80. public void setProcessInputStream(OutputStream os) throws IOException {
  81. this.os = os;
  82. }
  83. /**
  84. * connects the handler to the stderr of the Perforce process
  85. * @param is stderr coming from Perforce
  86. * @throws IOException under unknown circumstances
  87. */
  88. public void setProcessErrorStream(InputStream is) throws IOException {
  89. this.es = is;
  90. }
  91. /**
  92. * connects the handler to the stdout of the Perforce process
  93. * @param is stdout coming from Perforce
  94. * @throws IOException under unknown circumstances
  95. */
  96. public void setProcessOutputStream(InputStream is) throws IOException {
  97. this.is = is;
  98. }
  99. }