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.LogFactoryUtil;
36  import com.liferay.portal.kernel.scheduler.SchedulerEngineUtil;
37  import com.liferay.portal.kernel.util.GetterUtil;
38  import com.liferay.portal.pop.POPServerUtil;
39  import com.liferay.portal.tools.sql.DBUtil;
40  import com.liferay.portal.util.PropsKeys;
41  import com.liferay.portal.util.PropsUtil;
42  import com.liferay.portal.util.PropsValues;
43  import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
44  
45  import java.sql.Connection;
46  import java.sql.Statement;
47  
48  import org.apache.commons.logging.Log;
49  import org.apache.commons.logging.LogFactory;
50  
51  /**
52   * <a href="GlobalShutdownAction.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class GlobalShutdownAction extends SimpleAction {
58  
59      public void run(String[] ids) {
60  
61          // Hot deploy
62  
63          HotDeployUtil.unregisterListeners();
64  
65          // Instant messenger AIM
66  
67          try {
68              if (_log.isDebugEnabled()) {
69                  _log.debug("Shutting down AIM");
70              }
71  
72              AIMConnector.disconnect();
73          }
74          catch (Exception e) {
75          }
76  
77          // Instant messenger ICQ
78  
79          try {
80              if (_log.isDebugEnabled()) {
81                  _log.debug("Shutting down ICQ");
82              }
83  
84              ICQConnector.disconnect();
85          }
86          catch (Exception e) {
87          }
88  
89          // Instant messenger MSN
90  
91          try {
92              if (_log.isDebugEnabled()) {
93                  _log.debug("Shutting down MSN");
94              }
95  
96              MSNConnector.disconnect();
97          }
98          catch (Exception e) {
99          }
100 
101         // Instant messenger YM
102 
103         try {
104             if (_log.isDebugEnabled()) {
105                 _log.debug("Shutting down YM");
106             }
107 
108             YMConnector.disconnect();
109         }
110         catch (Exception e) {
111         }
112 
113         // JCR
114 
115         try {
116             if (_log.isDebugEnabled()) {
117                 _log.debug("Shutting down JCR");
118             }
119 
120             JCRFactoryUtil.shutdown();
121         }
122         catch (Exception e) {
123         }
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         // Hypersonic
150 
151         DBUtil dbUtil = DBUtil.getInstance();
152 
153         if (dbUtil.getType().equals(DBUtil.TYPE_HYPERSONIC)) {
154             try {
155                 Connection connection = DataAccess.getConnection();
156 
157                 Statement statement = connection.createStatement();
158 
159                 statement.executeUpdate("SHUTDOWN");
160 
161                 statement.close();
162             }
163             catch (Exception e) {
164                 _log.error(e, e);
165             }
166         }
167 
168         // Reset log to default JDK 1.4 logger. This will allow WARs dependent
169         // on the portal to still log events after the portal WAR has been
170         // destroyed.
171 
172         try {
173             LogFactoryUtil.setLogFactory(new Jdk14LogFactoryImpl());
174         }
175         catch (Exception e) {
176         }
177 
178         // Programmatically exit
179 
180         if (GetterUtil.getBoolean(PropsUtil.get(
181                 PropsKeys.SHUTDOWN_PROGRAMMATICALLY_EXIT))) {
182 
183             Thread thread = Thread.currentThread();
184 
185             ThreadGroup threadGroup = thread.getThreadGroup();
186 
187             for (int i = 0; i < 10; i++) {
188                 if (threadGroup.getParent() == null) {
189                     break;
190                 }
191                 else {
192                     threadGroup = threadGroup.getParent();
193                 }
194             }
195 
196             //threadGroup.list();
197 
198             Thread[] threads = new Thread[threadGroup.activeCount() * 2];
199 
200             threadGroup.enumerate(threads);
201 
202             for (int i = 0; i < threads.length; i++) {
203                 Thread curThread = threads[i];
204 
205                 if ((curThread != null) && (curThread != thread)) {
206                     try {
207                         curThread.interrupt();
208                     }
209                     catch (Exception e) {
210                     }
211                 }
212             }
213 
214             threadGroup.destroy();
215         }
216     }
217 
218     private static Log _log = LogFactory.getLog(GlobalShutdownAction.class);
219 
220 }