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