View Javadoc

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