KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.StringReader JavaDoc;
26 import javax.enterprise.deploy.shared.ActionType JavaDoc;
27 import javax.enterprise.deploy.shared.CommandType JavaDoc;
28 import javax.enterprise.deploy.shared.StateType JavaDoc;
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 /**
46  *
47  * @author Kirill Sorokin
48  * @author Libor Kotouc
49  */

50 class JBStartRunnable implements Runnable JavaDoc {
51
52     private final static String JavaDoc STARTUP_SH = "/bin/run.sh";
53     private final static String JavaDoc STARTUP_BAT = "/bin/run.bat";
54
55     private JBDeploymentManager dm;
56     private String JavaDoc 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 JavaDoc 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 JavaDoc[] createEnvironment(final InstanceProperties ip) {
87         
88         JBProperties properties = dm.getProperties();
89
90         // set the JAVA_OPTS value
91
String JavaDoc javaOpts = properties.getJavaOpts();
92         StringBuilder JavaDoc javaOptsBuilder = new StringBuilder JavaDoc(javaOpts);
93         // use the IDE proxy settings if the 'use proxy' checkbox is selected
94
// do not override a property if it was set manually by the user
95
if (properties.getProxyEnabled()) {
96             final String JavaDoc[] PROXY_PROPS = {
97                 "http.proxyHost", // NOI18N
98
"http.proxyPort", // NOI18N
99
"http.nonProxyHosts", // NOI18N
100
"https.proxyHost", // NOI18N
101
"https.proxyPort", // NOI18N
102
};
103             for (String JavaDoc prop : PROXY_PROPS) {
104                 if (javaOpts.indexOf(prop) == -1) {
105                     String JavaDoc value = System.getProperty(prop);
106                     if (value != null) {
107                         if ("http.nonProxyHosts".equals(prop)) { // NOI18N
108
try {
109                                 // remove newline characters, as the value may contain them, see issue #81174
110
BufferedReader JavaDoc br = new BufferedReader JavaDoc(new StringReader JavaDoc(value));
111                                 String JavaDoc line = null;
112                                 StringBuilder JavaDoc noNL = new StringBuilder JavaDoc();
113                                 while ((line = br.readLine()) != null) {
114                                     noNL.append(line);
115                                 }
116                                 value = noNL.toString();
117
118                                 // enclose the host list in double quotes because it may contain spaces
119
value = "\"" + value + "\""; // NOI18N
120
}
121                             catch (IOException JavaDoc 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); // NOI18N
132
}
133                     }
134                 }
135             }
136         }
137
138         if (startServer.getMode() == JBStartServer.MODE.DEBUG && javaOptsBuilder.toString().indexOf("-Xdebug") == -1) { // NOI18N
139
// if in debug mode and the debug options not specified manually
140
javaOptsBuilder.append(" -classic -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address="). // NOI18N
141
append(dm.getDebuggingPort()).
142                             append(",server=y,suspend=n"); // NOI18N
143
}
144         else
145         if (startServer.getMode() == JBStartServer.MODE.PROFILE) {
146
147             // get JVM arguments used for starting the server
148
String JavaDoc[] profJvmArgs = profilerSettings.getJvmArgs();
149             for (int i = 0; i < profJvmArgs.length; i++) {
150                 javaOptsBuilder.append(" ").append(profJvmArgs[i]); // NOI18N
151
}
152         }
153
154         // create new environment for server
155
javaOpts = javaOptsBuilder.toString();
156
157         // get Java platform that will run the server
158
JavaPlatform platform = (startServer.getMode() != JBStartServer.MODE.PROFILE ? properties.getJavaPlatform() : profilerSettings.getJavaPlatform());
159         String JavaDoc javaHome = getJavaHome(platform);
160
161         String JavaDoc envp[] = new String JavaDoc[] {
162             "JAVA=" + javaHome + "/bin/java", // NOI18N
163
"JAVA_HOME=" + javaHome, // NOI18N
164
"JAVA_OPTS=" + javaOpts, // NOI18N
165
};
166         return envp;
167     }
168     
169     private boolean checkPorts(final InstanceProperties ip) {
170
171         try {
172             String JavaDoc serverName = ip.getProperty(InstanceProperties.DISPLAY_NAME_ATTR);
173
174             String JavaDoc strHTTPConnectorPort = ip.getProperty(JBPluginProperties.PROPERTY_PORT);
175             int HTTPConnectorPort = new Integer JavaDoc(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 JavaDoc serverDir = ip.getProperty(JBPluginProperties.PROPERTY_SERVER_DIR);
182
183             String JavaDoc strJNPServicePort = JBPluginUtils.getJnpPort(serverDir);
184             int JNPServicePort = new Integer JavaDoc(strJNPServicePort).intValue();
185             if (!JBPluginUtils.isPortFree(JNPServicePort)) {
186                 fireStartProgressEvent(StateType.FAILED, createProgressMessage("MSG_START_SERVER_FAILED_JNP_PORT_IN_USE", strJNPServicePort));//NOI18N
187
return false;
188             }
189
190             String JavaDoc strRMINamingServicePort = JBPluginUtils.getRMINamingServicePort(serverDir);
191             int RMINamingServicePort = new Integer JavaDoc(strRMINamingServicePort).intValue();
192             if (!JBPluginUtils.isPortFree(RMINamingServicePort)) {
193                 fireStartProgressEvent(StateType.FAILED, createProgressMessage("MSG_START_SERVER_FAILED_RMI_PORT_IN_USE", strRMINamingServicePort));//NOI18N
194
return false;
195             }
196
197             String JavaDoc server = ip.getProperty(JBPluginProperties.PROPERTY_SERVER);
198             if (!"minimal".equals(server)) {
199                 String JavaDoc strRMIInvokerPort = JBPluginUtils.getRMIInvokerPort(serverDir);
200                 int RMIInvokerPort = new Integer JavaDoc(strRMIInvokerPort).intValue();
201                 if (!JBPluginUtils.isPortFree(RMIInvokerPort)) {
202                     fireStartProgressEvent(StateType.FAILED, createProgressMessage("MSG_START_SERVER_FAILED_INVOKER_PORT_IN_USE", strRMIInvokerPort));//NOI18N
203
return false;
204                 }
205             }
206
207         } catch (NumberFormatException JavaDoc nfe) {
208             // continue and let server to report the problem
209
}
210         
211         return true;
212     }
213
214     private NbProcessDescriptor createProcessDescriptor(InstanceProperties ip) {
215         
216         final String JavaDoc serverLocation = ip.getProperty(JBPluginProperties.PROPERTY_ROOT_DIR);
217         final String JavaDoc serverRunFileName = serverLocation + (Utilities.isWindows() ? STARTUP_BAT : STARTUP_SH);
218         if (!new File JavaDoc(serverRunFileName).exists()){
219             final String JavaDoc serverName = ip.getProperty(InstanceProperties.DISPLAY_NAME_ATTR);
220             fireStartProgressEvent(StateType.FAILED, createProgressMessage("MSG_START_SERVER_FAILED_FNF"));//NOI18N
221
return null;
222         }
223
224         final String JavaDoc instanceName = ip.getProperty(JBPluginProperties.PROPERTY_SERVER);
225         String JavaDoc args = ("all".equals(instanceName) ? "-b 127.0.0.1 " : "") + "-c " + instanceName; // NOI18N
226
return new NbProcessDescriptor(serverRunFileName, args);
227     }
228
229     
230     private static String JavaDoc getJavaHome(JavaPlatform platform) {
231         FileObject fo = (FileObject)platform.getInstallFolders().iterator().next();
232         return FileUtil.toFile(fo).getAbsolutePath();
233     }
234  
235     private String JavaDoc createProgressMessage(final String JavaDoc resName) {
236         return createProgressMessage(resName, null);
237     }
238     
239     private String JavaDoc createProgressMessage(final String JavaDoc resName, final String JavaDoc param) {
240         return NbBundle.getMessage(JBStartRunnable.class, resName, instanceName, param);
241     }
242
243     private Process JavaDoc createProcess(InstanceProperties ip) {
244         
245         //TODO do we really have to stop the log writer?
246
if (startServer.getMode() == JBStartServer.MODE.PROFILE) {
247
248             // stop logger if running
249
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 JavaDoc envp[] = createEnvironment(ip);
259
260         try {
261             return pd.exec(null, envp, true, null );
262         } catch (java.io.IOException JavaDoc ioe) {
263             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
264
265             final String JavaDoc serverLocation = ip.getProperty(JBPluginProperties.PROPERTY_ROOT_DIR);
266             final String JavaDoc 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; // finish, it looks like this server instance has been unregistered
277
}
278
279         // clear the old output
280
try {
281             io.getOut().reset();
282         } catch (IOException JavaDoc ioe) {
283             // no op
284
}
285         io.select();
286         
287         return io;
288     }
289
290     private void fireStartProgressEvent(StateType JavaDoc stateType, String JavaDoc 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 JavaDoc 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