001    /**
002     * Copyright (c) 2000-2011 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.log.Jdk14LogFactoryImpl;
033    import com.liferay.portal.kernel.log.Log;
034    import com.liferay.portal.kernel.log.LogFactoryUtil;
035    import com.liferay.portal.kernel.scheduler.SchedulerEngineUtil;
036    import com.liferay.portal.kernel.util.CentralizedThreadLocal;
037    import com.liferay.portal.kernel.util.GetterUtil;
038    import com.liferay.portal.kernel.util.PropsKeys;
039    import com.liferay.portal.search.lucene.LuceneHelperUtil;
040    import com.liferay.portal.util.PropsUtil;
041    import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
042    import com.liferay.util.ThirdPartyThreadLocalRegistry;
043    
044    import java.sql.Connection;
045    import java.sql.Statement;
046    
047    /**
048     * @author Brian Wing Shun Chan
049     */
050    public class GlobalShutdownAction extends SimpleAction {
051    
052            @Override
053            public void run(String[] ids) {
054    
055                    // Auto deploy
056    
057                    AutoDeployUtil.unregisterDir(AutoDeployDir.DEFAULT_NAME);
058    
059                    // Hot deploy
060    
061                    HotDeployUtil.unregisterListeners();
062    
063                    // Sandbox deploy
064    
065                    SandboxDeployUtil.unregisterDir(SandboxDeployDir.DEFAULT_NAME);
066    
067                    // Instant messenger AIM
068    
069                    try {
070                            if (_log.isDebugEnabled()) {
071                                    _log.debug("Shutting down AIM");
072                            }
073    
074                            AIMConnector.disconnect();
075                    }
076                    catch (Exception e) {
077                    }
078    
079                    // Instant messenger ICQ
080    
081                    try {
082                            if (_log.isDebugEnabled()) {
083                                    _log.debug("Shutting down ICQ");
084                            }
085    
086                            ICQConnector.disconnect();
087                    }
088                    catch (Exception e) {
089                    }
090    
091                    // Instant messenger MSN
092    
093                    try {
094                            if (_log.isDebugEnabled()) {
095                                    _log.debug("Shutting down MSN");
096                            }
097    
098                            MSNConnector.disconnect();
099                    }
100                    catch (Exception e) {
101                    }
102    
103                    // Instant messenger YM
104    
105                    try {
106                            if (_log.isDebugEnabled()) {
107                                    _log.debug("Shutting down YM");
108                            }
109    
110                            YMConnector.disconnect();
111                    }
112                    catch (Exception e) {
113                    }
114    
115                    // JCR
116    
117                    try {
118                            if (_log.isDebugEnabled()) {
119                                    _log.debug("Shutting down JCR");
120                            }
121    
122                            JCRFactoryUtil.shutdown();
123                    }
124                    catch (Exception e) {
125                    }
126    
127                    // Lucene
128    
129                    LuceneHelperUtil.shutdown();
130    
131                    // OpenOffice
132    
133                    DocumentConversionUtil.disconnect();
134    
135                    // Scheduler
136    
137                    try {
138                            SchedulerEngineUtil.shutdown();
139                    }
140                    catch (Exception e) {
141                    }
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                    // Wait 1 second so Quartz threads can cleanly shutdown
184    
185                    try {
186                            Thread.sleep(1000);
187                    }
188                    catch (Exception e) {
189                            e.printStackTrace();
190                    }
191    
192                    // Portal executors
193    
194                    PortalExecutorManagerUtil.shutdown(true);
195    
196                    // Programmatically exit
197    
198                    if (GetterUtil.getBoolean(
199                                    PropsUtil.get(PropsKeys.SHUTDOWN_PROGRAMMATICALLY_EXIT))) {
200    
201                            Thread currentThread = Thread.currentThread();
202    
203                            ThreadGroup threadGroup = getThreadGroup();
204    
205                            Thread[] threads = getThreads(threadGroup);
206    
207                            for (Thread thread : threads) {
208                                    if ((thread == null) || (thread == currentThread)) {
209                                            continue;
210                                    }
211    
212                                    try {
213                                            thread.interrupt();
214                                    }
215                                    catch (Exception e) {
216                                    }
217                            }
218    
219                            threadGroup.destroy();
220                    }
221            }
222    
223            protected ThreadGroup getThreadGroup() {
224                    Thread currentThread = Thread.currentThread();
225    
226                    ThreadGroup threadGroup = currentThread.getThreadGroup();
227    
228                    for (int i = 0; i < 10; i++) {
229                            if (threadGroup.getParent() == null) {
230                                    break;
231                            }
232                            else {
233                                    threadGroup = threadGroup.getParent();
234                            }
235                    }
236    
237                    return threadGroup;
238            }
239    
240            protected Thread[] getThreads(ThreadGroup threadGroup) {
241                    Thread[] threads = new Thread[threadGroup.activeCount() * 2];
242    
243                    threadGroup.enumerate(threads);
244    
245                    return threads;
246            }
247    
248            private static Log _log = LogFactoryUtil.getLog(GlobalShutdownAction.class);
249    
250    }