1 19 20 package org.netbeans.modules.j2ee.oc4j.ide; 21 22 import java.io.File ; 23 import java.io.InputStream ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import javax.enterprise.deploy.shared.ActionType ; 27 import javax.enterprise.deploy.shared.CommandType ; 28 import javax.enterprise.deploy.shared.StateType ; 29 import org.netbeans.api.java.platform.JavaPlatform; 30 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 31 import org.netbeans.modules.j2ee.oc4j.OC4JDeploymentManager; 32 import org.netbeans.modules.j2ee.oc4j.util.OC4JPluginProperties; 33 import org.netbeans.modules.j2ee.oc4j.util.OC4JDebug; 34 import org.netbeans.modules.j2ee.oc4j.util.OC4JPluginUtils; 35 import org.openide.ErrorManager; 36 import org.openide.execution.NbProcessDescriptor; 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.FileUtil; 39 import org.openide.util.NbBundle; 40 import org.openide.util.Utilities; 41 42 45 class OC4JStopRunnable implements Runnable { 46 47 private OC4JDeploymentManager dm; 48 private OC4JStartServer startServer; 49 private InstanceProperties ip; 50 private String instanceName; 51 52 56 private static final int TIMEOUT = 120000; 57 58 61 private static final int DELAY = 5000; 62 63 OC4JStopRunnable(OC4JDeploymentManager dm, OC4JStartServer startServer) { 64 this.dm = dm; 65 this.ip = dm.getProperties().getInstanceProperties(); 66 this.instanceName = ip.getProperty(InstanceProperties.DISPLAY_NAME_ATTR); 67 this.startServer = startServer; 68 } 69 70 public void run() { 71 long start = System.currentTimeMillis(); 74 75 Process serverProcess = createProcess(); 76 77 if (serverProcess == null) { 78 return; 79 } 80 81 fireStartProgressEvent(StateType.RUNNING, createProgressMessage("MSG_STOP_SERVER_IN_PROGRESS", instanceName)); 82 83 OC4JLogger.getInstance(dm.getUri()).readInputStreams(new InputStream [] { 86 serverProcess.getInputStream(), serverProcess.getErrorStream()}); 87 88 while (System.currentTimeMillis() - start < TIMEOUT) { 90 if (!startServer.isRunning()) { 92 fireStartProgressEvent(StateType.COMPLETED, createProgressMessage("MSG_SERVER_STOPPED", instanceName)); return; 94 } 95 96 try { 99 Thread.sleep(DELAY); 100 } catch (InterruptedException e) {} 101 102 fireStartProgressEvent(StateType.RUNNING,createProgressMessage("MSG_STOP_SERVER_IN_PROGRESS", instanceName)); 103 } 104 105 fireStartProgressEvent(StateType.FAILED, createProgressMessage("MSG_STOP_SERVER_FAILED", instanceName)); 107 if(serverProcess != null) 108 serverProcess.destroy(); 109 } 110 111 private String [] createEnvironment() { 112 List <String > envp = new ArrayList <String >(3); 113 String rootDir = ip.getProperty(OC4JPluginProperties.PROPERTY_OC4J_HOME); 114 JavaPlatform platform = dm.getProperties().getJavaPlatform(); 115 FileObject fo = (FileObject) platform.getInstallFolders().iterator().next(); 116 String javaHome = FileUtil.toFile(fo).getAbsolutePath(); 117 envp.add("ORACLE_HOME=" + rootDir); envp.add("JAVA_HOME=" + javaHome); envp.add("VERBOSE=on"); 121 return (String []) envp.toArray(new String [envp.size()]); 122 } 123 124 private NbProcessDescriptor createProcessDescriptor() { 125 String serverLocation = ip.getProperty(OC4JPluginProperties.PROPERTY_OC4J_HOME); 126 String scriptPath = serverLocation + File.separator + "bin" + File.separator + 127 (Utilities.isWindows() ? OC4JStartRunnable.SCRIPT_WIN : OC4JStartRunnable.SCRIPT_UNIX); 128 String passwd = ip.getProperty(InstanceProperties.PASSWORD_ATTR); 129 String adminPort = ip.getProperty(OC4JPluginProperties.PROPERTY_ADMIN_PORT); 130 131 if(!ip.getProperty(InstanceProperties.USERNAME_ATTR).equals("oc4jadmin")) 132 passwd = OC4JPluginUtils.requestPassword("oc4jadmin"); 133 134 if (!new File (scriptPath).exists()){ 135 fireStartProgressEvent(StateType.FAILED, createProgressMessage("MSG_STOP_SERVER_FAILED_FNF", instanceName)); return null; 137 } 138 139 OC4JDebug.log(getClass().getName(), "EXEC: " + scriptPath + 140 " -shutdown -port " + adminPort + " -password " + passwd); 141 142 return new NbProcessDescriptor(scriptPath, "-shutdown -port " + 143 adminPort + " -password " + passwd); } 145 146 private Process createProcess() { 147 NbProcessDescriptor pd = createProcessDescriptor(); 148 149 if (pd == null) 150 return null; 151 152 try { 153 return pd.exec(null, createEnvironment(), true, new File (ip.getProperty(OC4JPluginProperties.PROPERTY_OC4J_HOME))); 154 } catch (java.io.IOException ioe) { 155 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 156 fireStartProgressEvent(StateType.FAILED, createProgressMessage("MSG_STOP_SERVER_FAILED_PD")); 157 return null; 158 } 159 } 160 161 private String createProgressMessage(final String resName) { 162 return createProgressMessage(resName, null); 163 } 164 165 private String createProgressMessage(final String resName, final String param) { 166 return NbBundle.getMessage(OC4JStartRunnable.class, resName, instanceName, param); 167 } 168 169 private void fireStartProgressEvent(StateType stateType, String msg) { 170 startServer.fireHandleProgressEvent(null, new OC4JDeploymentStatus(ActionType.EXECUTE, CommandType.STOP, stateType, msg)); 171 } 172 } | Popular Tags |