001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            @SuppressWarnings("deprecation")
054            public void run(String[] ids) {
055    
056                    // Auto deploy
057    
058                    AutoDeployUtil.unregisterDir(AutoDeployDir.DEFAULT_NAME);
059    
060                    // Hot deploy
061    
062                    HotDeployUtil.unregisterListeners();
063    
064                    // Sandbox deploy
065    
066                    SandboxDeployUtil.unregisterDir(SandboxDeployDir.DEFAULT_NAME);
067    
068                    // Instant messenger AIM
069    
070                    try {
071                            if (_log.isDebugEnabled()) {
072                                    _log.debug("Shutting down AIM");
073                            }
074    
075                            AIMConnector.disconnect();
076                    }
077                    catch (Exception e) {
078                    }
079    
080                    // Instant messenger ICQ
081    
082                    try {
083                            if (_log.isDebugEnabled()) {
084                                    _log.debug("Shutting down ICQ");
085                            }
086    
087                            ICQConnector.disconnect();
088                    }
089                    catch (Exception e) {
090                    }
091    
092                    // Instant messenger MSN
093    
094                    try {
095                            if (_log.isDebugEnabled()) {
096                                    _log.debug("Shutting down MSN");
097                            }
098    
099                            MSNConnector.disconnect();
100                    }
101                    catch (Exception e) {
102                    }
103    
104                    // Instant messenger YM
105    
106                    try {
107                            if (_log.isDebugEnabled()) {
108                                    _log.debug("Shutting down YM");
109                            }
110    
111                            YMConnector.disconnect();
112                    }
113                    catch (Exception e) {
114                    }
115    
116                    // JCR
117    
118                    try {
119                            if (_log.isDebugEnabled()) {
120                                    _log.debug("Shutting down JCR");
121                            }
122    
123                            JCRFactoryUtil.shutdown();
124                    }
125                    catch (Exception e) {
126                    }
127    
128                    // Lucene
129    
130                    LuceneHelperUtil.shutdown();
131    
132                    // OpenOffice
133    
134                    DocumentConversionUtil.disconnect();
135    
136                    // Scheduler
137    
138                    try {
139                            SchedulerEngineUtil.shutdown();
140                    }
141                    catch (Exception e) {
142                    }
143    
144                    // Thread local registry
145    
146                    ThirdPartyThreadLocalRegistry.resetThreadLocals();
147                    CentralizedThreadLocal.clearShortLivedThreadLocals();
148    
149                    // Hypersonic
150    
151                    DB db = DBFactoryUtil.getDB();
152    
153                    String dbType = db.getType();
154    
155                    if (dbType.equals(DB.TYPE_HYPERSONIC)) {
156                            Connection connection = null;
157                            Statement statement = null;
158    
159                            try {
160                                    connection = DataAccess.getConnection();
161    
162                                    statement = connection.createStatement();
163    
164                                    statement.executeUpdate("SHUTDOWN");
165                            }
166                            catch (Exception e) {
167                                    _log.error(e, e);
168                            }
169                            finally {
170                                    DataAccess.cleanUp(connection, statement);
171                            }
172                    }
173    
174                    // Reset log to default JDK 1.4 logger. This will allow WARs dependent
175                    // on the portal to still log events after the portal WAR has been
176                    // destroyed.
177    
178                    try {
179                            LogFactoryUtil.setLogFactory(new Jdk14LogFactoryImpl());
180                    }
181                    catch (Exception e) {
182                    }
183    
184                    // Wait 1 second so Quartz threads can cleanly shutdown
185    
186                    try {
187                            Thread.sleep(1000);
188                    }
189                    catch (Exception e) {
190                            e.printStackTrace();
191                    }
192    
193                    // Portal executors
194    
195                    PortalExecutorManagerUtil.shutdown(true);
196    
197                    // Programmatically exit
198    
199                    if (GetterUtil.getBoolean(
200                                    PropsUtil.get(PropsKeys.SHUTDOWN_PROGRAMMATICALLY_EXIT))) {
201    
202                            Thread currentThread = Thread.currentThread();
203    
204                            ThreadGroup threadGroup = getThreadGroup();
205    
206                            Thread[] threads = getThreads(threadGroup);
207    
208                            for (Thread thread : threads) {
209                                    if ((thread == null) || (thread == currentThread)) {
210                                            continue;
211                                    }
212    
213                                    try {
214                                            thread.interrupt();
215                                    }
216                                    catch (Exception e) {
217                                    }
218                            }
219    
220                            threadGroup.destroy();
221                    }
222    
223                    ThreadGroup threadGroup = getThreadGroup();
224    
225                    Thread[] threads = getThreads(threadGroup);
226    
227                    for (Thread thread : threads) {
228                            String name = thread.getName();
229    
230                            // LPS-22392 or LPS-23031
231    
232                            if (name.equals("AWT-Windows") ||
233                                    name.equals("com.google.inject.internal.Finalizer")) {
234    
235                                    try {
236                                            thread.stop();
237                                    }
238                                    catch (Exception e) {
239                                    }
240                            }
241                    }
242            }
243    
244            protected ThreadGroup getThreadGroup() {
245                    Thread currentThread = Thread.currentThread();
246    
247                    ThreadGroup threadGroup = currentThread.getThreadGroup();
248    
249                    for (int i = 0; i < 10; i++) {
250                            if (threadGroup.getParent() == null) {
251                                    break;
252                            }
253                            else {
254                                    threadGroup = threadGroup.getParent();
255                            }
256                    }
257    
258                    return threadGroup;
259            }
260    
261            protected Thread[] getThreads(ThreadGroup threadGroup) {
262                    Thread[] threads = new Thread[threadGroup.activeCount() * 2];
263    
264                    threadGroup.enumerate(threads);
265    
266                    return threads;
267            }
268    
269            private static Log _log = LogFactoryUtil.getLog(GlobalShutdownAction.class);
270    
271    }