001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.deploy.hot;
016    
017    import com.liferay.portal.kernel.deploy.hot.BaseHotDeployListener;
018    import com.liferay.portal.kernel.deploy.hot.HotDeployEvent;
019    import com.liferay.portal.kernel.deploy.hot.HotDeployException;
020    import com.liferay.portal.kernel.messaging.DestinationNames;
021    import com.liferay.portal.kernel.messaging.Message;
022    import com.liferay.portal.kernel.messaging.MessageBusUtil;
023    
024    import javax.servlet.ServletContext;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class MessagingHotDeployListener extends BaseHotDeployListener {
030    
031            @Override
032            public void invokeDeploy(HotDeployEvent hotDeployEvent)
033                    throws HotDeployException {
034    
035                    try {
036                            doInvokeDeploy(hotDeployEvent);
037                    }
038                    catch (Throwable t) {
039                            throwHotDeployException(
040                                    hotDeployEvent,
041                                    "Error sending deploy message for " +
042                                            hotDeployEvent.getServletContextName(),
043                                    t);
044                    }
045            }
046    
047            @Override
048            public void invokeUndeploy(HotDeployEvent hotDeployEvent)
049                    throws HotDeployException {
050    
051                    try {
052                            doInvokeUndeploy(hotDeployEvent);
053                    }
054                    catch (Throwable t) {
055                            throwHotDeployException(
056                                    hotDeployEvent,
057                                    "Error sending undeploy message for " +
058                                            hotDeployEvent.getServletContextName(),
059                                    t);
060                    }
061            }
062    
063            protected void doInvokeDeploy(HotDeployEvent hotDeployEvent)
064                    throws Exception {
065    
066                    ServletContext servletContext = hotDeployEvent.getServletContext();
067    
068                    String servletContextName = servletContext.getServletContextName();
069    
070                    Message message = new Message();
071    
072                    message.put("command", "deploy");
073                    message.put("servletContextName", servletContextName);
074    
075                    MessageBusUtil.sendMessage(DestinationNames.HOT_DEPLOY, message);
076            }
077    
078            protected void doInvokeUndeploy(HotDeployEvent hotDeployEvent)
079                    throws Exception {
080    
081                    ServletContext servletContext = hotDeployEvent.getServletContext();
082    
083                    String servletContextName = servletContext.getServletContextName();
084    
085                    Message message = new Message();
086    
087                    message.put("command", "undeploy");
088                    message.put("servletContextName", servletContextName);
089    
090                    MessageBusUtil.sendMessage(DestinationNames.HOT_DEPLOY, message);
091            }
092    
093    }