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.job.JobScheduler;
31  import com.liferay.portal.kernel.events.ActionException;
32  import com.liferay.portal.kernel.events.SimpleAction;
33  import com.liferay.portal.kernel.log.Jdk14LogFactoryImpl;
34  import com.liferay.portal.kernel.log.LogFactoryUtil;
35  import com.liferay.portal.kernel.util.GetterUtil;
36  import com.liferay.portal.pop.POPServerUtil;
37  import com.liferay.portal.util.PropsUtil;
38  import com.liferay.portal.util.PropsValues;
39  
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
43  /**
44   * <a href="GlobalShutdownAction.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class GlobalShutdownAction extends SimpleAction {
50  
51      public void run(String[] ids) throws ActionException {
52  
53          // Disconnect AIM
54  
55          try {
56              _log.debug("Shutting down AIM");
57  
58              AIMConnector.disconnect();
59          }
60          catch (Exception e) {
61          }
62  
63          // Disconnect ICQ
64  
65          try {
66              _log.debug("Shutting down ICQ");
67  
68              ICQConnector.disconnect();
69          }
70          catch (Exception e) {
71          }
72  
73          // Disconnect MSN
74  
75          try {
76              _log.debug("Shutting down MSN");
77  
78              MSNConnector.disconnect();
79          }
80          catch (Exception e) {
81          }
82  
83          // Disconnect YM
84  
85          try {
86              _log.debug("Shutting down YM");
87  
88              YMConnector.disconnect();
89          }
90          catch (Exception e) {
91          }
92  
93          // POP server
94  
95          if (PropsValues.POP_SERVER_NOTIFICATIONS_ENABLED) {
96              POPServerUtil.stop();
97          }
98  
99          // Shutdown JCR
100 
101         try {
102             _log.debug("Shutting down JCR");
103 
104             JCRFactoryUtil.shutdown();
105         }
106         catch (Exception e) {
107         }
108 
109         // Scheduler
110 
111         try {
112             JobScheduler.shutdown();
113         }
114         catch (Exception e) {
115         }
116 
117         // Reset log to default JDK 1.4 logger. This will allow WARs dependent
118         // on the portal to still log events after the portal WAR has been
119         // destroyed.
120 
121         try {
122             LogFactoryUtil.setLogFactory(new Jdk14LogFactoryImpl());
123         }
124         catch (Exception e) {
125         }
126 
127         // Programmatically exit
128 
129         if (GetterUtil.getBoolean(PropsUtil.get(
130                 PropsUtil.SHUTDOWN_PROGRAMMATICALLY_EXIT))) {
131 
132             Thread thread = Thread.currentThread();
133 
134             ThreadGroup threadGroup = thread.getThreadGroup();
135 
136             for (int i = 0; i < 10; i++) {
137                 if (threadGroup.getParent() == null) {
138                     break;
139                 }
140                 else {
141                     threadGroup = threadGroup.getParent();
142                 }
143             }
144 
145             //threadGroup.list();
146 
147             Thread[] threads = new Thread[threadGroup.activeCount() * 2];
148 
149             threadGroup.enumerate(threads);
150 
151             for (int i = 0; i < threads.length; i++) {
152                 Thread curThread = threads[i];
153 
154                 if ((curThread != null) && (curThread != thread)) {
155                     try {
156                         curThread.interrupt();
157                     }
158                     catch (Exception e) {
159                     }
160                 }
161             }
162 
163             threadGroup.destroy();
164         }
165     }
166 
167     private static Log _log = LogFactory.getLog(GlobalShutdownAction.class);
168 
169 }