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