Coverage Report - org.mule.module.launcher.application.ApplicationWrapper
 
Classes in this File Line Coverage Branch Coverage Complexity
ApplicationWrapper
0%
0/55
0%
0/14
0
 
 1  
 /*
 2  
  * $Id: ApplicationWrapper.java 19667 2010-09-16 16:32:41Z aperepel $
 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.launcher.application;
 12  
 
 13  
 import org.mule.api.MuleContext;
 14  
 import org.mule.module.launcher.DeploymentStartException;
 15  
 import org.mule.module.launcher.GoodCitizenClassLoader;
 16  
 import org.mule.module.launcher.InstallException;
 17  
 import org.mule.module.launcher.descriptor.ApplicationDescriptor;
 18  
 
 19  
 import java.io.IOException;
 20  
 
 21  
 /**
 22  
  * Decorates the target deployer to properly switch out context classloader for deployment
 23  
  * one where applicable. E.g. init() phase may load custom classes for an application, which
 24  
  * must be executed with deployment (app) classloader in the context, and not Mule system
 25  
  * classloader.
 26  
  */
 27  
 public class ApplicationWrapper implements Application
 28  
 {
 29  
 
 30  
     private Application delegate;
 31  
 
 32  
     protected ApplicationWrapper(Application delegate) throws IOException
 33  0
     {
 34  0
         this.delegate = delegate;
 35  0
     }
 36  
 
 37  
     public void dispose()
 38  
     {
 39  0
         final ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
 40  
         try
 41  
         {
 42  0
             ClassLoader appCl = getDeploymentClassLoader();
 43  
             // if not initialized yet, it can be null
 44  0
             if (appCl != null)
 45  
             {
 46  0
                 Thread.currentThread().setContextClassLoader(appCl);
 47  
             }
 48  0
             delegate.dispose();
 49  0
             if (appCl != null)
 50  
             {
 51  
                 // close classloader to release jar connections in lieu of Java 7's ClassLoader.close()
 52  0
                 if (appCl instanceof GoodCitizenClassLoader)
 53  
                 {
 54  0
                     GoodCitizenClassLoader classLoader = (GoodCitizenClassLoader) appCl;
 55  0
                     classLoader.close();
 56  
                 }
 57  
             }
 58  
         }
 59  
         finally
 60  
         {
 61  0
             Thread.currentThread().setContextClassLoader(originalCl);
 62  0
         }
 63  0
     }
 64  
 
 65  
     public ClassLoader getDeploymentClassLoader()
 66  
     {
 67  0
         return delegate.getDeploymentClassLoader();
 68  
     }
 69  
 
 70  
     public MuleContext getMuleContext()
 71  
     {
 72  0
         return delegate.getMuleContext();
 73  
     }
 74  
 
 75  
     public void init()
 76  
     {
 77  0
         final ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
 78  
         try
 79  
         {
 80  0
             ClassLoader appCl = getDeploymentClassLoader();
 81  
             // if not initialized yet, it can be null
 82  0
             if (appCl != null)
 83  
             {
 84  0
                 Thread.currentThread().setContextClassLoader(appCl);
 85  
             }
 86  0
             delegate.init();
 87  
         }
 88  
         finally
 89  
         {
 90  0
             Thread.currentThread().setContextClassLoader(originalCl);
 91  0
         }
 92  0
     }
 93  
 
 94  
     public void install() throws InstallException
 95  
     {
 96  0
         final ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
 97  
         try
 98  
         {
 99  0
             ClassLoader appCl = getDeploymentClassLoader();
 100  
             // if not initialized yet, it can be null
 101  0
             if (appCl != null)
 102  
             {
 103  0
                 Thread.currentThread().setContextClassLoader(appCl);
 104  
             }
 105  0
             delegate.install();
 106  
         }
 107  
         finally
 108  
         {
 109  0
             Thread.currentThread().setContextClassLoader(originalCl);
 110  0
         }
 111  0
     }
 112  
 
 113  
     public void redeploy()
 114  
     {
 115  0
         delegate.redeploy();
 116  0
     }
 117  
 
 118  
     public void start() throws DeploymentStartException
 119  
     {
 120  0
         final ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
 121  
         try
 122  
         {
 123  0
             ClassLoader appCl = getDeploymentClassLoader();
 124  
             // if not initialized yet, it can be null
 125  0
             if (appCl != null)
 126  
             {
 127  0
                 Thread.currentThread().setContextClassLoader(appCl);
 128  
             }
 129  0
             delegate.start();
 130  
         }
 131  
         finally
 132  
         {
 133  0
             Thread.currentThread().setContextClassLoader(originalCl);
 134  0
         }
 135  0
     }
 136  
 
 137  
     public void stop()
 138  
     {
 139  0
         final ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
 140  
         try
 141  
         {
 142  0
             ClassLoader appCl = getDeploymentClassLoader();
 143  
             // if not initialized yet, it can be null
 144  0
             if (appCl != null)
 145  
             {
 146  0
                 Thread.currentThread().setContextClassLoader(appCl);
 147  
             }
 148  0
             delegate.stop();
 149  
         }
 150  
         finally
 151  
         {
 152  0
             Thread.currentThread().setContextClassLoader(originalCl);
 153  0
         }
 154  0
     }
 155  
 
 156  
     public String getAppName()
 157  
     {
 158  0
         return delegate.getAppName();
 159  
     }
 160  
 
 161  
     public ApplicationDescriptor getDescriptor()
 162  
     {
 163  0
         return delegate.getDescriptor();
 164  
     }
 165  
 
 166  
     @Override
 167  
     public String toString()
 168  
     {
 169  0
         return String.format("%s(%s)", getClass().getName(), delegate);
 170  
     }
 171  
 
 172  
     public Application getDelegate()
 173  
     {
 174  0
         return delegate;
 175  
     }
 176  
 }