View Javadoc

1   /*
2    * Copyright 2007 scala-tools.org
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,
11   * software 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
14   * and limitations under the License.
15   */
16   package org.scala_tools.maven;
17  
18  import java.io.File;
19  import java.io.InputStream;
20  import java.io.PrintWriter;
21  
22  import jline.ClassNameCompletor;
23  import jline.ConsoleReader;
24  import jline.FileNameCompletor;
25  import jline.SimpleCompletor;
26  
27  import org.codehaus.plexus.util.IOUtil;
28  
29  /**
30   * Use Jline to manage input stream, to provide arrow support (history and line navigation).
31   *
32   * @author David Bernard (dwayne)
33   * @created 2007
34   */
35  public class ConsolePiper extends Thread {
36      private ConsoleReader console_;
37      private PrintWriter processInput_;
38      private InputStream processOutput_;
39  
40      public ConsolePiper(Process p) throws Exception {
41          console_ = new ConsoleReader();
42          File histoFile = new File(System.getProperty("user.home"), ".m2/scala-console.histo");
43          histoFile.getParentFile().mkdirs();
44          console_.getHistory().setHistoryFile(histoFile);
45          console_.addCompletor(new FileNameCompletor());
46          console_.addCompletor(new ClassNameCompletor());
47          console_.addCompletor(new SimpleCompletor(new String [] {
48                  "abstract", "case", "catch", "class", "def",
49                  "do", "else", "extends", "false", "final",
50                  "finally", "for", "if", "implicit", "import", "lazy",
51                  "match", "new", "null", "object", "override",
52                  "package", "private", "protected", "requires", "return",
53                  "sealed", "super", "this", "throw", "trait",
54                  "try", "true", "type", "val", "var",
55                  "while", "with", "yield"
56                  }
57              )
58          );
59          processInput_ = new PrintWriter(p.getOutputStream());
60  //        processOutput_ = p.getInputStream();
61  //        if (StringUtils.isNotEmpty(firstInput)) {
62  //            firstInput_ = firstInput;
63  //        }
64      }
65  
66      @Override
67      public void run() {
68          try {
69              while (true) {
70  //                // wait for prompt from process
71  //                do {
72  //                    bytes_read = processOutput_.read(buffer);
73  //                    if (bytes_read != -1) {
74  //                        System.out.write(buffer, 0, bytes_read);
75  //                        System.out.flush();
76  //                    }
77  //                } while (processOutput_.available() > 0);
78  //                if ((bytes_read > 1) && new String(buffer, 0, bytes_read).startsWith("scala>") && (firstInput_ != null)) {
79  //                    console_.putString(firstInput_);
80  //                    console_.printNewline();
81  //                    firstInput_ = null;
82  //                }
83                  processInput_.println(console_.readLine());
84                  processInput_.flush();
85                  yield();
86                  sleep(500l);
87              }
88          } catch (InterruptedException exc) {
89              System.err.print("stop by interrupt");
90              return;
91          } catch (Exception exc) {
92              System.err.print("!!!! exc !!!");
93              exc.printStackTrace();
94              throw new RuntimeException("wrap: " + exc.getMessage(), exc);
95          } finally {
96              IOUtil.close(processInput_);
97              IOUtil.close(processOutput_);
98          }
99      }
100 
101 }