1
2
3
4
5
6
7
8
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
22
23 public class PluginClasspath
24 {
25
26 private URL runtimeClassesDir;
27 private URL[] runtimeLibs = new URL[0];
28
29 protected PluginClasspath()
30 {
31
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
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 }