001    /**
002     * Copyright (c) 2000-2010 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.hot.HotDeployUtil;
026    import com.liferay.portal.kernel.events.SimpleAction;
027    import com.liferay.portal.kernel.log.Jdk14LogFactoryImpl;
028    import com.liferay.portal.kernel.log.Log;
029    import com.liferay.portal.kernel.log.LogFactoryUtil;
030    import com.liferay.portal.kernel.scheduler.SchedulerEngineUtil;
031    import com.liferay.portal.kernel.util.GetterUtil;
032    import com.liferay.portal.kernel.util.PropsKeys;
033    import com.liferay.portal.kernel.util.ThreadLocalRegistry;
034    import com.liferay.portal.pop.POPServerUtil;
035    import com.liferay.portal.search.lucene.LuceneHelperUtil;
036    import com.liferay.portal.util.PropsUtil;
037    import com.liferay.portal.util.PropsValues;
038    import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
039    import com.liferay.util.ThirdPartyThreadLocalRegistry;
040    
041    import java.sql.Connection;
042    import java.sql.Statement;
043    
044    /**
045     * @author Brian Wing Shun Chan
046     */
047    public class GlobalShutdownAction extends SimpleAction {
048    
049            public void run(String[] ids) {
050    
051                    // Hot deploy
052    
053                    HotDeployUtil.unregisterListeners();
054    
055                    // Instant messenger AIM
056    
057                    try {
058                            if (_log.isDebugEnabled()) {
059                                    _log.debug("Shutting down AIM");
060                            }
061    
062                            AIMConnector.disconnect();
063                    }
064                    catch (Exception e) {
065                    }
066    
067                    // Instant messenger ICQ
068    
069                    try {
070                            if (_log.isDebugEnabled()) {
071                                    _log.debug("Shutting down ICQ");
072                            }
073    
074                            ICQConnector.disconnect();
075                    }
076                    catch (Exception e) {
077                    }
078    
079                    // Instant messenger MSN
080    
081                    try {
082                            if (_log.isDebugEnabled()) {
083                                    _log.debug("Shutting down MSN");
084                            }
085    
086                            MSNConnector.disconnect();
087                    }
088                    catch (Exception e) {
089                    }
090    
091                    // Instant messenger YM
092    
093                    try {
094                            if (_log.isDebugEnabled()) {
095                                    _log.debug("Shutting down YM");
096                            }
097    
098                            YMConnector.disconnect();
099                    }
100                    catch (Exception e) {
101                    }
102    
103                    // JCR
104    
105                    try {
106                            if (_log.isDebugEnabled()) {
107                                    _log.debug("Shutting down JCR");
108                            }
109    
110                            JCRFactoryUtil.shutdown();
111                    }
112                    catch (Exception e) {
113                    }
114    
115                    // Lucene
116    
117                    LuceneHelperUtil.shutdown();
118    
119                    // OpenOffice
120    
121                    DocumentConversionUtil.disconnect();
122    
123                    // POP server
124    
125                    if (PropsValues.POP_SERVER_NOTIFICATIONS_ENABLED) {
126                            POPServerUtil.stop();
127                    }
128    
129                    // Scheduler
130    
131                    try {
132                            SchedulerEngineUtil.shutdown();
133                    }
134                    catch (Exception e) {
135                    }
136    
137                    // Thread local registry
138    
139                    ThirdPartyThreadLocalRegistry.resetThreadLocals();
140                    ThreadLocalRegistry.resetThreadLocals();
141    
142                    // Hypersonic
143    
144                    DB db = DBFactoryUtil.getDB();
145    
146                    if (db.getType().equals(DB.TYPE_HYPERSONIC)) {
147                            try {
148                                    Connection connection = DataAccess.getConnection();
149    
150                                    Statement statement = connection.createStatement();
151    
152                                    statement.executeUpdate("SHUTDOWN");
153    
154                                    statement.close();
155                            }
156                            catch (Exception e) {
157                                    _log.error(e, e);
158                            }
159                    }
160    
161                    // Reset log to default JDK 1.4 logger. This will allow WARs dependent
162                    // on the portal to still log events after the portal WAR has been
163                    // destroyed.
164    
165                    try {
166                            LogFactoryUtil.setLogFactory(new Jdk14LogFactoryImpl());
167                    }
168                    catch (Exception e) {
169                    }
170    
171                    // Wait 1 second so Quartz threads can cleanly shutdown
172    
173                    try {
174                            Thread.sleep(1000);
175                    }
176                    catch (Exception e) {
177                            e.printStackTrace();
178                    }
179    
180                    // Programmatically exit
181    
182                    if (GetterUtil.getBoolean(PropsUtil.get(
183                                    PropsKeys.SHUTDOWN_PROGRAMMATICALLY_EXIT))) {
184    
185                            Thread currentThread = Thread.currentThread();
186    
187                            ThreadGroup threadGroup = currentThread.getThreadGroup();
188    
189                            for (int i = 0; i < 10; i++) {
190                                    if (threadGroup.getParent() == null) {
191                                            break;
192                                    }
193                                    else {
194                                            threadGroup = threadGroup.getParent();
195                                    }
196                            }
197    
198                            Thread[] threads = new Thread[threadGroup.activeCount() * 2];
199    
200                            threadGroup.enumerate(threads);
201    
202                            for (Thread thread : threads) {
203                                    if ((thread == null) || (thread == currentThread)) {
204                                            continue;
205                                    }
206    
207                                    try {
208                                            thread.interrupt();
209                                    }
210                                    catch (Exception e) {
211                                    }
212                            }
213    
214                            threadGroup.destroy();
215                    }
216            }
217    
218            private static Log _log = LogFactoryUtil.getLog(GlobalShutdownAction.class);
219    
220    }