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