Révision 2504
tmp/org.txm.statsengine.r.core/src/org/txm/statsengine/r/core/StartRserve.java (revision 2504) | ||
---|---|---|
58 | 58 |
* Rserve instance. |
59 | 59 |
*/ |
60 | 60 |
public class StartRserve { |
61 |
|
|
61 |
|
|
62 | 62 |
/** shortcut to <code>launchRserve(cmd, "--no-save --slave", "--no-save --slave", false)</code>. */ |
63 | 63 |
public static Process rProcess; |
64 |
|
|
64 |
|
|
65 | 65 |
/** |
66 | 66 |
* Launch rserve. |
67 | 67 |
* |
68 | 68 |
* @param cmd the cmd |
69 |
* @param options
|
|
69 |
* @param options |
|
70 | 70 |
* @return true, if successful |
71 | 71 |
*/ |
72 | 72 |
protected static boolean launchRserve(String cmd, int port, boolean debug, String rArgs, String rServeArgs) { |
... | ... | |
74 | 74 |
&& new File(cmd).canExecute()) |
75 | 75 |
return launchRserve(cmd, |
76 | 76 |
"--no-save --encoding utf8 " + rArgs, "--RS-encoding utf8 --no-save --encoding utf8 " + rServeArgs, port, debug); //$NON-NLS-1$ //$NON-NLS-2$ |
77 |
return false;//checkLocalRserve(port, debug, rArgs, rServeArgs); |
|
77 |
return false;// checkLocalRserve(port, debug, rArgs, rServeArgs);
|
|
78 | 78 |
} |
79 |
|
|
79 |
|
|
80 | 80 |
/** |
81 | 81 |
* attempt to start Rserve. Note: parameters are <b>not</b> quoted, so avoid |
82 | 82 |
* using any quotes in arguments |
... | ... | |
86 | 86 |
* @param rsrvargs arguments to be passed to Rserve |
87 | 87 |
* @param debug the debug |
88 | 88 |
* @return <code>true</code> if Rserve is running or was successfully |
89 |
* started, <code>false</code> otherwise. |
|
89 |
* started, <code>false</code> otherwise.
|
|
90 | 90 |
*/ |
91 | 91 |
protected static boolean launchRserve(String cmd, String rargs, String rsrvargs, int port, boolean debug) { |
92 |
//System.out.println("LAUNCH R SERVER "+cmd+" "+port+" "+debug); |
|
92 |
// System.out.println("LAUNCH R SERVER "+cmd+" "+port+" "+debug);
|
|
93 | 93 |
try { |
94 | 94 |
String[] cmdline = null; |
95 |
|
|
95 | 96 |
// FIXME: don't forget to remove this Window dedicated code if we manage to use http_proxy command line argument |
97 |
// Windows OS |
|
96 | 98 |
boolean isWindows = false; |
97 |
|
|
98 |
// Windows OS |
|
99 | 99 |
if (OSDetector.isFamilyWindows()) { |
100 | 100 |
isWindows = true; /* Windows startup */ |
101 | 101 |
|
102 | 102 |
|
103 | 103 |
// TODO: old tests for setting proxy in R |
104 |
//Windows : set --internet2 option to manage proxy if any |
|
104 |
// Windows : set --internet2 option to manage proxy if any
|
|
105 | 105 |
|
106 |
// // set Proxy configuration using Eclipse RCP stored values
|
|
107 |
// String proxy_conf = "";
|
|
108 |
// ProxyConf conf = new ProxyConf(RWorkspace.getRWorkspaceInstance());
|
|
109 |
// if (conf.mustSetProxyConfiguration()) {
|
|
110 |
// proxy_conf = "http_proxy="+conf.getHttpProxyUrl();
|
|
111 |
// rsrvargs += " "+proxy_conf; // add the http_proxy parameter
|
|
112 |
// Log.info("Run Rserve with proxy configuration: "+proxy_conf);
|
|
113 |
// }
|
|
106 |
// // set Proxy configuration using Eclipse RCP stored values
|
|
107 |
// String proxy_conf = "";
|
|
108 |
// ProxyConf conf = new ProxyConf(RWorkspace.getRWorkspaceInstance());
|
|
109 |
// if (conf.mustSetProxyConfiguration()) {
|
|
110 |
// proxy_conf = "http_proxy="+conf.getHttpProxyUrl();
|
|
111 |
// rsrvargs += " "+proxy_conf; // add the http_proxy parameter
|
|
112 |
// Log.info("Run Rserve with proxy configuration: "+proxy_conf);
|
|
113 |
// }
|
|
114 | 114 |
|
115 | 115 |
cmdline = new String[] { |
116 | 116 |
cmd, |
117 |
rargs,
|
|
117 |
rargs, |
|
118 | 118 |
"-e", //$NON-NLS-1$ |
119 |
"library(Rserve);run.Rserve(" //$NON-NLS-1$
|
|
120 |
+ (debug ? "TRUE" : "FALSE") //$NON-NLS-1$ //$NON-NLS-2$ |
|
121 |
+ ",port=" + port + "" //$NON-NLS-1$ //$NON-NLS-2$ |
|
122 |
+ ",args='" //$NON-NLS-1$ |
|
123 |
+ rsrvargs + "')\" " //$NON-NLS-1$ |
|
124 |
};
|
|
119 |
"library(Rserve);run.Rserve(" //$NON-NLS-1$ |
|
120 |
+ (debug ? "TRUE" : "FALSE") //$NON-NLS-1$ //$NON-NLS-2$
|
|
121 |
+ ",port=" + port + "" //$NON-NLS-1$ //$NON-NLS-2$
|
|
122 |
+ ",args='" //$NON-NLS-1$
|
|
123 |
+ rsrvargs + "')\" " //$NON-NLS-1$
|
|
124 |
}; |
|
125 | 125 |
} |
126 | 126 |
// Mac, Linux |
127 | 127 |
else { |
... | ... | |
129 | 129 |
"/bin/sh", //$NON-NLS-1$ |
130 | 130 |
"-c", //$NON-NLS-1$ |
131 | 131 |
"echo 'library(Rserve);run.Rserve(" //$NON-NLS-1$ |
132 |
+ (debug ? "TRUE" : "FALSE") //$NON-NLS-1$ //$NON-NLS-2$ |
|
133 |
+ ",port="+port+"" //$NON-NLS-1$ //$NON-NLS-2$
|
|
134 |
+ ",args=\"" + rsrvargs //$NON-NLS-1$ |
|
135 |
+ "\")'|" + cmd //$NON-NLS-1$
|
|
136 |
+ " " + rargs }; //$NON-NLS-1$ |
|
132 |
+ (debug ? "TRUE" : "FALSE") //$NON-NLS-1$ //$NON-NLS-2$
|
|
133 |
+ ",port=" + port + "" //$NON-NLS-1$ //$NON-NLS-2$
|
|
134 |
+ ",args=\"" + rsrvargs //$NON-NLS-1$
|
|
135 |
+ "\")'|" + cmd //$NON-NLS-1$
|
|
136 |
+ " " + rargs }; //$NON-NLS-1$
|
|
137 | 137 |
} |
138 |
|
|
139 |
// ProcessBuilder builder = new ProcessBuilder(cmdline);
|
|
140 |
// Rserveprocess = builder.start();
|
|
141 |
|
|
138 |
|
|
139 |
// ProcessBuilder builder = new ProcessBuilder(cmdline);
|
|
140 |
// Rserveprocess = builder.start();
|
|
141 |
|
|
142 | 142 |
Runtime runtime = Runtime.getRuntime(); |
143 | 143 |
Log.fine(RCoreMessages.bind(RCoreMessages.info_startingRWithCommandLine, StringUtils.join(cmdline, " "))); //$NON-NLS-1$ |
144 | 144 |
rProcess = runtime.exec(cmdline); |
145 | 145 |
|
146 |
//Log.info(Messages.StartRserve_0+Arrays.toString(cmdline)); |
|
146 |
// Log.info(Messages.StartRserve_0+Arrays.toString(cmdline));
|
|
147 | 147 |
StreamHog errStream = new StreamHog(rProcess.getErrorStream(), RWorkspace.isLoggingEvalCommandLines()); |
148 | 148 |
errStream.setName("R-err"); |
149 | 149 |
StreamHog inStream = new StreamHog(rProcess.getInputStream(), RWorkspace.isLoggingEvalCommandLines()); |
150 | 150 |
inStream.setName("R"); |
151 | 151 |
RWorkspace.getRWorkspaceInstance().registerLogger(errStream, inStream); |
152 |
|
|
153 |
//if (!debug && !isWindows) Rserveprocess.waitFor(); |
|
152 |
|
|
153 |
// if (!debug && !isWindows) Rserveprocess.waitFor();
|
|
154 | 154 |
// System.out.println("call terminated, let us try to connect ..."); |
155 |
} catch (Exception x) { |
|
155 |
} |
|
156 |
catch (Exception x) { |
|
156 | 157 |
Log.severe(RCoreMessages.error_failedToStartRServeWithCommand + x.getMessage()); |
157 | 158 |
return false; |
158 | 159 |
} |
159 |
|
|
160 |
|
|
160 | 161 |
Log.fine(RCoreMessages.info_statisticsEngineLaunched); |
161 |
|
|
162 |
|
|
162 | 163 |
try { |
163 | 164 |
Thread.sleep(200); |
164 |
} catch (InterruptedException ix) { } |
|
165 |
|
|
165 |
} |
|
166 |
catch (InterruptedException ix) { |
|
167 |
} |
|
168 |
|
|
166 | 169 |
int attempts = 10; |
167 | 170 |
while (attempts > 0) { |
168 | 171 |
try { |
169 |
//System.out.print("."); //$NON-NLS-1$ |
|
172 |
// System.out.print("."); //$NON-NLS-1$
|
|
170 | 173 |
RConnection c = new RConnection("127.0.0.1", port); //$NON-NLS-1$ |
171 | 174 |
c.close(); |
172 | 175 |
return true; |
173 |
} catch (Exception e2) { |
|
176 |
} |
|
177 |
catch (Exception e2) { |
|
174 | 178 |
try { |
175 | 179 |
Thread.sleep(1500); |
176 |
} catch (InterruptedException ix) { } |
|
180 |
} |
|
181 |
catch (InterruptedException ix) { |
|
182 |
} |
|
177 | 183 |
} |
178 |
|
|
184 |
|
|
179 | 185 |
attempts--; |
180 | 186 |
} |
181 | 187 |
Log.fine("R connection attemps done."); |
182 | 188 |
return false; |
183 | 189 |
} |
184 |
|
|
190 |
|
|
185 | 191 |
/** |
186 | 192 |
* checks whether Rserve is running and if that's not the case it attempts |
187 | 193 |
* to start it using the defaults for the platform where it is run on. This |
188 | 194 |
* method is meant to be set-and-forget and cover most default setups. For |
189 | 195 |
* special setups you may get more control over R with < |
190 | 196 |
* <code>launchRserve</code> instead. |
191 |
* @param rServeArgs |
|
192 |
* @param rargs |
|
197 |
* |
|
198 |
* @param rServeArgs |
|
199 |
* @param rargs |
|
193 | 200 |
* |
194 | 201 |
* @return true, if successful |
195 | 202 |
*/ |
... | ... | |
200 | 207 |
return true; |
201 | 208 |
} |
202 | 209 |
// Windows OS |
203 |
// if (OSDetector.isFamilyWindows()) {
|
|
204 |
// // System.out.println("Windows: query registry to find where R is installed ...");
|
|
205 |
// String installPath = null;
|
|
206 |
// try {
|
|
207 |
// Process rp = Runtime.getRuntime().exec("reg query HKLM\\Software\\R-core\\R"); //$NON-NLS-1$
|
|
208 |
// StreamHog regHog = new StreamHog(rp.getInputStream(), true);
|
|
209 |
// rp.waitFor();
|
|
210 |
// regHog.join();
|
|
211 |
// installPath = regHog.getInstallPath();
|
|
212 |
// } catch (Exception rge) {
|
|
213 |
// Log.severe(RCoreMessages.error_failedToRunREG + rge);
|
|
214 |
// return false;
|
|
215 |
// }
|
|
216 |
// if (installPath == null) {
|
|
217 |
// Log.severe(RCoreMessages.error_youNeedRSoftware);
|
|
218 |
// return false;
|
|
219 |
// }
|
|
220 |
// // System.out.println(" Found R in : "+installPath +
|
|
221 |
// // "\\bin\\R.exe");
|
|
222 |
// return launchRserve(installPath + "\\bin\\R.exe", port, debug, rargs, rServeArgs); //$NON-NLS-1$
|
|
223 |
// }
|
|
224 |
|
|
210 |
// if (OSDetector.isFamilyWindows()) {
|
|
211 |
// // System.out.println("Windows: query registry to find where R is installed ...");
|
|
212 |
// String installPath = null;
|
|
213 |
// try {
|
|
214 |
// Process rp = Runtime.getRuntime().exec("reg query HKLM\\Software\\R-core\\R"); //$NON-NLS-1$
|
|
215 |
// StreamHog regHog = new StreamHog(rp.getInputStream(), true);
|
|
216 |
// rp.waitFor();
|
|
217 |
// regHog.join();
|
|
218 |
// installPath = regHog.getInstallPath();
|
|
219 |
// } catch (Exception rge) {
|
|
220 |
// Log.severe(RCoreMessages.error_failedToRunREG + rge);
|
|
221 |
// return false;
|
|
222 |
// }
|
|
223 |
// if (installPath == null) {
|
|
224 |
// Log.severe(RCoreMessages.error_youNeedRSoftware);
|
|
225 |
// return false;
|
|
226 |
// }
|
|
227 |
// // System.out.println(" Found R in : "+installPath +
|
|
228 |
// // "\\bin\\R.exe");
|
|
229 |
// return launchRserve(installPath + "\\bin\\R.exe", port, debug, rargs, rServeArgs); //$NON-NLS-1$
|
|
230 |
// }
|
|
231 |
|
|
225 | 232 |
Log.fine(RCoreMessages.info_tryingToStartRWithRPath); |
226 | 233 |
if (launchRserve("R", port, debug, rargs, rServeArgs)) { //$NON-NLS-1$ |
227 | 234 |
return true; |
228 | 235 |
} |
229 |
|
|
236 |
|
|
230 | 237 |
// R not in the PATH env, try with R common installation paths |
231 | 238 |
String[] paths = { |
232 | 239 |
"/Library/Frameworks/R.framework/Resources/bin/R", //$NON-NLS-1$ |
... | ... | |
237 | 244 |
"/sw/bin/R", //$NON-NLS-1$ |
238 | 245 |
"/usr/common/bin/R", //$NON-NLS-1$ |
239 | 246 |
"/opt/bin/R" }; //$NON-NLS-1$ |
240 |
|
|
247 |
|
|
241 | 248 |
for (String path : paths) { |
242 | 249 |
File execFile = new File(path); |
243 | 250 |
Log.fine(RCoreMessages.bind(RCoreMessages.info_tryingToStartRFrom, path)); |
... | ... | |
245 | 252 |
return true; |
246 | 253 |
} |
247 | 254 |
} |
248 |
|
|
255 |
|
|
249 | 256 |
return false; |
250 |
//
|
|
251 |
// return (launchRserve("R", port, debug) //$NON-NLS-1$
|
|
252 |
// || /* try some common unix locations of R */
|
|
253 |
// (( && launchRserve("/Library/Frameworks/R.framework/Resources/bin/R", port, debug)) //$NON-NLS-1$
|
|
254 |
// || ((new File("/usr/local/lib/R/bin/R")).exists() && launchRserve("/usr/local/lib/R/bin/R")) //$NON-NLS-1$ //$NON-NLS-2$
|
|
255 |
// || ((new File("/usr/lib/R/bin/R")).exists() && launchRserve("/usr/lib/R/bin/R")) //$NON-NLS-1$ //$NON-NLS-2$
|
|
256 |
// || ((new File("/usr/local/bin/R")).exists() && launchRserve("/usr/local/bin/R")) //$NON-NLS-1$ //$NON-NLS-2$
|
|
257 |
// || ((new File("/sw/bin/R")).exists() && launchRserve("/sw/bin/R")) //$NON-NLS-1$ //$NON-NLS-2$
|
|
258 |
// || ((new File("/usr/common/bin/R")).exists() && launchRserve("/usr/common/bin/R")) || ((new File( //$NON-NLS-1$ //$NON-NLS-2$
|
|
259 |
// "/opt/bin/R")).exists() && launchRserve("/opt/bin/R"))); //$NON-NLS-1$ //$NON-NLS-2$
|
|
257 |
// |
|
258 |
// return (launchRserve("R", port, debug) //$NON-NLS-1$
|
|
259 |
// || /* try some common unix locations of R */
|
|
260 |
// (( && launchRserve("/Library/Frameworks/R.framework/Resources/bin/R", port, debug)) //$NON-NLS-1$
|
|
261 |
// || ((new File("/usr/local/lib/R/bin/R")).exists() && launchRserve("/usr/local/lib/R/bin/R")) //$NON-NLS-1$ //$NON-NLS-2$
|
|
262 |
// || ((new File("/usr/lib/R/bin/R")).exists() && launchRserve("/usr/lib/R/bin/R")) //$NON-NLS-1$ //$NON-NLS-2$
|
|
263 |
// || ((new File("/usr/local/bin/R")).exists() && launchRserve("/usr/local/bin/R")) //$NON-NLS-1$ //$NON-NLS-2$
|
|
264 |
// || ((new File("/sw/bin/R")).exists() && launchRserve("/sw/bin/R")) //$NON-NLS-1$ //$NON-NLS-2$
|
|
265 |
// || ((new File("/usr/common/bin/R")).exists() && launchRserve("/usr/common/bin/R")) || ((new File( //$NON-NLS-1$ //$NON-NLS-2$
|
|
266 |
// "/opt/bin/R")).exists() && launchRserve("/opt/bin/R"))); //$NON-NLS-1$ //$NON-NLS-2$
|
|
260 | 267 |
} |
261 |
|
|
268 |
|
|
262 | 269 |
/** |
263 | 270 |
* check whether Rserve is currently running (on local machine and default |
264 | 271 |
* port). |
... | ... | |
272 | 279 |
// System.out.println("Rserve is running."); |
273 | 280 |
// c.close(); |
274 | 281 |
return true; |
275 |
} catch (Exception e) { |
|
282 |
} |
|
283 |
catch (Exception e) { |
|
276 | 284 |
// System.out.println("First connect try failed with: "+ |
277 | 285 |
// e.getMessage()); |
278 | 286 |
} |
279 | 287 |
return false; |
280 | 288 |
} |
281 |
|
|
282 |
|
|
289 |
|
|
290 |
|
|
283 | 291 |
public static void main(String[] args) { |
284 | 292 |
System.out.println("result=" + checkLocalRserve(8212, false, "", "")); |
285 | 293 |
try { |
... | ... | |
290 | 298 |
c.eval("2+2"); |
291 | 299 |
c.shutdown(); |
292 | 300 |
System.out.println("Done"); |
293 |
} catch (Exception x) { |
|
301 |
} |
|
302 |
catch (Exception x) { |
|
294 | 303 |
x.printStackTrace(); |
295 | 304 |
} |
296 | 305 |
} |
Formats disponibles : Unified diff