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