001    /**
002     * Copyright (c) 2000-present 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.events;
016    
017    import com.liferay.portal.deploy.RequiredPluginsUtil;
018    import com.liferay.portal.fabric.server.FabricServerUtil;
019    import com.liferay.portal.im.AIMConnector;
020    import com.liferay.portal.im.ICQConnector;
021    import com.liferay.portal.im.YMConnector;
022    import com.liferay.portal.kernel.dao.db.DB;
023    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
024    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
025    import com.liferay.portal.kernel.deploy.auto.AutoDeployDir;
026    import com.liferay.portal.kernel.deploy.auto.AutoDeployUtil;
027    import com.liferay.portal.kernel.deploy.hot.HotDeployUtil;
028    import com.liferay.portal.kernel.deploy.sandbox.SandboxDeployDir;
029    import com.liferay.portal.kernel.deploy.sandbox.SandboxDeployUtil;
030    import com.liferay.portal.kernel.events.SimpleAction;
031    import com.liferay.portal.kernel.executor.PortalExecutorManagerUtil;
032    import com.liferay.portal.kernel.javadoc.JavadocManagerUtil;
033    import com.liferay.portal.kernel.log.Jdk14LogFactoryImpl;
034    import com.liferay.portal.kernel.log.Log;
035    import com.liferay.portal.kernel.log.LogFactoryUtil;
036    import com.liferay.portal.kernel.messaging.MessageBusUtil;
037    import com.liferay.portal.kernel.resiliency.mpi.MPIHelperUtil;
038    import com.liferay.portal.kernel.util.CentralizedThreadLocal;
039    import com.liferay.portal.kernel.util.GetterUtil;
040    import com.liferay.portal.kernel.util.PropsKeys;
041    import com.liferay.portal.kernel.util.StringPool;
042    import com.liferay.portal.struts.AuthPublicPathRegistry;
043    import com.liferay.portal.util.PropsUtil;
044    import com.liferay.portal.util.PropsValues;
045    import com.liferay.portal.zip.TrueZIPHelperUtil;
046    import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
047    import com.liferay.util.ThirdPartyThreadLocalRegistry;
048    
049    import java.sql.Connection;
050    import java.sql.Statement;
051    
052    import java.util.concurrent.Future;
053    import java.util.concurrent.TimeUnit;
054    
055    /**
056     * @author Brian Wing Shun Chan
057     */
058    public class GlobalShutdownAction extends SimpleAction {
059    
060            @Override
061            public void run(String[] ids) {
062    
063                    // Lower shutdown levels have dependences on higher levels, therefore
064                    // lower ones need to shutdown before higher ones. Components within the
065                    // same shutdown level should not depend on each other.
066    
067                    shutdownLevel1();
068                    shutdownLevel2();
069                    shutdownLevel3();
070                    shutdownLevel4();
071                    shutdownLevel5();
072                    shutdownLevel6();
073                    shutdownLevel7();
074            }
075    
076            protected ThreadGroup getThreadGroup() {
077                    Thread currentThread = Thread.currentThread();
078    
079                    ThreadGroup threadGroup = currentThread.getThreadGroup();
080    
081                    for (int i = 0; i < 10; i++) {
082                            if (threadGroup.getParent() == null) {
083                                    break;
084                            }
085                            else {
086                                    threadGroup = threadGroup.getParent();
087                            }
088                    }
089    
090                    return threadGroup;
091            }
092    
093            protected Thread[] getThreads(ThreadGroup threadGroup) {
094                    Thread[] threads = new Thread[threadGroup.activeCount() * 2];
095    
096                    threadGroup.enumerate(threads);
097    
098                    return threads;
099            }
100    
101            protected void shutdownLevel1() {
102    
103                    // Authentication
104    
105                    AuthPublicPathRegistry.unregister(PropsValues.AUTH_PUBLIC_PATHS);
106    
107                    // Instant messenger AIM
108    
109                    try {
110                            if (_log.isDebugEnabled()) {
111                                    _log.debug("Shutting down AIM");
112                            }
113    
114                            AIMConnector.disconnect();
115                    }
116                    catch (Exception e) {
117                    }
118    
119                    // Instant messenger ICQ
120    
121                    try {
122                            if (_log.isDebugEnabled()) {
123                                    _log.debug("Shutting down ICQ");
124                            }
125    
126                            ICQConnector.disconnect();
127                    }
128                    catch (Exception e) {
129                    }
130    
131                    // Instant messenger YM
132    
133                    try {
134                            if (_log.isDebugEnabled()) {
135                                    _log.debug("Shutting down YM");
136                            }
137    
138                            YMConnector.disconnect();
139                    }
140                    catch (Exception e) {
141                    }
142    
143                    // Javadoc
144    
145                    JavadocManagerUtil.unload(StringPool.BLANK);
146    
147                    // OpenOffice
148    
149                    DocumentConversionUtil.disconnect();
150    
151                    // Plugins
152    
153                    RequiredPluginsUtil.stopCheckingRequiredPlugins();
154            }
155    
156            protected void shutdownLevel2() {
157    
158                    // Auto deploy
159    
160                    AutoDeployUtil.unregisterDir(AutoDeployDir.DEFAULT_NAME);
161    
162                    // Hot deploy
163    
164                    HotDeployUtil.unregisterListeners();
165    
166                    // Sandbox deploy
167    
168                    SandboxDeployUtil.unregisterDir(SandboxDeployDir.DEFAULT_NAME);
169            }
170    
171            protected void shutdownLevel3() {
172    
173                    // Messaging
174    
175                    MessageBusUtil.shutdown(true);
176    
177                    // Portal fabric
178    
179                    if (PropsValues.PORTAL_FABRIC_ENABLED) {
180                            try {
181                                    Future<?> future = FabricServerUtil.stop();
182    
183                                    future.get(
184                                            PropsValues.PORTAL_FABRIC_SHUTDOWN_TIMEOUT,
185                                            TimeUnit.MILLISECONDS);
186                            }
187                            catch (Exception e) {
188                                    _log.error("Unable to stop fabric server", e);
189                            }
190                    }
191            }
192    
193            protected void shutdownLevel4() {
194    
195                    // Hypersonic
196    
197                    DB db = DBFactoryUtil.getDB();
198    
199                    String dbType = db.getType();
200    
201                    if (dbType.equals(DB.TYPE_HYPERSONIC)) {
202                            Connection connection = null;
203                            Statement statement = null;
204    
205                            try {
206                                    connection = DataAccess.getConnection();
207    
208                                    statement = connection.createStatement();
209    
210                                    statement.executeUpdate("SHUTDOWN");
211                            }
212                            catch (Exception e) {
213                                    _log.error(e, e);
214                            }
215                            finally {
216                                    DataAccess.cleanUp(connection, statement);
217                            }
218                    }
219    
220                    // Portal Resiliency
221    
222                    MPIHelperUtil.shutdown();
223            }
224    
225            protected void shutdownLevel5() {
226    
227                    // Portal executors
228    
229                    PortalExecutorManagerUtil.shutdown(true);
230    
231                    // TrueZip
232    
233                    TrueZIPHelperUtil.shutdown();
234            }
235    
236            protected void shutdownLevel6() {
237    
238                    // Reset log to default JDK 1.4 logger. This will allow WARs dependent
239                    // on the portal to still log events after the portal WAR has been
240                    // destroyed.
241    
242                    try {
243                            LogFactoryUtil.setLogFactory(new Jdk14LogFactoryImpl());
244                    }
245                    catch (Exception e) {
246                    }
247    
248                    // Thread local registry
249    
250                    ThirdPartyThreadLocalRegistry.resetThreadLocals();
251                    CentralizedThreadLocal.clearShortLivedThreadLocals();
252            }
253    
254            protected void shutdownLevel7() {
255    
256                    // Programmatically exit
257    
258                    if (GetterUtil.getBoolean(
259                                    PropsUtil.get(PropsKeys.SHUTDOWN_PROGRAMMATICALLY_EXIT))) {
260    
261                            Thread currentThread = Thread.currentThread();
262    
263                            ThreadGroup threadGroup = getThreadGroup();
264    
265                            Thread[] threads = getThreads(threadGroup);
266    
267                            for (Thread thread : threads) {
268                                    if ((thread == null) || (thread == currentThread)) {
269                                            continue;
270                                    }
271    
272                                    try {
273                                            thread.interrupt();
274                                    }
275                                    catch (Exception e) {
276                                    }
277                            }
278    
279                            threadGroup.destroy();
280                    }
281            }
282    
283            private static final Log _log = LogFactoryUtil.getLog(
284                    GlobalShutdownAction.class);
285    
286    }