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                    logRegistration(servletContextName);
086    
087                    List<Object> objects = SocialConfigurationUtil.read(
088                            hotDeployEvent.getContextClassLoader(), xmls);
089    
090                    _vars.put(servletContextName, objects);
091    
092                    if (_log.isInfoEnabled()) {
093                            _log.info(
094                                    "Social for " + servletContextName + " is available for use");
095                    }
096            }
097    
098            protected void doInvokeUndeploy(HotDeployEvent hotDeployEvent)
099                    throws Exception {
100    
101                    ServletContext servletContext = hotDeployEvent.getServletContext();
102    
103                    String servletContextName = servletContext.getServletContextName();
104    
105                    if (_log.isDebugEnabled()) {
106                            _log.debug("Invoking undeploy for " + servletContextName);
107                    }
108    
109                    List<Object> objects = (List<Object>)_vars.get(servletContextName);
110    
111                    if (objects == null) {
112                            return;
113                    }
114    
115                    for (Object object : objects) {
116                            if (object instanceof SocialActivityDefinition) {
117                                    SocialActivityDefinition activityDefinition =
118                                            (SocialActivityDefinition)object;
119    
120                                    SocialConfigurationUtil.removeActivityDefinition(
121                                            activityDefinition);
122    
123                                    continue;
124                            }
125    
126                            Tuple tuple = (Tuple)object;
127    
128                            SocialActivityDefinition activityDefinition =
129                                    (SocialActivityDefinition)tuple.getObject(0);
130    
131                            Object tupleObject1 = tuple.getObject(1);
132    
133                            if (tupleObject1 instanceof SocialAchievement) {
134                                    List<SocialAchievement> achievements =
135                                            activityDefinition.getAchievements();
136    
137                                    achievements.remove(tupleObject1);
138                            }
139                            else if (tupleObject1 instanceof SocialActivityCounterDefinition) {
140                                    Collection<SocialActivityCounterDefinition>
141                                            activityCounterDefinitions =
142                                                    activityDefinition.getActivityCounterDefinitions();
143    
144                                    activityCounterDefinitions.remove(tupleObject1);
145                            }
146                    }
147    
148                    if (_log.isInfoEnabled()) {
149                            _log.info("Social for " + servletContextName + " was unregistered");
150                    }
151            }
152    
153            protected void logRegistration(String servletContextName) {
154                    if (_log.isInfoEnabled()) {
155                            _log.info("Registering social for " + servletContextName);
156                    }
157            }
158    
159            private static Log _log = LogFactoryUtil.getLog(
160                    SocialHotDeployListener.class);
161    
162            private static Map<String, Object> _vars = new HashMap<String, Object>();
163    
164    }