001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.j2ee.jboss4.ide;
043:
044: import java.io.File;
045: import java.util.ArrayList;
046: import java.util.List;
047: import java.util.logging.Level;
048: import java.util.logging.Logger;
049: import javax.enterprise.deploy.shared.ActionType;
050: import javax.enterprise.deploy.shared.CommandType;
051: import javax.enterprise.deploy.shared.StateType;
052: import org.netbeans.api.java.platform.JavaPlatform;
053: import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
054: import org.netbeans.modules.j2ee.jboss4.JBDeploymentManager;
055: import org.netbeans.modules.j2ee.jboss4.ide.ui.JBPluginProperties;
056: import org.netbeans.modules.j2ee.jboss4.util.JBProperties;
057: import org.openide.execution.NbProcessDescriptor;
058: import org.openide.filesystems.FileObject;
059: import org.openide.filesystems.FileUtil;
060: import org.openide.util.NbBundle;
061: import org.openide.util.Utilities;
062:
063: /**
064: *
065: * @author Kirill Sorokin
066: * @author Libor Kotouc
067: */
068: class JBStopRunnable implements Runnable {
069:
070: private static final Logger LOGGER = Logger
071: .getLogger(JBStopRunnable.class.getName());
072:
073: private static final String SHUTDOWN_SH = "/bin/shutdown.sh"; // NOI18N
074: private static final String SHUTDOWN_BAT = "/bin/shutdown.bat"; // NOI18N
075:
076: private static final int TIMEOUT = 300000;
077:
078: private final JBDeploymentManager dm;
079: private final JBStartServer startServer;
080:
081: JBStopRunnable(JBDeploymentManager dm, JBStartServer startServer) {
082: this .dm = dm;
083: this .startServer = startServer;
084: }
085:
086: private String[] createEnvironment() {
087:
088: JBProperties properties = dm.getProperties();
089:
090: JavaPlatform platform = properties.getJavaPlatform();
091: FileObject fo = (FileObject) platform.getInstallFolders()
092: .iterator().next();
093: String javaHome = FileUtil.toFile(fo).getAbsolutePath();
094: List<String> envp = new ArrayList<String>(3);
095: envp.add("JAVA=" + javaHome + "/bin/java"); // NOI18N
096: envp.add("JAVA_HOME=" + javaHome); // NOI18N
097: if (Utilities.isWindows()) {
098: // the shutdown script should not wait for a key press
099: envp.add("NOPAUSE=true"); // NOI18N
100: }
101: return (String[]) envp.toArray(new String[envp.size()]);
102: }
103:
104: public void run() {
105:
106: InstanceProperties ip = dm.getInstanceProperties();
107:
108: String configName = ip.getProperty("server"); // NOI18N
109: if ("minimal".equals(configName)) { // NOI18N
110: startServer.fireHandleProgressEvent(null,
111: new JBDeploymentStatus(ActionType.EXECUTE,
112: CommandType.STOP, StateType.FAILED,
113: NbBundle.getMessage(JBStopRunnable.class,
114: "MSG_STOP_SERVER_FAILED_MINIMAL")));//NOI18N
115: return;
116: }
117:
118: String serverName = ip
119: .getProperty(InstanceProperties.DISPLAY_NAME_ATTR);
120:
121: String serverLocation = ip
122: .getProperty(JBPluginProperties.PROPERTY_ROOT_DIR);
123: String serverStopFileName = serverLocation
124: + (Utilities.isWindows() ? SHUTDOWN_BAT : SHUTDOWN_SH);
125:
126: File serverStopFile = new File(serverStopFileName);
127: if (!serverStopFile.exists()) {
128: startServer.fireHandleProgressEvent(null,
129: new JBDeploymentStatus(ActionType.EXECUTE,
130: CommandType.STOP, StateType.FAILED,
131: NbBundle.getMessage(JBStopRunnable.class,
132: "MSG_STOP_SERVER_FAILED_FNF",
133: serverName)));//NOI18N
134: return;
135: }
136:
137: JBProperties properties = dm.getProperties();
138: StringBuilder credentialsParams = new StringBuilder(32);
139: credentialsParams.append(" -u ").append(
140: properties.getUsername()).append(" -p ").append(
141: properties.getPassword()); // NOI18N
142: // Currently there is a problem stopping JBoss when Profiler agent is loaded.
143: // As a workaround for now, --halt parameter has to be used for stopping the server.
144: NbProcessDescriptor pd = (startServer.getMode() == JBStartServer.MODE.PROFILE ? new NbProcessDescriptor(
145: serverStopFileName, "--halt=0 " + credentialsParams)
146: : // NOI18N
147: new NbProcessDescriptor(serverStopFileName,
148: "--shutdown " + credentialsParams)); // NOI18N
149:
150: Process stoppingProcess = null;
151: try {
152: String envp[] = createEnvironment();
153: stoppingProcess = pd.exec(null, envp, true, null);
154: } catch (java.io.IOException ioe) {
155: LOGGER.log(Level.INFO, null, ioe);
156:
157: startServer.fireHandleProgressEvent(null,
158: new JBDeploymentStatus(ActionType.EXECUTE,
159: CommandType.STOP, StateType.FAILED,
160: NbBundle.getMessage(JBStopRunnable.class,
161: "MSG_STOP_SERVER_FAILED_PD",
162: serverName, serverStopFileName)));//NOI18N
163:
164: return;
165: }
166:
167: startServer.fireHandleProgressEvent(null,
168: new JBDeploymentStatus(ActionType.EXECUTE,
169: CommandType.STOP, StateType.RUNNING, NbBundle
170: .getMessage(JBStopRunnable.class,
171: "MSG_STOP_SERVER_IN_PROGRESS",
172: serverName)));
173:
174: LOGGER.log(Level.FINER, "Entering the loop"); // NOI18N
175:
176: int elapsed = 0;
177: while (elapsed < TIMEOUT) {
178: // check whether the stopping process did not fail
179: try {
180: int processExitValue = stoppingProcess.exitValue();
181: if (LOGGER.isLoggable(Level.FINER)) {
182: LOGGER.log(Level.FINER,
183: "The stopping process has terminated with the exit value "
184: + processExitValue); // NOI18N
185: }
186: if (processExitValue != 0) {
187: // stopping process failed
188: String msg = NbBundle.getMessage(
189: JBStopRunnable.class,
190: "MSG_STOP_SERVER_FAILED", serverName);
191: startServer.fireHandleProgressEvent(null,
192: new JBDeploymentStatus(ActionType.EXECUTE,
193: CommandType.STOP, StateType.FAILED,
194: msg));
195: return;
196: }
197: } catch (IllegalThreadStateException e) {
198: // process is still running
199: }
200: if (startServer.isRunning()) {
201: startServer.fireHandleProgressEvent(null,
202: new JBDeploymentStatus(ActionType.EXECUTE,
203: CommandType.STOP, StateType.RUNNING,
204: NbBundle.getMessage(
205: JBStopRunnable.class,
206: "MSG_STOP_SERVER_IN_PROGRESS",
207: serverName)));//NOI18N
208: LOGGER.log(Level.FINER, "STOPPING message fired"); // NOI18N
209: try {
210: elapsed += 500;
211: Thread.sleep(500);
212: } catch (InterruptedException e) {
213: }
214: } else {
215: LOGGER
216: .log(Level.FINER,
217: "JBoss has been stopped, going to stop the Log Writer thread");
218: final JBLogWriter logWriter = JBLogWriter
219: .getInstance(ip
220: .getProperty(InstanceProperties.DISPLAY_NAME_ATTR));
221: if (logWriter != null && logWriter.isRunning()) {
222: logWriter.waitForServerProcessFinished(10000);
223: logWriter.stop();
224: }
225:
226: startServer.fireHandleProgressEvent(null,
227: new JBDeploymentStatus(ActionType.EXECUTE,
228: CommandType.STOP, StateType.COMPLETED,
229: NbBundle.getMessage(
230: JBStopRunnable.class,
231: "MSG_SERVER_STOPPED",
232: serverName)));//NOI18N
233: LOGGER.log(Level.FINER, "STOPPED message fired"); // NOI18N
234:
235: return;
236: }
237: }
238:
239: startServer.fireHandleProgressEvent(null,
240: new JBDeploymentStatus(ActionType.EXECUTE,
241: CommandType.STOP, StateType.FAILED, NbBundle
242: .getMessage(JBStopRunnable.class,
243: "MSG_StopServerTimeout")));
244: if (stoppingProcess != null) {
245: stoppingProcess.destroy();
246: }
247:
248: LOGGER.log(Level.FINER, "TIMEOUT expired"); // NOI18N
249: }
250: }
|