KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > jboss4 > ide > JBStopRunnable


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.jboss4.ide;
21
22 import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.enterprise.deploy.shared.ActionType JavaDoc;
26 import javax.enterprise.deploy.shared.CommandType JavaDoc;
27 import javax.enterprise.deploy.shared.StateType JavaDoc;
28 import org.netbeans.api.java.platform.JavaPlatform;
29 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
30 import org.netbeans.modules.j2ee.jboss4.JBDeploymentManager;
31 import org.netbeans.modules.j2ee.jboss4.ide.ui.JBPluginProperties;
32 import org.netbeans.modules.j2ee.jboss4.util.JBProperties;
33 import org.openide.ErrorManager;
34 import org.openide.execution.NbProcessDescriptor;
35 import org.openide.filesystems.FileObject;
36 import org.openide.filesystems.FileUtil;
37 import org.openide.util.NbBundle;
38 import org.openide.util.Utilities;
39
40 /**
41  *
42  * @author Kirill Sorokin
43  * @author Libor Kotouc
44  */

45 class JBStopRunnable implements Runnable JavaDoc {
46
47     public static final boolean VERBOSE =
48         System.getProperty ("netbeans.serverplugins.jboss4.logging") != null;
49     
50     private final static String JavaDoc SHUTDOWN_SH = "/bin/shutdown.sh";//NOI18N
51
private final static String JavaDoc SHUTDOWN_BAT = "/bin/shutdown.bat";//NOI18N
52

53     private static int TIMEOUT = 300000;
54     
55     private JBDeploymentManager dm;
56     private JBStartServer startServer;
57
58     JBStopRunnable(JBDeploymentManager dm, JBStartServer startServer) {
59         this.dm = dm;
60         this.startServer = startServer;
61     }
62     
63     private String JavaDoc[] createEnvironment() {
64         
65         JBProperties properties = dm.getProperties();
66
67         JavaPlatform platform = properties.getJavaPlatform();
68         FileObject fo = (FileObject) platform.getInstallFolders().iterator().next();
69         String JavaDoc javaHome = FileUtil.toFile(fo).getAbsolutePath();
70         List JavaDoc<String JavaDoc> envp = new ArrayList JavaDoc<String JavaDoc>(3);
71         envp.add("JAVA=" + javaHome + "/bin/java"); // NOI18N
72
envp.add("JAVA_HOME=" + javaHome); // NOI18N
73
if (Utilities.isWindows()) {
74             // the shutdown script should not wait for a key press
75
envp.add("NOPAUSE=true"); // NOI18N
76
}
77         return (String JavaDoc[]) envp.toArray(new String JavaDoc[envp.size()]);
78     }
79     
80     public void run() {
81
82         InstanceProperties ip = dm.getInstanceProperties();
83
84         String JavaDoc configName = ip.getProperty("server"); // NOI18N
85
if ("minimal".equals(configName)) { // NOI18N
86
startServer.fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.STOP, StateType.FAILED, NbBundle.getMessage(JBStopRunnable.class, "MSG_STOP_SERVER_FAILED_MINIMAL")));//NOI18N
87
return;
88         }
89
90         String JavaDoc serverName = ip.getProperty(InstanceProperties.DISPLAY_NAME_ATTR);
91
92         String JavaDoc serverLocation = ip.getProperty(JBPluginProperties.PROPERTY_ROOT_DIR);
93         String JavaDoc serverStopFileName = serverLocation + (Utilities.isWindows() ? SHUTDOWN_BAT : SHUTDOWN_SH);
94
95         File JavaDoc serverStopFile = new File JavaDoc(serverStopFileName);
96         if (!serverStopFile.exists()){
97             startServer.fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.STOP, StateType.FAILED, NbBundle.getMessage(JBStopRunnable.class, "MSG_STOP_SERVER_FAILED_FNF", serverName)));//NOI18N
98
return;
99         }
100         
101         JBProperties properties = dm.getProperties();
102         StringBuilder JavaDoc credentialsParams = new StringBuilder JavaDoc(32);
103         credentialsParams.append(" -u ").append(properties.getUsername()).append(" -p ").append(properties.getPassword()); // NOI18N
104
// Currently there is a problem stopping JBoss when Profiler agent is loaded.
105
// As a workaround for now, --halt parameter has to be used for stopping the server.
106
NbProcessDescriptor pd = (startServer.getMode() == JBStartServer.MODE.PROFILE ?
107                                   new NbProcessDescriptor(serverStopFileName, "--halt=0 " + credentialsParams) : // NOI18N
108
new NbProcessDescriptor(serverStopFileName, "--shutdown " + credentialsParams)); // NOI18N
109

110         Process JavaDoc stoppingProcess = null;
111         try {
112             String JavaDoc envp[] = createEnvironment();
113             stoppingProcess = pd.exec(null, envp, true, null);
114         } catch (java.io.IOException JavaDoc ioe) {
115             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
116
117             startServer.fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.STOP, StateType.FAILED,
118                     NbBundle.getMessage(JBStopRunnable.class, "MSG_STOP_SERVER_FAILED_PD", serverName, serverStopFileName)));//NOI18N
119

120             return;
121         }
122
123         startServer.fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.STOP, StateType.RUNNING,
124                 NbBundle.getMessage(JBStopRunnable.class, "MSG_STOP_SERVER_IN_PROGRESS", serverName)));
125
126         if (VERBOSE) {
127             System.out.println("JBStopRunnable: Entering the loop"); // NOI18N
128
}
129         
130         int elapsed = 0;
131         while (elapsed < TIMEOUT) {
132             // check whether the stopping process did not fail
133
try {
134                 int processExitValue = stoppingProcess.exitValue();
135                 if (VERBOSE) {
136                     System.out.println("JBStopRunnable: the stopping process has terminated with the exit value " + processExitValue); // NOI18N
137
}
138                 if (processExitValue != 0) {
139                     // stopping process failed
140
String JavaDoc msg = NbBundle.getMessage(JBStopRunnable.class, "MSG_STOP_SERVER_FAILED", serverName);
141                     startServer.fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.STOP, StateType.FAILED, msg));
142                     return;
143                 }
144             } catch (IllegalThreadStateException JavaDoc e) {
145                 // process is still running
146
}
147             if (startServer.isRunning()) {
148                 startServer.fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.STOP, StateType.RUNNING,
149                         NbBundle.getMessage(JBStopRunnable.class, "MSG_STOP_SERVER_IN_PROGRESS", serverName)));//NOI18N
150
if (VERBOSE) {
151                     System.out.println("JBStopRunnable: STOPPING message fired"); // NOI18N
152
}
153                 try {
154                     elapsed += 500;
155                     Thread.sleep(500);
156                 } catch (InterruptedException JavaDoc e) {}
157             } else {
158                 if (VERBOSE) {
159                     System.out.println("JBStopRunnable: JBoss has been stopped, going to stop the Log Writer thread");
160                 }
161                 final JBLogWriter logWriter = JBLogWriter.getInstance(ip.getProperty(InstanceProperties.DISPLAY_NAME_ATTR));
162                 if (logWriter != null && logWriter.isRunning()) {
163                     logWriter.waitForServerProcessFinished(10000);
164                     logWriter.stop();
165                 }
166
167                 startServer.fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.STOP, StateType.COMPLETED,
168                         NbBundle.getMessage(JBStopRunnable.class, "MSG_SERVER_STOPPED", serverName)));//NOI18N
169
if (VERBOSE) {
170                     System.out.println("JBStopRunnable: STOPPED message fired"); // NOI18N
171
}
172
173                 return;
174             }
175         }
176         
177         startServer.fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.STOP, StateType.FAILED,
178                 NbBundle.getMessage(JBStopRunnable.class, "MSG_StopServerTimeout")));
179         if (stoppingProcess != null) {
180             stoppingProcess.destroy();
181         }
182         
183         if (VERBOSE) {
184             System.out.println("JBStopRunnable: TIMEOUT EXPIRED"); // NOI18N
185
}
186     }
187 }
188     
189
Popular Tags