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