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