View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.pmd.exec;
20  
21  import java.io.BufferedInputStream;
22  import java.io.BufferedOutputStream;
23  import java.io.File;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.OutputStream;
27  import java.io.UnsupportedEncodingException;
28  import java.net.URL;
29  import java.net.URLClassLoader;
30  import java.net.URLDecoder;
31  import java.nio.charset.StandardCharsets;
32  
33  import org.codehaus.plexus.logging.console.ConsoleLogger;
34  
35  abstract class Executor {
36  
37      protected static String buildClasspath() {
38          StringBuilder classpath = new StringBuilder();
39  
40          // plugin classpath needs to come first
41          ClassLoader pluginClassloader = Executor.class.getClassLoader();
42          buildClasspath(classpath, pluginClassloader);
43  
44          ClassLoader coreClassloader = ConsoleLogger.class.getClassLoader();
45          buildClasspath(classpath, coreClassloader);
46  
47          return classpath.toString();
48      }
49  
50      static void buildClasspath(StringBuilder classpath, ClassLoader cl) {
51          if (cl instanceof URLClassLoader) {
52              for (URL url : ((URLClassLoader) cl).getURLs()) {
53                  if ("file".equalsIgnoreCase(url.getProtocol())) {
54                      try {
55                          String filename = URLDecoder.decode(url.getPath(), StandardCharsets.UTF_8.name());
56                          classpath.append(new File(filename).getPath()).append(File.pathSeparatorChar);
57                      } catch (UnsupportedEncodingException e) {
58                          // skip as we provide the correct standard encoding
59                      }
60                  }
61              }
62          }
63      }
64  
65      protected static class ProcessStreamHandler implements Runnable {
66          private static final int BUFFER_SIZE = 8192;
67  
68          private final BufferedInputStream in;
69          private final BufferedOutputStream out;
70  
71          public static void start(InputStream in, OutputStream out) {
72              Thread t = new Thread(new ProcessStreamHandler(in, out));
73              t.start();
74          }
75  
76          private ProcessStreamHandler(InputStream in, OutputStream out) {
77              this.in = new BufferedInputStream(in);
78              this.out = new BufferedOutputStream(out);
79          }
80  
81          @Override
82          public void run() {
83              byte[] buffer = new byte[BUFFER_SIZE];
84              try {
85                  int count = in.read(buffer);
86                  while (count != -1) {
87                      out.write(buffer, 0, count);
88                      out.flush();
89                      count = in.read(buffer);
90                  }
91                  out.flush();
92              } catch (IOException e) {
93                  e.printStackTrace();
94              }
95          }
96      }
97  }