View Javadoc

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