1 19 20 package org.netbeans.modules.j2ee.jboss4.ide; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.io.StringReader ; 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.deployment.plugins.api.UISupport; 32 import org.netbeans.modules.j2ee.deployment.profiler.api.ProfilerServerSettings; 33 import org.netbeans.modules.j2ee.jboss4.JBDeploymentManager; 34 import org.netbeans.modules.j2ee.jboss4.ide.ui.JBPluginProperties; 35 import org.netbeans.modules.j2ee.jboss4.ide.ui.JBPluginUtils; 36 import org.netbeans.modules.j2ee.jboss4.util.JBProperties; 37 import org.openide.ErrorManager; 38 import org.openide.execution.NbProcessDescriptor; 39 import org.openide.filesystems.FileObject; 40 import org.openide.filesystems.FileUtil; 41 import org.openide.util.NbBundle; 42 import org.openide.util.Utilities; 43 import org.openide.windows.InputOutput; 44 45 50 class JBStartRunnable implements Runnable { 51 52 private final static String STARTUP_SH = "/bin/run.sh"; 53 private final static String STARTUP_BAT = "/bin/run.bat"; 54 55 private JBDeploymentManager dm; 56 private String instanceName; 57 private JBStartServer startServer; 58 private ProfilerServerSettings profilerSettings; 59 60 JBStartRunnable(ProfilerServerSettings profilerSettings, JBDeploymentManager dm, JBStartServer startServer) { 61 this.dm = dm; 62 this.instanceName = dm.getInstanceProperties().getProperty(InstanceProperties.DISPLAY_NAME_ATTR); 63 this.startServer = startServer; 64 this.profilerSettings = profilerSettings; 65 } 66 67 public void run() { 68 69 InstanceProperties ip = dm.getInstanceProperties(); 70 71 boolean free = checkPorts(ip); 72 if (!free) { 73 return; 74 } 75 76 Process serverProcess = createProcess(ip); 77 if (serverProcess == null) { 78 return; 79 } 80 81 JBLogWriter logWriter = createLogWriter(); 82 83 waitForServerToStart(logWriter, serverProcess); 84 } 85 86 private String [] createEnvironment(final InstanceProperties ip) { 87 88 JBProperties properties = dm.getProperties(); 89 90 String javaOpts = properties.getJavaOpts(); 92 StringBuilder javaOptsBuilder = new StringBuilder (javaOpts); 93 if (properties.getProxyEnabled()) { 96 final String [] PROXY_PROPS = { 97 "http.proxyHost", "http.proxyPort", "http.nonProxyHosts", "https.proxyHost", "https.proxyPort", }; 103 for (String prop : PROXY_PROPS) { 104 if (javaOpts.indexOf(prop) == -1) { 105 String value = System.getProperty(prop); 106 if (value != null) { 107 if ("http.nonProxyHosts".equals(prop)) { try { 109 BufferedReader br = new BufferedReader (new StringReader (value)); 111 String line = null; 112 StringBuilder noNL = new StringBuilder (); 113 while ((line = br.readLine()) != null) { 114 noNL.append(line); 115 } 116 value = noNL.toString(); 117 118 value = "\"" + value + "\""; } 121 catch (IOException ioe) { 122 ErrorManager.getDefault().annotate( 123 ioe, 124 NbBundle.getMessage(JBStartRunnable.class, "ERR_NonProxyHostParsingError") 125 ); 126 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ioe); 127 value = null; 128 } 129 } 130 if (value != null) { 131 javaOptsBuilder.append(" -D").append(prop).append("=").append(value); } 133 } 134 } 135 } 136 } 137 138 if (startServer.getMode() == JBStartServer.MODE.DEBUG && javaOptsBuilder.toString().indexOf("-Xdebug") == -1) { javaOptsBuilder.append(" -classic -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address="). append(dm.getDebuggingPort()). 142 append(",server=y,suspend=n"); } 144 else 145 if (startServer.getMode() == JBStartServer.MODE.PROFILE) { 146 147 String [] profJvmArgs = profilerSettings.getJvmArgs(); 149 for (int i = 0; i < profJvmArgs.length; i++) { 150 javaOptsBuilder.append(" ").append(profJvmArgs[i]); } 152 } 153 154 javaOpts = javaOptsBuilder.toString(); 156 157 JavaPlatform platform = (startServer.getMode() != JBStartServer.MODE.PROFILE ? properties.getJavaPlatform() : profilerSettings.getJavaPlatform()); 159 String javaHome = getJavaHome(platform); 160 161 String envp[] = new String [] { 162 "JAVA=" + javaHome + "/bin/java", "JAVA_HOME=" + javaHome, "JAVA_OPTS=" + javaOpts, }; 166 return envp; 167 } 168 169 private boolean checkPorts(final InstanceProperties ip) { 170 171 try { 172 String serverName = ip.getProperty(InstanceProperties.DISPLAY_NAME_ATTR); 173 174 String strHTTPConnectorPort = ip.getProperty(JBPluginProperties.PROPERTY_PORT); 175 int HTTPConnectorPort = new Integer (strHTTPConnectorPort).intValue(); 176 if (!JBPluginUtils.isPortFree(HTTPConnectorPort)) { 177 fireStartProgressEvent(StateType.FAILED, createProgressMessage("MSG_START_SERVER_FAILED_HTTP_PORT_IN_USE", strHTTPConnectorPort)); 178 return false; 179 } 180 181 String serverDir = ip.getProperty(JBPluginProperties.PROPERTY_SERVER_DIR); 182 183 String strJNPServicePort = JBPluginUtils.getJnpPort(serverDir); 184 int JNPServicePort = new Integer (strJNPServicePort).intValue(); 185 if (!JBPluginUtils.isPortFree(JNPServicePort)) { 186 fireStartProgressEvent(StateType.FAILED, createProgressMessage("MSG_START_SERVER_FAILED_JNP_PORT_IN_USE", strJNPServicePort)); return false; 188 } 189 190 String strRMINamingServicePort = JBPluginUtils.getRMINamingServicePort(serverDir); 191 int RMINamingServicePort = new Integer (strRMINamingServicePort).intValue(); 192 if (!JBPluginUtils.isPortFree(RMINamingServicePort)) { 193 fireStartProgressEvent(StateType.FAILED, createProgressMessage("MSG_START_SERVER_FAILED_RMI_PORT_IN_USE", strRMINamingServicePort)); return false; 195 } 196 197 String server = ip.getProperty(JBPluginProperties.PROPERTY_SERVER); 198 if (!"minimal".equals(server)) { 199 String strRMIInvokerPort = JBPluginUtils.getRMIInvokerPort(serverDir); 200 int RMIInvokerPort = new Integer (strRMIInvokerPort).intValue(); 201 if (!JBPluginUtils.isPortFree(RMIInvokerPort)) { 202 fireStartProgressEvent(StateType.FAILED, createProgressMessage("MSG_START_SERVER_FAILED_INVOKER_PORT_IN_USE", strRMIInvokerPort)); return false; 204 } 205 } 206 207 } catch (NumberFormatException nfe) { 208 } 210 211 return true; 212 } 213 214 private NbProcessDescriptor createProcessDescriptor(InstanceProperties ip) { 215 216 final String serverLocation = ip.getProperty(JBPluginProperties.PROPERTY_ROOT_DIR); 217 final String serverRunFileName = serverLocation + (Utilities.isWindows() ? STARTUP_BAT : STARTUP_SH); 218 if (!new File (serverRunFileName).exists()){ 219 final String serverName = ip.getProperty(InstanceProperties.DISPLAY_NAME_ATTR); 220 fireStartProgressEvent(StateType.FAILED, createProgressMessage("MSG_START_SERVER_FAILED_FNF")); return null; 222 } 223 224 final String instanceName = ip.getProperty(JBPluginProperties.PROPERTY_SERVER); 225 String args = ("all".equals(instanceName) ? "-b 127.0.0.1 " : "") + "-c " + instanceName; return new NbProcessDescriptor(serverRunFileName, args); 227 } 228 229 230 private static String getJavaHome(JavaPlatform platform) { 231 FileObject fo = (FileObject)platform.getInstallFolders().iterator().next(); 232 return FileUtil.toFile(fo).getAbsolutePath(); 233 } 234 235 private String createProgressMessage(final String resName) { 236 return createProgressMessage(resName, null); 237 } 238 239 private String createProgressMessage(final String resName, final String param) { 240 return NbBundle.getMessage(JBStartRunnable.class, resName, instanceName, param); 241 } 242 243 private Process createProcess(InstanceProperties ip) { 244 245 if (startServer.getMode() == JBStartServer.MODE.PROFILE) { 247 248 JBLogWriter logWriter = JBLogWriter.getInstance(instanceName); 250 if (logWriter != null && logWriter.isRunning()) logWriter.stop(); 251 } 252 253 NbProcessDescriptor pd = createProcessDescriptor(ip); 254 if (pd == null) { 255 return null; 256 } 257 258 String envp[] = createEnvironment(ip); 259 260 try { 261 return pd.exec(null, envp, true, null ); 262 } catch (java.io.IOException ioe) { 263 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 264 265 final String serverLocation = ip.getProperty(JBPluginProperties.PROPERTY_ROOT_DIR); 266 final String serverRunFileName = serverLocation + (Utilities.isWindows() ? STARTUP_BAT : STARTUP_SH); 267 fireStartProgressEvent(StateType.FAILED, createProgressMessage("MSG_START_SERVER_FAILED_PD", serverRunFileName)); 268 269 return null; 270 } 271 } 272 273 private InputOutput openConsole() { 274 InputOutput io = UISupport.getServerIO(dm.getUrl()); 275 if (io == null) { 276 return null; } 278 279 try { 281 io.getOut().reset(); 282 } catch (IOException ioe) { 283 } 285 io.select(); 286 287 return io; 288 } 289 290 private void fireStartProgressEvent(StateType stateType, String msg) { 291 startServer.fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.START, stateType, msg)); 292 } 293 294 private JBLogWriter createLogWriter() { 295 InputOutput io = openConsole(); 296 return JBLogWriter.createInstance(io, instanceName); 297 } 298 299 private void waitForServerToStart(JBLogWriter logWriter, Process serverProcess) { 300 301 fireStartProgressEvent(StateType.RUNNING, createProgressMessage("MSG_START_SERVER_IN_PROGRESS")); 302 303 JBStartServer.ACTION_STATUS status = logWriter.start(serverProcess, startServer); 304 if (status == JBStartServer.ACTION_STATUS.SUCCESS) { 305 fireStartProgressEvent(StateType.COMPLETED, createProgressMessage("MSG_SERVER_STARTED")); 306 } 307 else 308 if (status == JBStartServer.ACTION_STATUS.FAILURE) { 309 fireStartProgressEvent(StateType.FAILED, createProgressMessage("MSG_START_SERVER_FAILED")); 310 } 311 else 312 if (status == JBStartServer.ACTION_STATUS.UNKNOWN) { 313 fireStartProgressEvent(StateType.FAILED, createProgressMessage("MSG_StartServerTimeout")); 314 } 315 316 } 317 318 } 319 320 | Popular Tags |