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