View Javadoc
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.launcher;
8   
9   import org.mule.module.reboot.MuleContainerBootstrapUtils;
10  import org.mule.util.FileUtils;
11  import org.mule.util.SystemUtils;
12  
13  import java.io.File;
14  import java.net.URL;
15  import java.util.Collection;
16  
17  import org.apache.commons.logging.Log;
18  import org.apache.commons.logging.LogFactory;
19  
20  /**
21   *  Load $MULE_HOME/lib/shared/<domain> libraries.
22   */
23  public class MuleSharedDomainClassLoader extends GoodCitizenClassLoader
24  {
25  
26      protected transient Log logger = LogFactory.getLog(getClass());
27  
28      private final String domain = "undefined";
29  
30      @SuppressWarnings("unchecked")
31      public MuleSharedDomainClassLoader(String domain, ClassLoader parent)
32      {
33          super(new URL[0], parent);
34          try
35          {
36              File domainDir = new File(MuleContainerBootstrapUtils.getMuleHome(), "lib/shared/" + domain);
37              if (!domainDir.exists())
38              {
39                  throw new IllegalArgumentException(
40                          String.format("Shared ClassLoader Domain '%s' doesn't exist", domain));
41              }
42  
43              if (!domainDir.canRead())
44              {
45                  throw new IllegalArgumentException(
46                          String.format("Shared ClassLoader Domain '%s' is not accessible", domain));
47              }
48  
49              Collection<File> jars = FileUtils.listFiles(domainDir, new String[] {"jar"}, false);
50  
51              if (logger.isDebugEnabled())
52              {
53                  {
54                      StringBuilder sb = new StringBuilder();
55                      sb.append("Loading Shared ClassLoader Domain: ").append(domain).append(SystemUtils.LINE_SEPARATOR);
56                      sb.append("=============================").append(SystemUtils.LINE_SEPARATOR);
57  
58                      for (File jar : jars)
59                      {
60                          sb.append(jar.toURI().toURL()).append(SystemUtils.LINE_SEPARATOR);
61                      }
62  
63                      sb.append("=============================").append(SystemUtils.LINE_SEPARATOR);
64  
65                      logger.debug(sb.toString());
66                  }
67              }
68  
69              for (File jar : jars)
70              {
71                  addURL(jar.toURI().toURL());
72              }
73          }
74          catch (Throwable t)
75          {
76              throw new RuntimeException(t);
77          }
78      }
79  
80      public String getDomain()
81      {
82          return domain;
83      }
84  
85      @Override
86      public String toString()
87      {
88          return String.format("%s[%s]@%s", getClass().getName(),
89                               domain,
90                               Integer.toHexString(System.identityHashCode(this)));
91      }
92  
93  }