1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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 }