View Javadoc

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      {
34          this.delegate = delegate;
35      }
36  
37      public void dispose()
38      {
39          final ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
40          try
41          {
42              ClassLoader appCl = getDeploymentClassLoader();
43              // if not initialized yet, it can be null
44              if (appCl != null)
45              {
46                  Thread.currentThread().setContextClassLoader(appCl);
47              }
48              delegate.dispose();
49              if (appCl != null)
50              {
51                  // close classloader to release jar connections in lieu of Java 7's ClassLoader.close()
52                  if (appCl instanceof GoodCitizenClassLoader)
53                  {
54                      GoodCitizenClassLoader classLoader = (GoodCitizenClassLoader) appCl;
55                      classLoader.close();
56                  }
57              }
58          }
59          finally
60          {
61              Thread.currentThread().setContextClassLoader(originalCl);
62          }
63      }
64  
65      public ClassLoader getDeploymentClassLoader()
66      {
67          return delegate.getDeploymentClassLoader();
68      }
69  
70      public MuleContext getMuleContext()
71      {
72          return delegate.getMuleContext();
73      }
74  
75      public void init()
76      {
77          final ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
78          try
79          {
80              ClassLoader appCl = getDeploymentClassLoader();
81              // if not initialized yet, it can be null
82              if (appCl != null)
83              {
84                  Thread.currentThread().setContextClassLoader(appCl);
85              }
86              delegate.init();
87          }
88          finally
89          {
90              Thread.currentThread().setContextClassLoader(originalCl);
91          }
92      }
93  
94      public void install() throws InstallException
95      {
96          final ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
97          try
98          {
99              ClassLoader appCl = getDeploymentClassLoader();
100             // if not initialized yet, it can be null
101             if (appCl != null)
102             {
103                 Thread.currentThread().setContextClassLoader(appCl);
104             }
105             delegate.install();
106         }
107         finally
108         {
109             Thread.currentThread().setContextClassLoader(originalCl);
110         }
111     }
112 
113     public void redeploy()
114     {
115         delegate.redeploy();
116     }
117 
118     public void start() throws DeploymentStartException
119     {
120         final ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
121         try
122         {
123             ClassLoader appCl = getDeploymentClassLoader();
124             // if not initialized yet, it can be null
125             if (appCl != null)
126             {
127                 Thread.currentThread().setContextClassLoader(appCl);
128             }
129             delegate.start();
130         }
131         finally
132         {
133             Thread.currentThread().setContextClassLoader(originalCl);
134         }
135     }
136 
137     public void stop()
138     {
139         final ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
140         try
141         {
142             ClassLoader appCl = getDeploymentClassLoader();
143             // if not initialized yet, it can be null
144             if (appCl != null)
145             {
146                 Thread.currentThread().setContextClassLoader(appCl);
147             }
148             delegate.stop();
149         }
150         finally
151         {
152             Thread.currentThread().setContextClassLoader(originalCl);
153         }
154     }
155 
156     public String getAppName()
157     {
158         return delegate.getAppName();
159     }
160 
161     public ApplicationDescriptor getDescriptor()
162     {
163         return delegate.getDescriptor();
164     }
165 
166     @Override
167     public String toString()
168     {
169         return String.format("%s(%s)", getClass().getName(), delegate);
170     }
171 
172     public Application getDelegate()
173     {
174         return delegate;
175     }
176 }