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.boot;
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 reboot 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      private List urls = new ArrayList();
31  
32      /**
33       * Constructs a new DefaultMuleClassPathConfig.
34       * @param muleHome Mule home directory
35       * @param muleBase Mule base directory
36       */
37      public DefaultMuleClassPathConfig(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          try
44          {
45              if (!muleHome.getCanonicalFile().equals(muleBase.getCanonicalFile()))
46              {
47                  File userOverrideDir = new File(muleBase, USER_DIR);
48                  this.addFile(userOverrideDir);
49                  this.addFiles(this.listJars(userOverrideDir));
50              }
51          }
52          catch (IOException ioe)
53          {
54              System.out.println("Unable to check to see if there are local jars to load: " + ioe.toString());
55          }
56  
57          File userDir = new File(muleHome, USER_DIR);
58          this.addFile(userDir);
59          this.addFiles(this.listJars(userDir));
60  
61          File muleDir = new File(muleHome, MULE_DIR);
62          this.addFile(muleDir);
63          this.addFiles(this.listJars(muleDir));
64  
65          File optDir = new File(muleHome, OPT_DIR);
66          this.addFile(optDir);
67          this.addFiles(this.listJars(optDir));
68      }
69  
70      /**
71       * Getter for property 'urls'.
72       *
73       * @return A copy of 'urls'. Items are java.net.URL
74       */
75      public List getURLs()
76      {
77          return new ArrayList(this.urls);
78      }
79  
80      /**
81       * Setter for property 'urls'.
82       *
83       * @param urls Value to set for property 'urls'.
84       */
85      public void addURLs(List urls)
86      {
87          if (urls != null && !urls.isEmpty())
88          {
89              this.urls.addAll(urls);
90          }
91      }
92  
93      /**
94       * Add a URL to Mule's classpath.
95       *
96       * @param url folder (should end with a slash) or jar path
97       */
98      public void addURL(URL url)
99      {
100         this.urls.add(url);
101     }
102 
103     public void addFiles(List files)
104     {
105         for (Iterator i = files.iterator(); i.hasNext();)
106         {
107             this.addFile((File)i.next());
108         }
109     }
110 
111     public void addFile(File jar)
112     {
113         try
114         {
115             this.addURL(jar.getAbsoluteFile().toURI().toURL());
116         }
117         catch (MalformedURLException mux)
118         {
119             throw new RuntimeException("Failed to construct a classpath URL", mux);
120         }
121     }
122 
123     /**
124      * Find and if necessary filter the jars for classpath.
125      *
126      * @return a list of {@link File}s
127      */
128     protected List listJars(File path)
129     {
130         File[] jars = path.listFiles(new FileFilter()
131         {
132             public boolean accept(File pathname)
133             {
134                 try
135                 {
136                     return pathname.getCanonicalPath().endsWith(".jar");
137                 }
138                 catch (IOException e)
139                 {
140                     throw new RuntimeException(e.getMessage());
141                 }
142             }
143         });
144 
145         return jars == null ? Collections.EMPTY_LIST : Arrays.asList(jars);
146     }
147 
148 }