1 19 20 package org.netbeans.modules.j2ee.jboss4.ide; 21 22 import java.io.File ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 import javax.enterprise.deploy.shared.ActionType ; 26 import javax.enterprise.deploy.shared.CommandType ; 27 import javax.enterprise.deploy.shared.StateType ; 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 45 class JBStopRunnable implements Runnable { 46 47 public static final boolean VERBOSE = 48 System.getProperty ("netbeans.serverplugins.jboss4.logging") != null; 49 50 private final static String SHUTDOWN_SH = "/bin/shutdown.sh"; private final static String SHUTDOWN_BAT = "/bin/shutdown.bat"; 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 [] createEnvironment() { 64 65 JBProperties properties = dm.getProperties(); 66 67 JavaPlatform platform = properties.getJavaPlatform(); 68 FileObject fo = (FileObject) platform.getInstallFolders().iterator().next(); 69 String javaHome = FileUtil.toFile(fo).getAbsolutePath(); 70 List <String > envp = new ArrayList <String >(3); 71 envp.add("JAVA=" + javaHome + "/bin/java"); envp.add("JAVA_HOME=" + javaHome); if (Utilities.isWindows()) { 74 envp.add("NOPAUSE=true"); } 77 return (String []) envp.toArray(new String [envp.size()]); 78 } 79 80 public void run() { 81 82 InstanceProperties ip = dm.getInstanceProperties(); 83 84 String configName = ip.getProperty("server"); if ("minimal".equals(configName)) { startServer.fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.STOP, StateType.FAILED, NbBundle.getMessage(JBStopRunnable.class, "MSG_STOP_SERVER_FAILED_MINIMAL"))); return; 88 } 89 90 String serverName = ip.getProperty(InstanceProperties.DISPLAY_NAME_ATTR); 91 92 String serverLocation = ip.getProperty(JBPluginProperties.PROPERTY_ROOT_DIR); 93 String serverStopFileName = serverLocation + (Utilities.isWindows() ? SHUTDOWN_BAT : SHUTDOWN_SH); 94 95 File serverStopFile = new File (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))); return; 99 } 100 101 JBProperties properties = dm.getProperties(); 102 StringBuilder credentialsParams = new StringBuilder (32); 103 credentialsParams.append(" -u ").append(properties.getUsername()).append(" -p ").append(properties.getPassword()); NbProcessDescriptor pd = (startServer.getMode() == JBStartServer.MODE.PROFILE ? 107 new NbProcessDescriptor(serverStopFileName, "--halt=0 " + credentialsParams) : new NbProcessDescriptor(serverStopFileName, "--shutdown " + credentialsParams)); 110 Process stoppingProcess = null; 111 try { 112 String envp[] = createEnvironment(); 113 stoppingProcess = pd.exec(null, envp, true, null); 114 } catch (java.io.IOException 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))); 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"); } 129 130 int elapsed = 0; 131 while (elapsed < TIMEOUT) { 132 try { 134 int processExitValue = stoppingProcess.exitValue(); 135 if (VERBOSE) { 136 System.out.println("JBStopRunnable: the stopping process has terminated with the exit value " + processExitValue); } 138 if (processExitValue != 0) { 139 String 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 e) { 145 } 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))); if (VERBOSE) { 151 System.out.println("JBStopRunnable: STOPPING message fired"); } 153 try { 154 elapsed += 500; 155 Thread.sleep(500); 156 } catch (InterruptedException 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))); if (VERBOSE) { 170 System.out.println("JBStopRunnable: STOPPED message fired"); } 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"); } 186 } 187 } 188 189 | Popular Tags |