Révision 2805

tmp/org.txm.statsengine.r.core/src/org/txm/statsengine/r/core/RStatsEngine.java (revision 2805)
1 1
package org.txm.statsengine.r.core;
2 2

  
3
import java.io.File;
4
import java.io.IOException;
3 5
import java.util.HashMap;
4 6

  
5 7
import org.eclipse.core.runtime.IConfigurationElement;
6 8
import org.eclipse.core.runtime.IProgressMonitor;
7 9
import org.eclipse.core.runtime.RegistryFactory;
10
import org.eclipse.osgi.util.NLS;
8 11
import org.txm.core.messages.TXMCoreMessages;
9 12
import org.txm.core.results.TXMResult;
10 13
import org.txm.statsengine.core.StatsEngine;
11 14
import org.txm.statsengine.r.core.exceptions.RWorkspaceException;
12 15
import org.txm.statsengine.r.core.messages.RCoreMessages;
13 16
import org.txm.statsengine.r.core.preferences.RPreferences;
17
import org.txm.utils.OSDetector;
18
import org.txm.utils.StreamHog;
14 19
import org.txm.utils.logger.Log;
15 20

  
16 21
/**
......
21 26
 *
22 27
 */
23 28
public class RStatsEngine extends StatsEngine {
24
	
25
	
29

  
30

  
26 31
	/** The state of the stats engine. */
27 32
	private static boolean started = false;
28
	
29
	
33

  
34

  
30 35
	private static boolean mandatory = false;
31
	
32
	
36

  
37

  
33 38
	public RStatsEngine() {
34 39
		// no instantiation
35 40
	}
36
	
37
	
41

  
42

  
38 43
	/**
39 44
	 * Starts the stats engine if the engine is not remote then launch RServe.
40 45
	 *
......
46 51
			if (monitor != null) {
47 52
				monitor.subTask("Starting Statistics Engine...");
48 53
			}
49
			
54

  
50 55
			if (RPreferences.getInstance().getBoolean(RPreferences.DISABLE)) {
51 56
				Log.severe("Warning, Statistics Engine is disabled.");
52 57
				return false;
53 58
			}
54
			
59

  
55 60
			// try launching R server and connecting to it
56 61
			int port = RPreferences.getInstance().getInt(RPreferences.PORT);
57 62
			String user = RPreferences.getInstance().getString(RPreferences.USER);
......
59 64
			boolean remote = RPreferences.getInstance().getBoolean(RPreferences.REMOTE);
60 65
			boolean debug = RPreferences.getInstance().getBoolean(RPreferences.DEBUG);
61 66
			mandatory = RPreferences.getInstance().getBoolean(RPreferences.IS_MANDATORY);
62
			
67

  
63 68
			Log.fine(RCoreMessages.bind(RCoreMessages.info_startingStatsEngine, new Object[] { user, remote, port, debug }));
64 69
			// System.out.println("test remote :" + R_PATH_TO_EXECUTABLE + ":" + properties.getProperty(R_PATH_TO_EXECUTABLE));
65 70
			if (!remote) {
......
70 75
						debug,
71 76
						RPreferences.getInstance().getString(RPreferences.RARGS),
72 77
						RPreferences.getInstance().getString(RPreferences.RSERVEARGS));
73
				
78

  
74 79
				if (started) { // try connecting to R witout login
75 80
					started = RWorkspace.connect("127.0.0.1", port); //$NON-NLS-1$
76 81
				}
......
78 83
			else { // try connecting to R, with login and password
79 84
				started = RWorkspace.connect(RPreferences.getInstance().getString(RPreferences.SERVER_ADDRESS), port, user, password);
80 85
			}
81
			
86

  
82 87
			if (started) { // post configuration of R
83 88
				String rPackagesPath = RPreferences.getInstance().getString(RPreferences.PACKAGES_PATH);
84 89
				started = RWorkspace.getRWorkspaceInstance().postConfiguration(rPackagesPath);
85 90
			}
86
			
91

  
87 92
			// System.out.println("try to set R_FILE_TRANSFERT to " + RPreferences.getInstance().getBoolean(RPreferences.R_FILE_TRANSFERT));
88 93
			RWorkspace.setUseFileCommunication(RPreferences.getInstance().getBoolean(RPreferences.FILE_TRANSFERT));
89 94
			// System.out.println("success");
90 95
			// System.out.println("file transfert ? "+RWorkspace.isFileTranfert());
91
			
96

  
92 97
			loadRTransformers();
93 98
		}
94 99
		catch (RWorkspaceException e) {
95 100
			Log.severe(RCoreMessages.error_connectionFailed);
96 101
			started = false;
97 102
		}
98
		
103

  
99 104
		return started;
100 105
	}
101
	
106

  
102 107
	public HashMap<String, RTransformer<? extends TXMResult>> getTransformers() {
103 108
		return transformers;
104 109
	}
105
	
110

  
106 111
	HashMap<String, RTransformer<? extends TXMResult>> transformers = new HashMap<>();
107
	
112

  
108 113
	private void loadRTransformers() {
109
		
114

  
110 115
		transformers.clear();
111
		
116

  
112 117
		IConfigurationElement[] contributions = RegistryFactory.getRegistry().getConfigurationElementsFor(RTransformer.class.getName());
113
		
118

  
114 119
		Log.fine(TXMCoreMessages.bind("Looking for R transformers contributions with extension point id ''{0}''...", RTransformer.class.getName())); //$NON-NLS-1$
115 120
		Log.fine(TXMCoreMessages.bind("{0} transformers(s) found.", contributions.length)); //$NON-NLS-1$
116
		
121

  
117 122
		for (int i = 0; i < contributions.length; i++) {
118 123
			try {
119 124
				@SuppressWarnings("unchecked")
120 125
				RTransformer<? extends TXMResult> transformer = (RTransformer<? extends TXMResult>) contributions[i].createExecutableExtension("class"); //$NON-NLS-1$
121
				
126

  
122 127
				Log.finer(TXMCoreMessages.bind("Registering R transformer: {0}...", transformer));
123
				
128

  
124 129
				transformers.put(contributions[i].getAttribute("type"), transformer);
125 130
			}
126 131
			catch (Exception e) {
......
129 134
			}
130 135
		}
131 136
	}
132
	
137

  
133 138
	@Override
134 139
	public boolean stop() {
135 140
		try {
......
145 150
		}
146 151
		return true;
147 152
	}
148
	
149
	
150
	
153

  
154

  
155

  
151 156
	/**
152 157
	 * kill Rserve process.
153 158
	 */
154 159
	public static void kill() {
160
		
155 161
		if (!RPreferences.getInstance().getBoolean(RPreferences.REMOTE)) {
156
			
162

  
157 163
			try {
158 164
				if (StartRserve.rProcess != null) {
159 165
					Log.finest("Quitting R process...");
......
164 170
				// TODO Auto-generated catch block
165 171
				e1.printStackTrace();
166 172
			}
167
			
173

  
168 174
			// FIXME: SJ: became useless ?
169 175
			// FIXME: SJ, later: actually it seems R and RTerm are not always terminated on Windows, need to check again before removing this code
170 176
			// // Windows OS
171
			// if (OSDetector.isFamilyWindows()) {
172
			// try {
173
			// String cmd = "taskkill /IM Rterm.exe /F"; //$NON-NLS-1$
174
			// //String cmd = "cmd /c FOR /F \"tokens=5 delims= \" %P IN ('netstat -ano ^| findstr :6330 ^| findstr LISTENING') DO taskkill /F /PID %P"; //$NON-NLS-1$
175
			// Log.finest("Executing command: " + cmd);
176
			//
177
			// Process p = Runtime.getRuntime().exec(
178
			// //"taskkill /IM Rserve.exe /F"); //$NON-NLS-1$
179
			// cmd
180
			// );
181
			//
182
			// // consumes streams
183
			// new StreamConsumer(p.getErrorStream(), -1).start();;
184
			// new StreamConsumer(p.getInputStream(), 0).start();
185
			//
186
			// p.waitFor();
187
			// started = false;
188
			// } catch (IOException e) {
189
			// Log.severe(RCoreMessages.error_failedToKillRServe + Log.toString(e));
190
			// try {
191
			//
192
			// String cmd = "cmd /c FOR /F \"tokens=5 delims= \" %P IN ('netstat -ano ^| findstr :6330 ^| findstr LISTENING') DO tskill %P"; //$NON-NLS-1$
193
			// Log.finest("Executing command: " + cmd);
194
			//
195
			// Process p = Runtime.getRuntime().exec(
196
			// //"tskill Rserve.exe"); //$NON-NLS-1$
197
			// cmd
198
			// );
199
			// p.waitFor();
200
			// started = false;
201
			// } catch (IOException e2) {
202
			// Log.severe(RCoreMessages.error_failedToKillRServe2+Log.toString(e2));
203
			// Log.printStackTrace(e2);
204
			// } catch (InterruptedException e3) {
205
			// Log.severe(Log.toString(e3));
206
			// Log.printStackTrace(e3);
207
			// }
208
			// }
209
			// catch (InterruptedException e) {
210
			// Log.severe("Error while closing R: " + e.getLocalizedMessage());
211
			// Log.printStackTrace(e);
212
			// }
213
			// }
214
			// // Mac, Linux
215
			// else {
216
			// try {
217
			// Process p = Runtime.getRuntime().exec(
218
			// "kill -9 `lsof -t -a -c Rserve-bin.so -i : " + RPreferences.getInstance().getInt(RPreferences.PORT) + "`"); //$NON-NLS-1$ //$NON-NLS-2$
219
			// p.waitFor();
220
			// started = false;
221
			// } catch (IOException e) {
222
			// Log.severe(RCoreMessages.error_failedToKillRServe3 + Log.toString(e));
223
			// org.txm.utils.logger.Log.printStackTrace(e);
224
			// } catch (InterruptedException e) {
225
			// Log.severe("Error while closing R: " + e.getLocalizedMessage());
226
			// org.txm.utils.logger.Log.printStackTrace(e);
227
			// }
228
			// }
177
			if (OSDetector.isFamilyWindows()) {
178
				try {
179
					String execName = RPreferences.getInstance().getString(RPreferences.PATH_TO_EXECUTABLE);
180
					File f = new File(execName);
181
					String cmd = NLS.bind("taskkill /IM {0} /F", f.getName()); //$NON-NLS-1$
182
					//String cmd = "cmd /c FOR /F \"tokens=5 delims= \" %P IN ('netstat -ano ^| findstr :6330 ^| findstr LISTENING') DO taskkill /F /PID %P"; //$NON-NLS-1$
183
					Log.finest("Executing command: " + cmd);
184

  
185
					Process p = Runtime.getRuntime().exec(cmd);
186

  
187
					// consumes streams
188
					StreamHog errStream = new StreamHog(p.getErrorStream(), true);
189
					errStream.setName("ts-kill-r-err");
190
					StreamHog inStream = new StreamHog(p.getInputStream(), true);
191
					inStream.setName("ts-kill-r");
192

  
193
					p.waitFor();
194
					started = false;
195
				} catch (IOException e) {
196
					Log.severe("Error: " + Log.toString(e));
197
					try {
198

  
199
						String cmd = NLS.bind("cmd /c FOR /F \"tokens=5 delims= \" %P IN (''netstat -ano ^| findstr :{0} ^| findstr LISTENING'') DO tskill %P", RPreferences.getInstance().getInt(RPreferences.PORT)); //$NON-NLS-1$
200
						Log.finest("Executing command: " + cmd);
201

  
202
						Process p = Runtime.getRuntime().exec(
203
								//"tskill Rserve.exe"); //$NON-NLS-1$
204
								cmd
205
								);
206
						p.waitFor();
207
						started = false;
208
					} catch (Exception e3) {
209
						Log.printStackTrace(e3);
210
					}
211
				}
212
				catch (InterruptedException e) {
213
					Log.severe(NLS.bind("Error while closing R: {0}", e.getLocalizedMessage()));
214
					Log.printStackTrace(e);
215
				}
216
			}
217
			// Mac, Linux
218
			else {
219
				try {
220
					Process p = Runtime.getRuntime().exec(
221
							NLS.bind("kill -9 `lsof -t -a -c Rserve-bin.so -i : {0}`", RPreferences.getInstance().getInt(RPreferences.PORT))); //$NON-NLS-1$ //$NON-NLS-2$
222
					p.waitFor();
223
					started = false;
224
				} catch (Exception e) {
225
					Log.severe(NLS.bind("Error while closing R: {0}", e.getLocalizedMessage()));
226
					org.txm.utils.logger.Log.printStackTrace(e);
227
				}
228
			}
229 229
		}
230 230
	}
231
	
232
	
233
	
231

  
232

  
233

  
234 234
	/**
235 235
	 * Returns the running state of the stats engine.
236 236
	 */
237 237
	public static boolean isStarted() {
238 238
		return started;
239 239
	}
240
	
241
	
240

  
241

  
242 242
	/**
243 243
	 * Checks if the engine is mandatory.
244 244
	 * 
......
247 247
	public static boolean isMandatory() {
248 248
		return mandatory;
249 249
	}
250
	
251
	
250

  
251

  
252 252
	@Override
253 253
	public boolean isRunning() {
254 254
		return started;
255 255
	}
256
	
257
	
256

  
257

  
258 258
	@Override
259 259
	public boolean initialize() {
260 260
		return true;
261 261
	}
262
	
262

  
263 263
	@Override
264 264
	public String getName() {
265 265
		return "R";

Formats disponibles : Unified diff