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