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.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  public abstract class BaseHotDeployListener implements HotDeployListener {
48  
49      public void throwHotDeployException(
50              HotDeployEvent event, String msg, Throwable t)
51          throws HotDeployException {
52  
53          ServletContext servletContext = event.getServletContext();
54  
55          String servletContextName = servletContext.getServletContextName();
56  
57          throw new HotDeployException(msg + servletContextName, t);
58      }
59  
60      protected void registerClpMessageListeners(
61              ServletContext servletContext, ClassLoader portletClassLoader)
62          throws Exception {
63  
64          List<MessageListener> clpMessageListeners =
65              (List<MessageListener>)servletContext.getAttribute(
66                  WebKeys.CLP_MESSAGE_LISTENERS);
67  
68          if (clpMessageListeners != null) {
69              return;
70          }
71  
72          clpMessageListeners = new ArrayList<MessageListener>();
73  
74          Set<String> classNames = ServletContextUtil.getClassNames(
75              servletContext);
76  
77          for (String className : classNames) {
78              if (className.endsWith(".ClpMessageListener")) {
79                  Class<?> clpMessageListenerClass = portletClassLoader.loadClass(
80                      className);
81  
82                  MessageListener clpMessageListener =
83                      (MessageListener)clpMessageListenerClass.newInstance();
84  
85                  Field servletContextNameField =
86                      clpMessageListenerClass.getField(
87                          "SERVLET_CONTEXT_NAME");
88  
89                  String clpServletContextName = servletContextNameField.get(
90                      clpMessageListener).toString();
91  
92                  if (clpServletContextName.equals(
93                          servletContext.getServletContextName())) {
94  
95                      continue;
96                  }
97  
98                  clpMessageListeners.add(clpMessageListener);
99  
100                 MessageBusUtil.registerMessageListener(
101                     DestinationNames.HOT_DEPLOY, clpMessageListener);
102             }
103         }
104 
105         servletContext.setAttribute(
106             WebKeys.CLP_MESSAGE_LISTENERS, clpMessageListeners);
107     }
108 
109     protected void unregisterClpMessageListeners(ServletContext servletContext)
110         throws Exception {
111 
112         List<MessageListener> clpMessageListeners =
113             (List<MessageListener>)servletContext.getAttribute(
114                 WebKeys.CLP_MESSAGE_LISTENERS);
115 
116         if (clpMessageListeners != null) {
117             servletContext.removeAttribute(WebKeys.CLP_MESSAGE_LISTENERS);
118 
119             for (MessageListener clpMessageListener : clpMessageListeners) {
120                 MessageBusUtil.unregisterMessageListener(
121                     DestinationNames.HOT_DEPLOY, clpMessageListener);
122             }
123         }
124     }
125 
126 }