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.application;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.module.launcher.DeploymentStartException;
11  import org.mule.module.launcher.InstallException;
12  import org.mule.module.launcher.descriptor.ApplicationDescriptor;
13  
14  import java.io.IOException;
15  
16  /**
17   * Decorates the target deployer to properly switch out context classloader for deployment
18   * one where applicable. E.g. init() phase may load custom classes for an application, which
19   * must be executed with deployment (app) classloader in the context, and not Mule system
20   * classloader.
21   */
22  public class ApplicationWrapper implements Application
23  {
24  
25      private Application delegate;
26  
27      protected ApplicationWrapper(Application delegate) throws IOException
28      {
29          this.delegate = delegate;
30      }
31  
32      public void dispose()
33      {
34          // moved wrapper logic into the actual implementation, as redeploy() invokes it directly, bypassing
35          // classloader cleanup
36          delegate.dispose();
37      }
38  
39      public ClassLoader getDeploymentClassLoader()
40      {
41          return delegate.getDeploymentClassLoader();
42      }
43  
44      public MuleContext getMuleContext()
45      {
46          return delegate.getMuleContext();
47      }
48  
49      public void init()
50      {
51          final ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
52          try
53          {
54              ClassLoader appCl = getDeploymentClassLoader();
55              // if not initialized yet, it can be null
56              if (appCl != null)
57              {
58                  Thread.currentThread().setContextClassLoader(appCl);
59              }
60              delegate.init();
61          }
62          finally
63          {
64              Thread.currentThread().setContextClassLoader(originalCl);
65          }
66      }
67  
68      public void install() throws InstallException
69      {
70          final ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
71          try
72          {
73              ClassLoader appCl = getDeploymentClassLoader();
74              // if not initialized yet, it can be null
75              if (appCl != null)
76              {
77                  Thread.currentThread().setContextClassLoader(appCl);
78              }
79              delegate.install();
80          }
81          finally
82          {
83              Thread.currentThread().setContextClassLoader(originalCl);
84          }
85      }
86  
87      public void redeploy()
88      {
89          delegate.redeploy();
90      }
91  
92      public void start() throws DeploymentStartException
93      {
94          final ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
95          try
96          {
97              ClassLoader appCl = getDeploymentClassLoader();
98              // if not initialized yet, it can be null
99              if (appCl != null)
100             {
101                 Thread.currentThread().setContextClassLoader(appCl);
102             }
103             delegate.start();
104         }
105         finally
106         {
107             Thread.currentThread().setContextClassLoader(originalCl);
108         }
109     }
110 
111     public void stop()
112     {
113         final ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
114         try
115         {
116             ClassLoader appCl = getDeploymentClassLoader();
117             // if not initialized yet, it can be null
118             if (appCl != null)
119             {
120                 Thread.currentThread().setContextClassLoader(appCl);
121             }
122             delegate.stop();
123         }
124         finally
125         {
126             Thread.currentThread().setContextClassLoader(originalCl);
127         }
128     }
129 
130     public String getAppName()
131     {
132         return delegate.getAppName();
133     }
134 
135     public ApplicationDescriptor getDescriptor()
136     {
137         return delegate.getDescriptor();
138     }
139 
140     @Override
141     public String toString()
142     {
143         return String.format("%s(%s)", getClass().getName(), delegate);
144     }
145 
146     public Application getDelegate()
147     {
148         return delegate;
149     }
150 }