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