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.apache.maven.cli.logging.Slf4jConfiguration;
34 import org.apache.maven.cli.logging.Slf4jConfigurationFactory;
35 import org.codehaus.plexus.logging.console.ConsoleLogger;
36 import org.slf4j.ILoggerFactory;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 abstract class Executor {
41 private static final Logger LOG = LoggerFactory.getLogger(Executor.class);
42
43 protected void setupLogLevel(String logLevel) {
44 ILoggerFactory slf4jLoggerFactory = LoggerFactory.getILoggerFactory();
45 Slf4jConfiguration slf4jConfiguration = Slf4jConfigurationFactory.getConfiguration(slf4jLoggerFactory);
46 if ("debug".equals(logLevel)) {
47 slf4jConfiguration.setRootLoggerLevel(Slf4jConfiguration.Level.DEBUG);
48 } else if ("info".equals(logLevel)) {
49 slf4jConfiguration.setRootLoggerLevel(Slf4jConfiguration.Level.INFO);
50 } else {
51 slf4jConfiguration.setRootLoggerLevel(Slf4jConfiguration.Level.ERROR);
52 }
53 slf4jConfiguration.activate();
54 }
55
56 protected static String buildClasspath() {
57 StringBuilder classpath = new StringBuilder();
58
59
60 ClassLoader pluginClassloader = Executor.class.getClassLoader();
61 buildClasspath(classpath, pluginClassloader);
62
63 ClassLoader coreClassloader = ConsoleLogger.class.getClassLoader();
64 buildClasspath(classpath, coreClassloader);
65
66 return classpath.toString();
67 }
68
69 static void buildClasspath(StringBuilder classpath, ClassLoader cl) {
70 if (cl instanceof URLClassLoader) {
71 for (URL url : ((URLClassLoader) cl).getURLs()) {
72 if ("file".equalsIgnoreCase(url.getProtocol())) {
73 try {
74 String filename = URLDecoder.decode(url.getPath(), StandardCharsets.UTF_8.name());
75 classpath.append(new File(filename).getPath()).append(File.pathSeparatorChar);
76 } catch (UnsupportedEncodingException e) {
77 LOG.warn("Ignoring " + url + " in classpath due to UnsupportedEncodingException", e);
78 }
79 }
80 }
81 }
82 }
83
84 protected static class ProcessStreamHandler implements Runnable {
85 private static final int BUFFER_SIZE = 8192;
86
87 private final BufferedInputStream in;
88 private final BufferedOutputStream out;
89
90 public static void start(InputStream in, OutputStream out) {
91 Thread t = new Thread(new ProcessStreamHandler(in, out));
92 t.start();
93 }
94
95 private ProcessStreamHandler(InputStream in, OutputStream out) {
96 this.in = new BufferedInputStream(in);
97 this.out = new BufferedOutputStream(out);
98 }
99
100 @Override
101 public void run() {
102 byte[] buffer = new byte[BUFFER_SIZE];
103 try {
104 int count = in.read(buffer);
105 while (count != -1) {
106 out.write(buffer, 0, count);
107 out.flush();
108 count = in.read(buffer);
109 }
110 out.flush();
111 } catch (IOException e) {
112 LOG.error(e.getMessage(), e);
113 }
114 }
115 }
116 }