1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.deploy.hot;
21  
22  import com.liferay.portal.kernel.deploy.hot.HotDeployEvent;
23  import com.liferay.portal.kernel.deploy.hot.HotDeployException;
24  import com.liferay.portal.kernel.log.Log;
25  import com.liferay.portal.kernel.log.LogFactoryUtil;
26  import com.liferay.portal.kernel.util.HttpUtil;
27  import com.liferay.portal.theme.ThemeLoaderFactory;
28  import com.liferay.portal.velocity.VelocityContextPool;
29  
30  import javax.servlet.ServletContext;
31  
32  /**
33   * <a href="ThemeLoaderHotDeployListener.java.html"><b><i>View Source</i></b>
34   * </a>
35   *
36   * @author Brian Wing Shun Chan
37   *
38   */
39  public class ThemeLoaderHotDeployListener extends BaseHotDeployListener {
40  
41      public void invokeDeploy(HotDeployEvent event) throws HotDeployException {
42          try {
43              doInvokeDeploy(event);
44          }
45          catch (Throwable t) {
46              throwHotDeployException(
47                  event, "Error registering theme loader for ", t);
48          }
49      }
50  
51      public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
52          try {
53              doInvokeUndeploy(event);
54          }
55          catch (Throwable t) {
56              throwHotDeployException(
57                  event, "Error unregistering theme loader for ", t);
58          }
59      }
60  
61      protected void doInvokeDeploy(HotDeployEvent event) throws Exception {
62          ServletContext servletContext = event.getServletContext();
63  
64          String servletContextName = servletContext.getServletContextName();
65  
66          if (_log.isDebugEnabled()) {
67              _log.debug("Invoking deploy for " + servletContextName);
68          }
69  
70          String[] xmls = new String[] {
71              HttpUtil.URLtoString(
72                  servletContext.getResource("/WEB-INF/liferay-theme-loader.xml"))
73          };
74  
75          if (xmls[0] == null) {
76              return;
77          }
78  
79          if (_log.isInfoEnabled()) {
80              _log.info("Registering theme loader for " + servletContextName);
81          }
82  
83          ThemeLoaderFactory.init(servletContextName, servletContext, xmls);
84      }
85  
86      protected void doInvokeUndeploy(HotDeployEvent event) throws Exception {
87          ServletContext servletContext = event.getServletContext();
88  
89          String servletContextName = servletContext.getServletContextName();
90  
91          if (_log.isDebugEnabled()) {
92              _log.debug("Invoking undeploy for " + servletContextName);
93          }
94  
95          boolean value = ThemeLoaderFactory.destroy(servletContextName);
96  
97          if (!value) {
98              return;
99          }
100 
101         if (_log.isInfoEnabled()) {
102             _log.info("Unregistering theme loader for " + servletContextName);
103         }
104 
105         VelocityContextPool.remove(servletContextName);
106 
107         if (_log.isInfoEnabled()) {
108             _log.info(
109                 "Theme loader for " + servletContextName +
110                     " unregistered successfully");
111         }
112     }
113 
114     private static Log _log =
115         LogFactoryUtil.getLog(ThemeLoaderHotDeployListener.class);
116 
117 }