View Javadoc

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