View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.log4j.chainsaw.plugins;
18  
19  import org.apache.log4j.chainsaw.prefs.SettingsManager;
20  
21  import java.io.File;
22  import java.net.URL;
23  import java.net.URLClassLoader;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  /**
28   * A factory class to create a Classloader that can refenerence jars/classes/resources
29   * within a users plugin directory.
30   * <p>
31   * Currently a work in progress to see if this allows external jars required by
32   * some 3rd party vendors for things like the JMSReceiver.
33   *
34   * @author psmith
35   */
36  public class PluginClassLoaderFactory {
37      private final ClassLoader pluginClassLoader;
38  
39      private static final PluginClassLoaderFactory instance = new PluginClassLoaderFactory();
40  
41      private PluginClassLoaderFactory() {
42          this.pluginClassLoader = PluginClassLoaderFactory.create(new File(SettingsManager.getInstance().getSettingsDirectory() + File.separator + "plugins"));
43  
44      }
45  
46      public static PluginClassLoaderFactory getInstance() {
47          return instance;
48      }
49  
50      public ClassLoader getClassLoader() {
51          return this.pluginClassLoader;
52      }
53  
54      /**
55       * Creates a Classloader that will be able to access any of the classes found
56       * in any .JAR file contained within the specified directory path, PLUS
57       * the actual Plugin directory itself, so it acts like the WEB-INF/classes directory,
58       * any class file in the directory will be accessible
59       *
60       * @param pluginDirectory
61       * @return
62       * @throws IllegalArgumentException if the pluginDirectory is null, does not exist, or cannot be read
63       * @throws RuntimeException         if turning a File into a URL failed, which would be very unexpected
64       */
65      private static final ClassLoader create(File pluginDirectory) {
66          if (pluginDirectory == null || !pluginDirectory.exists() || !pluginDirectory.canRead()) {
67              return PluginClassLoaderFactory.class.getClassLoader();
68          }
69  
70          String[] strings = pluginDirectory.list((dir, name) -> name.toUpperCase().endsWith(".JAR"));
71  
72  
73          List<URL> list = new ArrayList<>();
74          // add the plugin directory as a resource loading path
75          try {
76              list.add(pluginDirectory.toURI().toURL());
77          } catch (Exception e) {
78              throw new RuntimeException(e.getMessage());
79          }
80          if (strings != null) {
81              for (String name : strings) {
82                  File file = new File(pluginDirectory, name);
83                  try {
84                      list.add(file.toURI().toURL());
85                      System.out.println("Added " + file.getAbsolutePath()
86                          + " to Plugin class loader list");
87                  } catch (Exception e) {
88                      System.err.println("Failed to retrieve the URL for file: "
89                          + file.getAbsolutePath());
90                      throw new RuntimeException(e.getMessage());
91                  }
92              }
93          }
94          ClassLoader parent = PluginClassLoaderFactory.class.getClassLoader();
95          URL[] urls = (URL[]) list.toArray(new URL[list.size()]);
96          return new URLClassLoader(urls, parent);
97      }
98  
99  }