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