View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.reboot;
8   
9   import java.io.File;
10  import java.io.FileFilter;
11  import java.io.IOException;
12  import java.net.MalformedURLException;
13  import java.net.URL;
14  import java.util.ArrayList;
15  import java.util.Arrays;
16  import java.util.Collections;
17  import java.util.Iterator;
18  import java.util.List;
19  
20  /**
21   * Constructs a default set of JAR Urls located under Mule home folder.
22   */
23  //TODO this duplicates DefaultMuleClassPathConfig in the boot module. See if this class can be moved to mule-core
24  public class DefaultMuleClassPathConfig
25  {
26      protected static final String MULE_DIR = "/lib/mule";
27      protected static final String USER_DIR = "/lib/user";
28      protected static final String OPT_DIR = "/lib/opt";
29  
30      protected List<URL> urls = new ArrayList<URL>();
31  
32      public DefaultMuleClassPathConfig(File muleHome, File muleBase)
33      {
34          init(muleHome, muleBase);
35      }
36  
37      protected void init(File muleHome, File muleBase)
38      {
39          /*
40           * Pick up any local jars, if there are any. Doing this here insures that any
41           * local class that override the global classes will in fact do so.
42           */
43          addMuleBaseUserLibs(muleHome, muleBase);
44  
45          addLibraryDirectory(muleHome, USER_DIR);
46          addLibraryDirectory(muleHome, MULE_DIR);
47          addLibraryDirectory(muleHome, OPT_DIR);
48      }
49  
50      protected void addMuleBaseUserLibs(File muleHome, File muleBase)
51      {
52          try
53          {
54              if (!muleHome.getCanonicalFile().equals(muleBase.getCanonicalFile()))
55              {
56                  File userOverrideDir = new File(muleBase, USER_DIR);
57                  addFile(userOverrideDir);
58                  addFiles(listJars(userOverrideDir));
59              }
60          }
61          catch (IOException ioe)
62          {
63              System.out.println("Unable to check to see if there are local jars to load: " + ioe.toString());
64          }
65      }
66  
67      protected void addLibraryDirectory(File muleHome, String libDirectory)
68      {
69          File directory = new File(muleHome, libDirectory);
70          addFile(directory);
71          addFiles(listJars(directory));
72      }
73  
74      public List<URL> getURLs()
75      {
76          return new ArrayList<URL>(this.urls);
77      }
78  
79      public void addURLs(List<URL> moreUrls)
80      {
81          if (moreUrls != null && !moreUrls.isEmpty())
82          {
83              this.urls.addAll(moreUrls);
84          }
85      }
86  
87      /**
88       * Add a URL to Mule's classpath.
89       *
90       * @param url folder (should end with a slash) or jar path
91       */
92      public void addURL(URL url)
93      {
94          this.urls.add(url);
95      }
96  
97      public void addFiles(List<File> files)
98      {
99          for (Iterator<File> i = files.iterator(); i.hasNext();)
100         {
101             this.addFile(i.next());
102         }
103     }
104 
105     public void addFile(File jar)
106     {
107         try
108         {
109             this.addURL(jar.getAbsoluteFile().toURI().toURL());
110         }
111         catch (MalformedURLException mux)
112         {
113             throw new RuntimeException("Failed to construct a classpath URL", mux);
114         }
115     }
116 
117     /**
118      * Find and if necessary filter the jars for classpath.
119      *
120      * @return a list of {@link java.io.File}s
121      */
122     protected List<File> listJars(File path)
123     {
124         File[] jars = path.listFiles(new FileFilter()
125         {
126             public boolean accept(File pathname)
127             {
128                 try
129                 {
130                     return pathname.getCanonicalPath().endsWith(".jar");
131                 }
132                 catch (IOException e)
133                 {
134                     throw new RuntimeException(e.getMessage());
135                 }
136             }
137         });
138 
139         if (jars != null)
140         {
141             return Arrays.asList(jars);
142         }
143         return Collections.emptyList();
144     }
145 
146 }