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