1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.HotDeployEvent;
26  import com.liferay.portal.kernel.deploy.hot.HotDeployException;
27  import com.liferay.portal.kernel.deploy.hot.HotDeployListener;
28  import com.liferay.portal.kernel.messaging.DestinationNames;
29  import com.liferay.portal.kernel.messaging.MessageBusUtil;
30  import com.liferay.portal.kernel.messaging.MessageListener;
31  import com.liferay.portal.kernel.servlet.ServletContextUtil;
32  import com.liferay.portal.util.WebKeys;
33  
34  import java.lang.reflect.Field;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  import java.util.Set;
39  
40  import javax.servlet.ServletContext;
41  
42  /**
43   * <a href="BaseHotDeployListener.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public abstract class BaseHotDeployListener implements HotDeployListener {
49  
50      public void throwHotDeployException(
51              HotDeployEvent event, String msg, Throwable t)
52          throws HotDeployException {
53  
54          ServletContext servletContext = event.getServletContext();
55  
56          String servletContextName = servletContext.getServletContextName();
57  
58          throw new HotDeployException(msg + servletContextName, t);
59      }
60  
61      protected void registerClpMessageListeners(
62              ServletContext servletContext, ClassLoader portletClassLoader)
63          throws Exception {
64  
65          List<MessageListener> clpMessageListeners =
66              (List<MessageListener>)servletContext.getAttribute(
67                  WebKeys.CLP_MESSAGE_LISTENERS);
68  
69          if (clpMessageListeners != null) {
70              return;
71          }
72  
73          clpMessageListeners = new ArrayList<MessageListener>();
74  
75          Set<String> classNames = ServletContextUtil.getClassNames(
76              servletContext);
77  
78          for (String className : classNames) {
79              if (className.endsWith(".ClpMessageListener")) {
80                  Class<?> clpMessageListenerClass = portletClassLoader.loadClass(
81                      className);
82  
83                  MessageListener clpMessageListener =
84                      (MessageListener)clpMessageListenerClass.newInstance();
85  
86                  Field servletContextNameField =
87                      clpMessageListenerClass.getField(
88                          "SERVLET_CONTEXT_NAME");
89  
90                  String clpServletContextName = servletContextNameField.get(
91                      clpMessageListener).toString();
92  
93                  if (clpServletContextName.equals(
94                          servletContext.getServletContextName())) {
95  
96                      continue;
97                  }
98  
99                  clpMessageListeners.add(clpMessageListener);
100 
101                 MessageBusUtil.registerMessageListener(
102                     DestinationNames.HOT_DEPLOY, clpMessageListener);
103             }
104         }
105 
106         servletContext.setAttribute(
107             WebKeys.CLP_MESSAGE_LISTENERS, clpMessageListeners);
108     }
109 
110     protected void unregisterClpMessageListeners(ServletContext servletContext)
111         throws Exception {
112 
113         List<MessageListener> clpMessageListeners =
114             (List<MessageListener>)servletContext.getAttribute(
115                 WebKeys.CLP_MESSAGE_LISTENERS);
116 
117         if (clpMessageListeners != null) {
118             servletContext.removeAttribute(WebKeys.CLP_MESSAGE_LISTENERS);
119 
120             for (MessageListener clpMessageListener : clpMessageListeners) {
121                 MessageBusUtil.unregisterMessageListener(
122                     DestinationNames.HOT_DEPLOY, clpMessageListener);
123             }
124         }
125     }
126 
127 }