1   /**
2    * Copyright (c) 2000-2008 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.deploy.hot.HotDeployUtil;
31  import com.liferay.portal.kernel.events.SimpleAction;
32  import com.liferay.portal.kernel.job.JobSchedulerUtil;
33  import com.liferay.portal.kernel.log.Jdk14LogFactoryImpl;
34  import com.liferay.portal.kernel.log.LogFactoryUtil;
35  import com.liferay.portal.kernel.scheduler.SchedulerEngineUtil;
36  import com.liferay.portal.kernel.util.GetterUtil;
37  import com.liferay.portal.pop.POPServerUtil;
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 org.apache.commons.logging.Log;
44  import org.apache.commons.logging.LogFactory;
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              _log.debug("Shutting down AIM");
64  
65              AIMConnector.disconnect();
66          }
67          catch (Exception e) {
68          }
69  
70          // Instant messenger ICQ
71  
72          try {
73              _log.debug("Shutting down ICQ");
74  
75              ICQConnector.disconnect();
76          }
77          catch (Exception e) {
78          }
79  
80          // Instant messenger MSN
81  
82          try {
83              _log.debug("Shutting down MSN");
84  
85              MSNConnector.disconnect();
86          }
87          catch (Exception e) {
88          }
89  
90          // Instant messenger YM
91  
92          try {
93              _log.debug("Shutting down YM");
94  
95              YMConnector.disconnect();
96          }
97          catch (Exception e) {
98          }
99  
100         // JCR
101 
102         try {
103             _log.debug("Shutting down JCR");
104 
105             JCRFactoryUtil.shutdown();
106         }
107         catch (Exception e) {
108         }
109 
110         // OpenOffice
111 
112         DocumentConversionUtil.disconnect();
113 
114         // POP server
115 
116         if (PropsValues.POP_SERVER_NOTIFICATIONS_ENABLED) {
117             POPServerUtil.stop();
118         }
119 
120         // Scheduler
121 
122         try {
123             JobSchedulerUtil.shutdown();
124         }
125         catch (Exception e) {
126         }
127 
128         try {
129             SchedulerEngineUtil.shutdown();
130         }
131         catch (Exception e) {
132         }
133 
134         // Reset log to default JDK 1.4 logger. This will allow WARs dependent
135         // on the portal to still log events after the portal WAR has been
136         // destroyed.
137 
138         try {
139             LogFactoryUtil.setLogFactory(new Jdk14LogFactoryImpl());
140         }
141         catch (Exception e) {
142         }
143 
144         // Programmatically exit
145 
146         if (GetterUtil.getBoolean(PropsUtil.get(
147                 PropsKeys.SHUTDOWN_PROGRAMMATICALLY_EXIT))) {
148 
149             Thread thread = Thread.currentThread();
150 
151             ThreadGroup threadGroup = thread.getThreadGroup();
152 
153             for (int i = 0; i < 10; i++) {
154                 if (threadGroup.getParent() == null) {
155                     break;
156                 }
157                 else {
158                     threadGroup = threadGroup.getParent();
159                 }
160             }
161 
162             //threadGroup.list();
163 
164             Thread[] threads = new Thread[threadGroup.activeCount() * 2];
165 
166             threadGroup.enumerate(threads);
167 
168             for (int i = 0; i < threads.length; i++) {
169                 Thread curThread = threads[i];
170 
171                 if ((curThread != null) && (curThread != thread)) {
172                     try {
173                         curThread.interrupt();
174                     }
175                     catch (Exception e) {
176                     }
177                 }
178             }
179 
180             threadGroup.destroy();
181         }
182     }
183 
184     private static Log _log = LogFactory.getLog(GlobalShutdownAction.class);
185 
186 }