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