View Javadoc

1   /*
2    * $Id: DefaultMuleClassPathConfig.java 19191 2010-08-25 21:05:23Z tcarlson $
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.boot;
12  
13  import java.io.File;
14  import java.io.FileFilter;
15  import java.io.IOException;
16  import java.net.MalformedURLException;
17  import java.net.URL;
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.Collections;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  /**
25   * Constructs a default set of JAR Urls located under Mule home folder.
26   */
27  // TODO this duplicates DefaultMuleClassPathConfig in the reboot module. See if this class can be moved to mule-core
28  public class DefaultMuleClassPathConfig
29  {
30      protected static final String MULE_DIR = "/lib/mule";
31      protected static final String USER_DIR = "/lib/user";
32      protected static final String OPT_DIR = "/lib/opt";
33  
34      private List urls = new ArrayList();
35  
36      /**
37       * Constructs a new DefaultMuleClassPathConfig.
38       * @param muleHome Mule home directory
39       * @param muleBase Mule base directory
40       */
41      public DefaultMuleClassPathConfig(File muleHome, File muleBase)
42      {
43          /**
44           * Pick up any local jars, if there are any. Doing this here insures that any
45           * local class that override the global classes will in fact do so.
46           */
47          try
48          {
49              if (!muleHome.getCanonicalFile().equals(muleBase.getCanonicalFile()))
50              {
51                  File userOverrideDir = new File(muleBase, USER_DIR);
52                  this.addFile(userOverrideDir);
53                  this.addFiles(this.listJars(userOverrideDir));
54              }
55          }
56          catch (IOException ioe)
57          {
58              System.out.println("Unable to check to see if there are local jars to load: " + ioe.toString());
59          }
60  
61          File userDir = new File(muleHome, USER_DIR);
62          this.addFile(userDir);
63          this.addFiles(this.listJars(userDir));
64  
65          File muleDir = new File(muleHome, MULE_DIR);
66          this.addFile(muleDir);
67          this.addFiles(this.listJars(muleDir));
68  
69          File optDir = new File(muleHome, OPT_DIR);
70          this.addFile(optDir);
71          this.addFiles(this.listJars(optDir));
72      }
73  
74      /**
75       * Getter for property 'urls'.
76       *
77       * @return A copy of 'urls'. Items are java.net.URL
78       */
79      public List getURLs()
80      {
81          return new ArrayList(this.urls);
82      }
83  
84      /**
85       * Setter for property 'urls'.
86       *
87       * @param urls Value to set for property 'urls'.
88       */
89      public void addURLs(List urls)
90      {
91          if (urls != null && !urls.isEmpty())
92          {
93              this.urls.addAll(urls);
94          }
95      }
96  
97      /**
98       * Add a URL to Mule's classpath.
99       *
100      * @param url folder (should end with a slash) or jar path
101      */
102     public void addURL(URL url)
103     {
104         this.urls.add(url);
105     }
106 
107     public void addFiles(List files)
108     {
109         for (Iterator i = files.iterator(); i.hasNext();)
110         {
111             this.addFile((File)i.next());
112         }
113     }
114 
115     public void addFile(File jar)
116     {
117         try
118         {
119             this.addURL(jar.getAbsoluteFile().toURI().toURL());
120         }
121         catch (MalformedURLException mux)
122         {
123             throw new RuntimeException("Failed to construct a classpath URL", mux);
124         }
125     }
126 
127     /**
128      * Find and if necessary filter the jars for classpath.
129      *
130      * @return a list of {@link File}s
131      */
132     protected List listJars(File path)
133     {
134         File[] jars = path.listFiles(new FileFilter()
135         {
136             public boolean accept(File pathname)
137             {
138                 try
139                 {
140                     return pathname.getCanonicalPath().endsWith(".jar");
141                 }
142                 catch (IOException e)
143                 {
144                     throw new RuntimeException(e.getMessage());
145                 }
146             }
147         });
148 
149         return jars == null ? Collections.EMPTY_LIST : Arrays.asList(jars);
150     }
151 
152 }