1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.events;
24  
25  import com.liferay.portal.im.AIMConnector;
26  import com.liferay.portal.im.ICQConnector;
27  import com.liferay.portal.im.MSNConnector;
28  import com.liferay.portal.im.YMConnector;
29  import com.liferay.portal.jcr.JCRFactoryUtil;
30  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
31  import com.liferay.portal.kernel.deploy.hot.HotDeployUtil;
32  import com.liferay.portal.kernel.events.SimpleAction;
33  import com.liferay.portal.kernel.job.JobSchedulerUtil;
34  import com.liferay.portal.kernel.log.Jdk14LogFactoryImpl;
35  import com.liferay.portal.kernel.log.Log;
36  import com.liferay.portal.kernel.log.LogFactoryUtil;
37  import com.liferay.portal.kernel.scheduler.SchedulerEngineUtil;
38  import com.liferay.portal.kernel.util.GetterUtil;
39  import com.liferay.portal.pop.POPServerUtil;
40  import com.liferay.portal.tools.sql.DBUtil;
41  import com.liferay.portal.util.PropsKeys;
42  import com.liferay.portal.util.PropsUtil;
43  import com.liferay.portal.util.PropsValues;
44  import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
45  
46  import java.sql.Connection;
47  import java.sql.Statement;
48  
49  /**
50   * <a href="GlobalShutdownAction.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   *
54   */
55  public class GlobalShutdownAction extends SimpleAction {
56  
57      public void run(String[] ids) {
58  
59          // Hot deploy
60  
61          HotDeployUtil.unregisterListeners();
62  
63          // Instant messenger AIM
64  
65          try {
66              if (_log.isDebugEnabled()) {
67                  _log.debug("Shutting down AIM");
68              }
69  
70              AIMConnector.disconnect();
71          }
72          catch (Exception e) {
73          }
74  
75          // Instant messenger ICQ
76  
77          try {
78              if (_log.isDebugEnabled()) {
79                  _log.debug("Shutting down ICQ");
80              }
81  
82              ICQConnector.disconnect();
83          }
84          catch (Exception e) {
85          }
86  
87          // Instant messenger MSN
88  
89          try {
90              if (_log.isDebugEnabled()) {
91                  _log.debug("Shutting down MSN");
92              }
93  
94              MSNConnector.disconnect();
95          }
96          catch (Exception e) {
97          }
98  
99          // Instant messenger YM
100 
101         try {
102             if (_log.isDebugEnabled()) {
103                 _log.debug("Shutting down YM");
104             }
105 
106             YMConnector.disconnect();
107         }
108         catch (Exception e) {
109         }
110 
111         // JCR
112 
113         try {
114             if (_log.isDebugEnabled()) {
115                 _log.debug("Shutting down JCR");
116             }
117 
118             JCRFactoryUtil.shutdown();
119         }
120         catch (Exception e) {
121         }
122 
123         // OpenOffice
124 
125         DocumentConversionUtil.disconnect();
126 
127         // POP server
128 
129         if (PropsValues.POP_SERVER_NOTIFICATIONS_ENABLED) {
130             POPServerUtil.stop();
131         }
132 
133         // Scheduler
134 
135         try {
136             JobSchedulerUtil.shutdown();
137         }
138         catch (Exception e) {
139         }
140 
141         try {
142             SchedulerEngineUtil.shutdown();
143         }
144         catch (Exception e) {
145         }
146 
147         // Hypersonic
148 
149         DBUtil dbUtil = DBUtil.getInstance();
150 
151         if (dbUtil.getType().equals(DBUtil.TYPE_HYPERSONIC)) {
152             try {
153                 Connection connection = DataAccess.getConnection();
154 
155                 Statement statement = connection.createStatement();
156 
157                 statement.executeUpdate("SHUTDOWN");
158 
159                 statement.close();
160             }
161             catch (Exception e) {
162                 _log.error(e, e);
163             }
164         }
165 
166         // Reset log to default JDK 1.4 logger. This will allow WARs dependent
167         // on the portal to still log events after the portal WAR has been
168         // destroyed.
169 
170         try {
171             LogFactoryUtil.setLogFactory(new Jdk14LogFactoryImpl());
172         }
173         catch (Exception e) {
174         }
175 
176         // Programmatically exit
177 
178         if (GetterUtil.getBoolean(PropsUtil.get(
179                 PropsKeys.SHUTDOWN_PROGRAMMATICALLY_EXIT))) {
180 
181             Thread thread = Thread.currentThread();
182 
183             ThreadGroup threadGroup = thread.getThreadGroup();
184 
185             for (int i = 0; i < 10; i++) {
186                 if (threadGroup.getParent() == null) {
187                     break;
188                 }
189                 else {
190                     threadGroup = threadGroup.getParent();
191                 }
192             }
193 
194             //threadGroup.list();
195 
196             Thread[] threads = new Thread[threadGroup.activeCount() * 2];
197 
198             threadGroup.enumerate(threads);
199 
200             for (int i = 0; i < threads.length; i++) {
201                 Thread curThread = threads[i];
202 
203                 if ((curThread != null) && (curThread != thread)) {
204                     try {
205                         curThread.interrupt();
206                     }
207                     catch (Exception e) {
208                     }
209                 }
210             }
211 
212             threadGroup.destroy();
213         }
214     }
215 
216     private static Log _log = LogFactoryUtil.getLog(GlobalShutdownAction.class);
217 
218 }