View Javadoc

1   /*
2    * $Id: PluginClasspath.java 22140 2011-06-07 14:43:17Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.launcher.plugin;
12  
13  import java.io.File;
14  import java.io.FilenameFilter;
15  import java.net.MalformedURLException;
16  import java.net.URL;
17  
18  import org.apache.commons.io.filefilter.SuffixFileFilter;
19  
20  /**
21   * Represents a physical classpath of the plugin after it was unpacked/deployed.
22   */
23  public class PluginClasspath
24  {
25  
26      private URL runtimeClassesDir;
27      private URL[] runtimeLibs = new URL[0];
28  
29      protected PluginClasspath()
30      {
31          // non-public, use a factory method instead
32      }
33  
34      public static PluginClasspath from(File pluginDir)
35      {
36          if (!pluginDir.exists())
37          {
38              throw new IllegalArgumentException("Can't read from the temporary plugin directory: " + pluginDir);
39          }
40          final PluginClasspath cp = new PluginClasspath();
41  
42          try
43          {
44              cp.setRuntimeClassesDir(new File(pluginDir, "classes").toURI().toURL());
45              final File libDir = new File(pluginDir, "lib");
46              if (libDir.exists())
47              {
48                  final File[] jars = libDir.listFiles((FilenameFilter) new SuffixFileFilter(".jar"));
49                  URL[] urls = new URL[jars.length];
50                  for (int i = 0; i < jars.length; i++)
51                  {
52                      urls[i] = jars[i].toURI().toURL();
53                  }
54                  cp.setRuntimeLibs(urls);
55              }
56          }
57          catch (MalformedURLException e)
58          {
59              throw new IllegalArgumentException("Failed to create plugin classpath " + pluginDir);
60          }
61          return cp;
62      }
63  
64      public URL[] getRuntimeLibs()
65      {
66          return runtimeLibs;
67      }
68  
69      public URL getRuntimeClassesDir()
70      {
71          return runtimeClassesDir;
72      }
73  
74      /**
75       * @return merged classpath, 'classes' dir coming first
76       */
77      public URL[] toURLs()
78      {
79          URL[] merged = new URL[runtimeLibs.length + 1];
80          merged[0] = runtimeClassesDir;
81          System.arraycopy(runtimeLibs, 0, merged, 1, runtimeLibs.length);
82  
83          return merged;
84      }
85  
86      protected void setRuntimeClassesDir(URL runtimeClassesDir)
87      {
88          this.runtimeClassesDir = runtimeClassesDir;
89      }
90  
91      protected void setRuntimeLibs(URL[] runtimeLibs)
92      {
93          this.runtimeLibs = runtimeLibs;
94      }
95  
96  }