Coverage Report - org.mule.module.boot.DefaultMuleClassPathConfig
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultMuleClassPathConfig
77%
27/35
30%
3/10
2.125
DefaultMuleClassPathConfig$1
25%
1/4
N/A
2.125
 
 1  
 /*
 2  
  * $Id: DefaultMuleClassPathConfig.java 10789 2008-02-12 20:04:43Z dfeist $
 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.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  
 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  2
     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  2
     {
 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  2
             if (!muleHome.getCanonicalFile().equals(muleBase.getCanonicalFile()))
 49  
             {
 50  2
                 File userOverrideDir = new File(muleBase, USER_DIR);
 51  2
                 this.addFile(userOverrideDir);
 52  2
                 this.addFiles(this.listJars(userOverrideDir));
 53  
             }
 54  
         }
 55  0
         catch (IOException ioe)
 56  
         {
 57  0
             System.out.println("Unable to check to see if there are local jars to load: " + ioe.toString());
 58  2
         }
 59  
 
 60  2
         File userDir = new File(muleHome, USER_DIR);
 61  2
         this.addFile(userDir);
 62  2
         this.addFiles(this.listJars(userDir));
 63  
 
 64  2
         File muleDir = new File(muleHome, MULE_DIR);
 65  2
         this.addFile(muleDir);
 66  2
         this.addFiles(this.listJars(muleDir));
 67  
 
 68  2
         File optDir = new File(muleHome, OPT_DIR);
 69  2
         this.addFile(optDir);
 70  2
         this.addFiles(this.listJars(optDir));
 71  2
     }
 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  2
         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  0
         if (urls != null && !urls.isEmpty())
 91  
         {
 92  0
             this.urls.addAll(urls);
 93  
         }
 94  0
     }
 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  8
         this.urls.add(url);
 104  8
     }
 105  
 
 106  
     public void addFiles(List files)
 107  
     {
 108  8
         for (Iterator i = files.iterator(); i.hasNext();)
 109  
         {
 110  0
             this.addFile((File)i.next());
 111  
         }
 112  8
     }
 113  
 
 114  
     public void addFile(File jar)
 115  
     {
 116  
         try
 117  
         {
 118  8
             this.addURL(jar.getAbsoluteFile().toURI().toURL());
 119  
         }
 120  0
         catch (MalformedURLException mux)
 121  
         {
 122  0
             throw new RuntimeException("Failed to construct a classpath URL", mux);
 123  8
         }
 124  8
     }
 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  8
         File[] jars = path.listFiles(new FileFilter()
 134  
         {
 135  8
             public boolean accept(File pathname)
 136  
             {
 137  
                 try
 138  
                 {
 139  0
                     return pathname.getCanonicalPath().endsWith(".jar");
 140  
                 }
 141  0
                 catch (IOException e)
 142  
                 {
 143  0
                     throw new RuntimeException(e.getMessage());
 144  
                 }
 145  
             }
 146  
         });
 147  
 
 148  8
         return jars == null ? Collections.EMPTY_LIST : Arrays.asList(jars);
 149  
     }
 150  
 
 151  
 }