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.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.HttpUtil;
023    import com.liferay.portal.kernel.util.Tuple;
024    import com.liferay.portlet.social.model.SocialAchievement;
025    import com.liferay.portlet.social.model.SocialActivityCounterDefinition;
026    import com.liferay.portlet.social.model.SocialActivityDefinition;
027    import com.liferay.portlet.social.util.SocialConfigurationUtil;
028    
029    import java.util.Collection;
030    import java.util.HashMap;
031    import java.util.List;
032    import java.util.Map;
033    
034    import javax.servlet.ServletContext;
035    
036    /**
037     * @author Zsolt Berentey
038     */
039    public class SocialHotDeployListener extends BaseHotDeployListener {
040    
041            @Override
042            public void invokeDeploy(HotDeployEvent hotDeployEvent)
043                    throws HotDeployException {
044    
045                    try {
046                            doInvokeDeploy(hotDeployEvent);
047                    }
048                    catch (Throwable t) {
049                            throwHotDeployException(
050                                    hotDeployEvent, "Error registering social for ", t);
051                    }
052            }
053    
054            @Override
055            public void invokeUndeploy(HotDeployEvent hotDeployEvent)
056                    throws HotDeployException {
057    
058                    try {
059                            doInvokeUndeploy(hotDeployEvent);
060                    }
061                    catch (Throwable t) {
062                            throwHotDeployException(
063                                    hotDeployEvent, "Error unregistering social for ", t);
064                    }
065            }
066    
067            protected void doInvokeDeploy(HotDeployEvent hotDeployEvent)
068                    throws Exception {
069    
070                    ServletContext servletContext = hotDeployEvent.getServletContext();
071    
072                    String servletContextName = servletContext.getServletContextName();
073    
074                    if (_log.isDebugEnabled()) {
075                            _log.debug("Invoking deploy for " + servletContextName);
076                    }
077    
078                    String[] xmls = new String[] {
079                            HttpUtil.URLtoString(
080                                    servletContext.getResource("/WEB-INF/liferay-social.xml"))
081                    };
082    
083                    if (xmls[0] == null) {
084                            return;
085                    }
086    
087                    if (_log.isInfoEnabled()) {
088                            _log.info("Registering social for " + servletContextName);
089                    }
090    
091                    List<Object> objects = SocialConfigurationUtil.read(
092                            hotDeployEvent.getContextClassLoader(), xmls);
093    
094                    _objects.put(servletContextName, objects);
095    
096                    if (_log.isInfoEnabled()) {
097                            _log.info(
098                                    "Social for " + servletContextName + " is available for use");
099                    }
100            }
101    
102            protected void doInvokeUndeploy(HotDeployEvent hotDeployEvent)
103                    throws Exception {
104    
105                    ServletContext servletContext = hotDeployEvent.getServletContext();
106    
107                    String servletContextName = servletContext.getServletContextName();
108    
109                    if (_log.isDebugEnabled()) {
110                            _log.debug("Invoking undeploy for " + servletContextName);
111                    }
112    
113                    List<Object> objects = (List<Object>)_objects.get(servletContextName);
114    
115                    if (objects == null) {
116                            return;
117                    }
118    
119                    for (Object object : objects) {
120                            if (object instanceof SocialActivityDefinition) {
121                                    SocialActivityDefinition activityDefinition =
122                                            (SocialActivityDefinition)object;
123    
124                                    SocialConfigurationUtil.removeActivityDefinition(
125                                            activityDefinition);
126    
127                                    continue;
128                            }
129    
130                            Tuple tuple = (Tuple)object;
131    
132                            SocialActivityDefinition activityDefinition =
133                                    (SocialActivityDefinition)tuple.getObject(0);
134    
135                            Object tupleObject1 = tuple.getObject(1);
136    
137                            if (tupleObject1 instanceof SocialAchievement) {
138                                    List<SocialAchievement> achievements =
139                                            activityDefinition.getAchievements();
140    
141                                    achievements.remove(tupleObject1);
142                            }
143                            else if (tupleObject1 instanceof SocialActivityCounterDefinition) {
144                                    Collection<SocialActivityCounterDefinition>
145                                            activityCounterDefinitions =
146                                                    activityDefinition.getActivityCounterDefinitions();
147    
148                                    activityCounterDefinitions.remove(tupleObject1);
149                            }
150                    }
151    
152                    if (_log.isInfoEnabled()) {
153                            _log.info("Social for " + servletContextName + " was unregistered");
154                    }
155            }
156    
157            private static Log _log = LogFactoryUtil.getLog(
158                    SocialHotDeployListener.class);
159    
160            private static Map<String, Object> _objects = new HashMap<String, Object>();
161    
162    }