1 19 20 package org.netbeans.modules.j2ee.oc4j.ide; 21 22 import java.io.File ; 23 import java.io.InputStream ; 24 import java.lang.Thread.UncaughtExceptionHandler; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 import javax.enterprise.deploy.shared.ActionType ; 28 import javax.enterprise.deploy.shared.CommandType ; 29 import javax.enterprise.deploy.shared.StateType ; 30 import javax.swing.JOptionPane ; 31 import org.netbeans.api.java.platform.JavaPlatform; 32 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 33 import org.netbeans.modules.j2ee.oc4j.OC4JDeploymentManager; 34 import org.netbeans.modules.j2ee.oc4j.util.OC4JPluginProperties; 35 import org.netbeans.modules.j2ee.oc4j.util.OC4JDebug; 36 import org.openide.ErrorManager; 37 import org.openide.execution.NbProcessDescriptor; 38 import org.openide.filesystems.FileObject; 39 import org.openide.filesystems.FileUtil; 40 import org.openide.util.NbBundle; 41 import org.openide.util.Utilities; 42 43 46 class OC4JStartRunnable implements Runnable { 47 48 static final String SCRIPT_UNIX = "oc4j"; 49 static final String SCRIPT_WIN = "oc4j.cmd"; 50 51 private OC4JDeploymentManager dm; 52 private String instanceName; 53 private OC4JStartServer startServer; 54 private InstanceProperties ip; 55 56 60 private static final int TIMEOUT = 120000; 61 62 65 private static final int DELAY = 1000; 66 67 public OC4JStartRunnable(OC4JDeploymentManager dm, OC4JStartServer startServer) { 68 this.dm = dm; 69 this.ip = dm.getProperties().getInstanceProperties(); 70 this.instanceName = ip.getProperty(InstanceProperties.DISPLAY_NAME_ATTR); 71 this.startServer = startServer; 72 } 73 74 public void run() { 75 long start = System.currentTimeMillis(); 78 79 Process serverProcess = createProcess(); 80 81 if (serverProcess == null) { 82 return; 83 } 84 85 fireStartProgressEvent(StateType.RUNNING, createProgressMessage("MSG_START_SERVER_IN_PROGRESS")); 86 87 OC4JLogger.getInstance(dm.getUri()).readInputStreams(new InputStream [] { 90 serverProcess.getInputStream(), serverProcess.getErrorStream()}); 91 92 while (System.currentTimeMillis() - start < TIMEOUT) { 94 if (startServer.isRunning()) { 96 fireStartProgressEvent(StateType.COMPLETED, createProgressMessage("MSG_SERVER_STARTED")); return; 98 } 99 100 try { 103 Thread.sleep(DELAY); 104 } catch (InterruptedException e) {} 105 } 106 107 fireStartProgressEvent(StateType.FAILED, createProgressMessage("MSG_START_SERVER_FAILED")); 110 } 111 112 private String [] createEnvironment() { 113 StringBuilder javaOpts = new StringBuilder (); 114 List <String > envp = new ArrayList <String >(3); 115 String rootDir = ip.getProperty(OC4JPluginProperties.PROPERTY_OC4J_HOME); 116 JavaPlatform platform = dm.getProperties().getJavaPlatform(); 117 FileObject fo = (FileObject) platform.getInstallFolders().iterator().next(); 118 String javaHome = FileUtil.toFile(fo).getAbsolutePath(); 119 120 if(startServer.getMode() == OC4JStartServer.MODE.DEBUG) { 121 javaOpts.append(" -classic -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address="). append(dm.getProperties().getDebugPort()). 123 append(",server=y,suspend=n"); } 125 126 envp.add("ORACLE_HOME=" + rootDir); envp.add("JAVA_HOME=" + javaHome); envp.add("OC4J_JVM_ARGS=" + javaOpts); envp.add("VERBOSE=on"); 131 return (String []) envp.toArray(new String [envp.size()]); 132 } 133 134 private NbProcessDescriptor createProcessDescriptor() { 135 final String serverLocation = ip.getProperty(OC4JPluginProperties.PROPERTY_OC4J_HOME); 136 final String startScript = serverLocation + File.separator + "bin" + File.separator + 137 (Utilities.isWindows() ? SCRIPT_WIN : SCRIPT_UNIX); OC4JDebug.log(getClass().getName(), "serverLocation: " + serverLocation + ", startScript: " + startScript); 139 140 if (!new File (startScript).exists()){ 141 OC4JDebug.log(getClass().getName(), "startScript " + startScript + " doesn't exist"); 142 fireStartProgressEvent(StateType.FAILED, createProgressMessage("MSG_START_SERVER_FAILED_FNF")); return null; 144 } 145 146 OC4JDebug.log(getClass().getName(), "EXEC: " + startScript + " -start "); 147 return new NbProcessDescriptor(startScript, "-start"); } 149 150 private Process createProcess() { 151 NbProcessDescriptor pd = createProcessDescriptor(); 152 153 if (pd == null) 154 return null; 155 156 try { 157 return pd.exec(null, createEnvironment(), true, new File (ip.getProperty(OC4JPluginProperties.PROPERTY_OC4J_HOME))); 158 } catch (java.io.IOException ioe) { 159 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 160 fireStartProgressEvent(StateType.FAILED, createProgressMessage("MSG_START_SERVER_FAILED_PD")); 161 return null; 162 } 163 } 164 165 private String createProgressMessage(final String resName) { 166 return createProgressMessage(resName, null); 167 } 168 169 private String createProgressMessage(final String resName, final String param) { 170 return NbBundle.getMessage(OC4JStartRunnable.class, resName, instanceName, param); 171 } 172 173 private void fireStartProgressEvent(StateType stateType, String msg) { 174 startServer.fireHandleProgressEvent(null, new OC4JDeploymentStatus(ActionType.EXECUTE, CommandType.START, stateType, msg)); 175 } 176 } | Popular Tags |