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