1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.events;
16  
17  import com.liferay.portal.im.AIMConnector;
18  import com.liferay.portal.im.ICQConnector;
19  import com.liferay.portal.im.MSNConnector;
20  import com.liferay.portal.im.YMConnector;
21  import com.liferay.portal.jcr.JCRFactoryUtil;
22  import com.liferay.portal.kernel.dao.db.DB;
23  import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
24  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
25  import com.liferay.portal.kernel.deploy.hot.HotDeployUtil;
26  import com.liferay.portal.kernel.events.SimpleAction;
27  import com.liferay.portal.kernel.log.Jdk14LogFactoryImpl;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.scheduler.SchedulerEngineUtil;
31  import com.liferay.portal.kernel.util.GetterUtil;
32  import com.liferay.portal.kernel.util.PropsKeys;
33  import com.liferay.portal.kernel.util.ReflectionUtil;
34  import com.liferay.portal.kernel.util.ThreadLocalRegistry;
35  import com.liferay.portal.pop.POPServerUtil;
36  import com.liferay.portal.search.lucene.LuceneHelperUtil;
37  import com.liferay.portal.util.PropsUtil;
38  import com.liferay.portal.util.PropsValues;
39  import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
40  import com.liferay.util.ThirdPartyThreadLocalRegistry;
41  
42  import java.sql.Connection;
43  import java.sql.Statement;
44  
45  import org.apache.axis.utils.XMLUtils;
46  
47  /**
48   * <a href="GlobalShutdownAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   */
52  public class GlobalShutdownAction extends SimpleAction {
53  
54      public void run(String[] ids) {
55  
56          // Hot deploy
57  
58          HotDeployUtil.unregisterListeners();
59  
60          // Instant messenger AIM
61  
62          try {
63              if (_log.isDebugEnabled()) {
64                  _log.debug("Shutting down AIM");
65              }
66  
67              AIMConnector.disconnect();
68          }
69          catch (Exception e) {
70          }
71  
72          // Instant messenger ICQ
73  
74          try {
75              if (_log.isDebugEnabled()) {
76                  _log.debug("Shutting down ICQ");
77              }
78  
79              ICQConnector.disconnect();
80          }
81          catch (Exception e) {
82          }
83  
84          // Instant messenger MSN
85  
86          try {
87              if (_log.isDebugEnabled()) {
88                  _log.debug("Shutting down MSN");
89              }
90  
91              MSNConnector.disconnect();
92          }
93          catch (Exception e) {
94          }
95  
96          // Instant messenger YM
97  
98          try {
99              if (_log.isDebugEnabled()) {
100                 _log.debug("Shutting down YM");
101             }
102 
103             YMConnector.disconnect();
104         }
105         catch (Exception e) {
106         }
107 
108         // JCR
109 
110         try {
111             if (_log.isDebugEnabled()) {
112                 _log.debug("Shutting down JCR");
113             }
114 
115             JCRFactoryUtil.shutdown();
116         }
117         catch (Exception e) {
118         }
119 
120         // Lucene
121 
122         LuceneHelperUtil.shutdown();
123 
124         // OpenOffice
125 
126         DocumentConversionUtil.disconnect();
127 
128         // POP server
129 
130         if (PropsValues.POP_SERVER_NOTIFICATIONS_ENABLED) {
131             POPServerUtil.stop();
132         }
133 
134         // Scheduler
135 
136         try {
137             SchedulerEngineUtil.shutdown();
138         }
139         catch (Exception e) {
140         }
141 
142         // Thread local registry
143 
144         ThirdPartyThreadLocalRegistry.resetThreadLocals();
145         ThreadLocalRegistry.resetThreadLocals();
146 
147         ThreadLocal<?> threadLocal =
148             (ThreadLocal<?>)ReflectionUtil.getFieldValue(
149                 XMLUtils.class, "documentBuilder");
150 
151         threadLocal.remove();
152 
153         // Hypersonic
154 
155         DB db = DBFactoryUtil.getDB();
156 
157         if (db.getType().equals(DB.TYPE_HYPERSONIC)) {
158             try {
159                 Connection connection = DataAccess.getConnection();
160 
161                 Statement statement = connection.createStatement();
162 
163                 statement.executeUpdate("SHUTDOWN");
164 
165                 statement.close();
166             }
167             catch (Exception e) {
168                 _log.error(e, e);
169             }
170         }
171 
172         // Reset log to default JDK 1.4 logger. This will allow WARs dependent
173         // on the portal to still log events after the portal WAR has been
174         // destroyed.
175 
176         try {
177             LogFactoryUtil.setLogFactory(new Jdk14LogFactoryImpl());
178         }
179         catch (Exception e) {
180         }
181 
182         // Wait 1 second so Quartz threads can cleanly shutdown
183 
184         try {
185             Thread.sleep(1000);
186         }
187         catch (Exception e) {
188             e.printStackTrace();
189         }
190 
191         // Programmatically exit
192 
193         if (GetterUtil.getBoolean(PropsUtil.get(
194                 PropsKeys.SHUTDOWN_PROGRAMMATICALLY_EXIT))) {
195 
196             Thread currentThread = Thread.currentThread();
197 
198             ThreadGroup threadGroup = currentThread.getThreadGroup();
199 
200             for (int i = 0; i < 10; i++) {
201                 if (threadGroup.getParent() == null) {
202                     break;
203                 }
204                 else {
205                     threadGroup = threadGroup.getParent();
206                 }
207             }
208 
209             Thread[] threads = new Thread[threadGroup.activeCount() * 2];
210 
211             threadGroup.enumerate(threads);
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     private static Log _log = LogFactoryUtil.getLog(GlobalShutdownAction.class);
230 
231 }