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.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          // plugin classpath needs to come first
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 }