1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class GlobalShutdownAction extends SimpleAction {
55  
56      public void run(String[] ids) {
57  
58          // Hot deploy
59  
60          HotDeployUtil.unregisterListeners();
61  
62          // Instant messenger AIM
63  
64          try {
65              if (_log.isDebugEnabled()) {
66                  _log.debug("Shutting down AIM");
67              }
68  
69              AIMConnector.disconnect();
70          }
71          catch (Exception e) {
72          }
73  
74          // Instant messenger ICQ
75  
76          try {
77              if (_log.isDebugEnabled()) {
78                  _log.debug("Shutting down ICQ");
79              }
80  
81              ICQConnector.disconnect();
82          }
83          catch (Exception e) {
84          }
85  
86          // Instant messenger MSN
87  
88          try {
89              if (_log.isDebugEnabled()) {
90                  _log.debug("Shutting down MSN");
91              }
92  
93              MSNConnector.disconnect();
94          }
95          catch (Exception e) {
96          }
97  
98          // Instant messenger YM
99  
100         try {
101             if (_log.isDebugEnabled()) {
102                 _log.debug("Shutting down YM");
103             }
104 
105             YMConnector.disconnect();
106         }
107         catch (Exception e) {
108         }
109 
110         // JCR
111 
112         try {
113             if (_log.isDebugEnabled()) {
114                 _log.debug("Shutting down JCR");
115             }
116 
117             JCRFactoryUtil.shutdown();
118         }
119         catch (Exception e) {
120         }
121 
122         // OpenOffice
123 
124         DocumentConversionUtil.disconnect();
125 
126         // POP server
127 
128         if (PropsValues.POP_SERVER_NOTIFICATIONS_ENABLED) {
129             POPServerUtil.stop();
130         }
131 
132         // Scheduler
133 
134         try {
135             JobSchedulerUtil.shutdown();
136         }
137         catch (Exception e) {
138         }
139 
140         try {
141             SchedulerEngineUtil.shutdown();
142         }
143         catch (Exception e) {
144         }
145 
146         // Hypersonic
147 
148         DBUtil dbUtil = DBUtil.getInstance();
149 
150         if (dbUtil.getType().equals(DBUtil.TYPE_HYPERSONIC)) {
151             try {
152                 Connection connection = DataAccess.getConnection();
153 
154                 Statement statement = connection.createStatement();
155 
156                 statement.executeUpdate("SHUTDOWN");
157 
158                 statement.close();
159             }
160             catch (Exception e) {
161                 _log.error(e, e);
162             }
163         }
164 
165         // Reset log to default JDK 1.4 logger. This will allow WARs dependent
166         // on the portal to still log events after the portal WAR has been
167         // destroyed.
168 
169         try {
170             LogFactoryUtil.setLogFactory(new Jdk14LogFactoryImpl());
171         }
172         catch (Exception e) {
173         }
174 
175         // Programmatically exit
176 
177         if (GetterUtil.getBoolean(PropsUtil.get(
178                 PropsKeys.SHUTDOWN_PROGRAMMATICALLY_EXIT))) {
179 
180             Thread thread = Thread.currentThread();
181 
182             ThreadGroup threadGroup = thread.getThreadGroup();
183 
184             for (int i = 0; i < 10; i++) {
185                 if (threadGroup.getParent() == null) {
186                     break;
187                 }
188                 else {
189                     threadGroup = threadGroup.getParent();
190                 }
191             }
192 
193             //threadGroup.list();
194 
195             Thread[] threads = new Thread[threadGroup.activeCount() * 2];
196 
197             threadGroup.enumerate(threads);
198 
199             for (int i = 0; i < threads.length; i++) {
200                 Thread curThread = threads[i];
201 
202                 if ((curThread != null) && (curThread != thread)) {
203                     try {
204                         curThread.interrupt();
205                     }
206                     catch (Exception e) {
207                     }
208                 }
209             }
210 
211             threadGroup.destroy();
212         }
213     }
214 
215     private static Log _log = LogFactoryUtil.getLog(GlobalShutdownAction.class);
216 
217 }