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.deploy.hot.HotDeployListener;
25  import com.liferay.portal.kernel.messaging.DestinationNames;
26  import com.liferay.portal.kernel.messaging.MessageBusUtil;
27  import com.liferay.portal.kernel.messaging.MessageListener;
28  import com.liferay.portal.kernel.servlet.ServletContextUtil;
29  import com.liferay.portal.util.WebKeys;
30  
31  import java.lang.reflect.Field;
32  
33  import java.util.ArrayList;
34  import java.util.List;
35  import java.util.Set;
36  
37  import javax.servlet.ServletContext;
38  
39  /**
40   * <a href="BaseHotDeployListener.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public abstract class BaseHotDeployListener implements HotDeployListener {
46  
47      public void throwHotDeployException(
48              HotDeployEvent event, String msg, Throwable t)
49          throws HotDeployException {
50  
51          ServletContext servletContext = event.getServletContext();
52  
53          String servletContextName = servletContext.getServletContextName();
54  
55          throw new HotDeployException(msg + servletContextName, t);
56      }
57  
58      protected void registerClpMessageListeners(
59              ServletContext servletContext, ClassLoader portletClassLoader)
60          throws Exception {
61  
62          List<MessageListener> clpMessageListeners =
63              (List<MessageListener>)servletContext.getAttribute(
64                  WebKeys.CLP_MESSAGE_LISTENERS);
65  
66          if (clpMessageListeners != null) {
67              return;
68          }
69  
70          clpMessageListeners = new ArrayList<MessageListener>();
71  
72          Set<String> classNames = ServletContextUtil.getClassNames(
73              servletContext);
74  
75          for (String className : classNames) {
76              if (className.endsWith(".ClpMessageListener")) {
77                  Class<?> clpMessageListenerClass = portletClassLoader.loadClass(
78                      className);
79  
80                  MessageListener clpMessageListener =
81                      (MessageListener)clpMessageListenerClass.newInstance();
82  
83                  Field servletContextNameField =
84                      clpMessageListenerClass.getField(
85                          "SERVLET_CONTEXT_NAME");
86  
87                  String clpServletContextName = servletContextNameField.get(
88                      clpMessageListener).toString();
89  
90                  if (clpServletContextName.equals(
91                          servletContext.getServletContextName())) {
92  
93                      continue;
94                  }
95  
96                  clpMessageListeners.add(clpMessageListener);
97  
98                  MessageBusUtil.registerMessageListener(
99                      DestinationNames.HOT_DEPLOY, clpMessageListener);
100             }
101         }
102 
103         servletContext.setAttribute(
104             WebKeys.CLP_MESSAGE_LISTENERS, clpMessageListeners);
105     }
106 
107     protected void unregisterClpMessageListeners(ServletContext servletContext)
108         throws Exception {
109 
110         List<MessageListener> clpMessageListeners =
111             (List<MessageListener>)servletContext.getAttribute(
112                 WebKeys.CLP_MESSAGE_LISTENERS);
113 
114         if (clpMessageListeners != null) {
115             servletContext.removeAttribute(WebKeys.CLP_MESSAGE_LISTENERS);
116 
117             for (MessageListener clpMessageListener : clpMessageListeners) {
118                 MessageBusUtil.unregisterMessageListener(
119                     DestinationNames.HOT_DEPLOY, clpMessageListener);
120             }
121         }
122     }
123 
124 }