View Javadoc

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