1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.job.JobSchedulerUtil;
28  import com.liferay.portal.kernel.log.Jdk14LogFactoryImpl;
29  import com.liferay.portal.kernel.log.Log;
30  import com.liferay.portal.kernel.log.LogFactoryUtil;
31  import com.liferay.portal.kernel.scheduler.SchedulerEngineUtil;
32  import com.liferay.portal.kernel.util.GetterUtil;
33  import com.liferay.portal.kernel.util.PropsKeys;
34  import com.liferay.portal.kernel.util.ReflectionUtil;
35  import com.liferay.portal.kernel.util.ThreadLocalRegistry;
36  import com.liferay.portal.pop.POPServerUtil;
37  import com.liferay.portal.search.lucene.LuceneHelperUtil;
38  import com.liferay.portal.util.PropsUtil;
39  import com.liferay.portal.util.PropsValues;
40  import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
41  import com.liferay.util.ThirdPartyThreadLocalRegistry;
42  
43  import java.sql.Connection;
44  import java.sql.Statement;
45  
46  import org.apache.axis.utils.XMLUtils;
47  
48  /**
49   * <a href="GlobalShutdownAction.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   */
53  public class GlobalShutdownAction extends SimpleAction {
54  
55      public void run(String[] ids) {
56  
57          // Hot deploy
58  
59          HotDeployUtil.unregisterListeners();
60  
61          // Instant messenger AIM
62  
63          try {
64              if (_log.isDebugEnabled()) {
65                  _log.debug("Shutting down AIM");
66              }
67  
68              AIMConnector.disconnect();
69          }
70          catch (Exception e) {
71          }
72  
73          // Instant messenger ICQ
74  
75          try {
76              if (_log.isDebugEnabled()) {
77                  _log.debug("Shutting down ICQ");
78              }
79  
80              ICQConnector.disconnect();
81          }
82          catch (Exception e) {
83          }
84  
85          // Instant messenger MSN
86  
87          try {
88              if (_log.isDebugEnabled()) {
89                  _log.debug("Shutting down MSN");
90              }
91  
92              MSNConnector.disconnect();
93          }
94          catch (Exception e) {
95          }
96  
97          // Instant messenger YM
98  
99          try {
100             if (_log.isDebugEnabled()) {
101                 _log.debug("Shutting down YM");
102             }
103 
104             YMConnector.disconnect();
105         }
106         catch (Exception e) {
107         }
108 
109         // JCR
110 
111         try {
112             if (_log.isDebugEnabled()) {
113                 _log.debug("Shutting down JCR");
114             }
115 
116             JCRFactoryUtil.shutdown();
117         }
118         catch (Exception e) {
119         }
120 
121         // Lucene
122 
123         LuceneHelperUtil.shutdown();
124 
125         // OpenOffice
126 
127         DocumentConversionUtil.disconnect();
128 
129         // POP server
130 
131         if (PropsValues.POP_SERVER_NOTIFICATIONS_ENABLED) {
132             POPServerUtil.stop();
133         }
134 
135         // Scheduler
136 
137         try {
138             JobSchedulerUtil.shutdown();
139         }
140         catch (Exception e) {
141         }
142 
143         try {
144             SchedulerEngineUtil.shutdown();
145         }
146         catch (Exception e) {
147         }
148 
149         // Thread local registry
150 
151         ThirdPartyThreadLocalRegistry.resetThreadLocals();
152         ThreadLocalRegistry.resetThreadLocals();
153 
154         ThreadLocal<?> threadLocal =
155             (ThreadLocal<?>)ReflectionUtil.getFieldValue(
156                 XMLUtils.class, "documentBuilder");
157 
158         threadLocal.remove();
159 
160         // Hypersonic
161 
162         DB db = DBFactoryUtil.getDB();
163 
164         if (db.getType().equals(DB.TYPE_HYPERSONIC)) {
165             try {
166                 Connection connection = DataAccess.getConnection();
167 
168                 Statement statement = connection.createStatement();
169 
170                 statement.executeUpdate("SHUTDOWN");
171 
172                 statement.close();
173             }
174             catch (Exception e) {
175                 _log.error(e, e);
176             }
177         }
178 
179         // Reset log to default JDK 1.4 logger. This will allow WARs dependent
180         // on the portal to still log events after the portal WAR has been
181         // destroyed.
182 
183         try {
184             LogFactoryUtil.setLogFactory(new Jdk14LogFactoryImpl());
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         // Programmatically exit
199 
200         if (GetterUtil.getBoolean(PropsUtil.get(
201                 PropsKeys.SHUTDOWN_PROGRAMMATICALLY_EXIT))) {
202 
203             Thread currentThread = Thread.currentThread();
204 
205             ThreadGroup threadGroup = currentThread.getThreadGroup();
206 
207             for (int i = 0; i < 10; i++) {
208                 if (threadGroup.getParent() == null) {
209                     break;
210                 }
211                 else {
212                     threadGroup = threadGroup.getParent();
213                 }
214             }
215 
216             Thread[] threads = new Thread[threadGroup.activeCount() * 2];
217 
218             threadGroup.enumerate(threads);
219 
220             for (Thread thread : threads) {
221                 if ((thread == null) || (thread == currentThread)) {
222                     continue;
223                 }
224 
225                 try {
226                     thread.interrupt();
227                 }
228                 catch (Exception e) {
229                 }
230             }
231 
232             threadGroup.destroy();
233         }
234     }
235 
236     private static Log _log = LogFactoryUtil.getLog(GlobalShutdownAction.class);
237 
238 }