001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.events;
016    
017    import com.liferay.portal.kernel.events.SessionAction;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.notifications.ChannelException;
021    import com.liferay.portal.kernel.notifications.ChannelHubManagerUtil;
022    import com.liferay.portal.kernel.util.WebKeys;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.service.UserLocalServiceUtil;
025    
026    import javax.servlet.http.HttpSession;
027    
028    /**
029     * @author Edward Han
030     * @author Brian Wing Shun Chan
031     * @author Shuyang Zhou
032     */
033    public class ChannelSessionDestroyAction extends SessionAction {
034    
035            @Override
036            public void run(HttpSession session) {
037                    try {
038                            User user = (User)session.getAttribute(WebKeys.USER);
039    
040                            if (user == null) {
041                                    Long userId = (Long)session.getAttribute(WebKeys.USER_ID);
042    
043                                    if (userId != null) {
044                                            user = UserLocalServiceUtil.getUser(userId);
045                                    }
046                            }
047    
048                            if ((user == null) || user.isDefaultUser()) {
049                                    return;
050                            }
051    
052                            if (_log.isDebugEnabled()) {
053                                    _log.debug("Destroying channel " + user.getUserId());
054                            }
055    
056                            try {
057                                    ChannelHubManagerUtil.destroyChannel(
058                                            user.getCompanyId(), user.getUserId());
059                            }
060                            catch (ChannelException ce) {
061                                    if (_log.isDebugEnabled()) {
062                                            _log.debug(
063                                                    "User channel " + user.getUserId() +
064                                                            " is already unregistered",
065                                                    ce);
066                                    }
067                            }
068                    }
069                    catch (Exception e) {
070                            _log.error(e, e);
071                    }
072            }
073    
074            private static Log _log = LogFactoryUtil.getLog(
075                    ChannelSessionDestroyAction.class);
076    
077    }