Revision 925

tmp/org.txm.utils/src/org/txm/utils/StreamConsumer.java (revision 925)
1
package org.txm.utils;
2

  
3
import java.io.BufferedReader;
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.io.InputStreamReader;
7

  
8
import org.txm.utils.logger.Log;
9

  
10
/**
11
 * Consumer for dumping stdout and stderr from a native process.
12
 * @author sjacquot
13
 *
14
 */
15
public class StreamConsumer extends Thread {
16
    
17
	InputStream is;
18
	int type;
19
    
20
    public StreamConsumer(InputStream is, int type) {
21
        this.is = is;
22
        this.type = type;
23
    }
24
    
25
    @Override
26
    public void run() {
27
        try {
28
            InputStreamReader isr = new InputStreamReader(this.is);
29
            BufferedReader br = new BufferedReader(isr);
30
            String line = null;
31
            while ((line = br.readLine()) != null) {
32
            	if(this.type == 0)	{
33
            		Log.finest(line);
34
            	}
35
            	else	{
36
            		Log.severe(line);
37
            	}
38
            }
39
        }
40
        catch (IOException e) {
41
        	e.printStackTrace();  
42
        }
43
    }
44
}
tmp/org.txm.utils/src/org/txm/utils/___StreamConsumer.java (revision 925)
1
package org.txm.utils;
2

  
3
import java.io.BufferedReader;
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.io.InputStreamReader;
7

  
8
import org.txm.utils.logger.Log;
9

  
10
/**
11
 * Consumer for dumping stdout and stderr from a native process.
12
 * @author sjacquot
13
 *
14
 */
15
public class ___StreamConsumer extends Thread {
16
    
17
	InputStream is;
18
	int type;
19
    
20
    public ___StreamConsumer(InputStream is, int type) {
21
        this.is = is;
22
        this.type = type;
23
    }
24
    
25
    @Override
26
    public void run() {
27
        try {
28
            InputStreamReader isr = new InputStreamReader(this.is);
29
            BufferedReader br = new BufferedReader(isr);
30
            String line = null;
31
            while ((line = br.readLine()) != null) {
32
            	if(this.type == 0)	{
33
            		Log.finest(line);
34
            	}
35
            	else	{
36
            		Log.severe(line);
37
            	}
38
            }
39
        }
40
        catch (IOException e) {
41
        	e.printStackTrace();  
42
        }
43
    }
44
}
0 45

  
tmp/org.txm.statsengine.r.core/src/org/txm/statsengine/r/core/RStatsEngine.java (revision 925)
9 9
import org.txm.statsengine.r.core.messages.RCoreMessages;
10 10
import org.txm.statsengine.r.core.preferences.RPreferences;
11 11
import org.txm.utils.OSDetector;
12
import org.txm.utils.StreamConsumer;
12
import org.txm.utils.___StreamConsumer;
13 13
import org.txm.utils.logger.Log;
14 14

  
15 15
/**
......
93 93

  
94 94
	
95 95

  
96

  
97
	@Override
98
	public boolean stop() {
99
		try {
100
			RWorkspace.shutdown();
101
			RStatsEngine.kill();
102
			started = false;
103
		} catch (Exception e) {
104
			Log.severe("Error while closing R.");
105
			e.printStackTrace();
106
			return false;
107
		}
108
		return true;
109
	}
110

  
111

  
96 112
	
97 113
	/**
98 114
	 * kill Rserve process.
......
110 126
				e1.printStackTrace();
111 127
			}
112 128
			
113
			// Windows OS
114
			if (OSDetector.isFamilyWindows()) {
115
				try {
116
					String cmd = "taskkill /IM Rterm.exe /F"; //$NON-NLS-1$
117
					//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$ 
118
					Log.finest("Executing command: " + cmd);
119

  
120
					Process p = Runtime.getRuntime().exec(
121
							//"taskkill /IM Rserve.exe /F"); //$NON-NLS-1$
122
							cmd
123
							);
124

  
125
					// consumes streams
126
		            new StreamConsumer(p.getErrorStream(), -1).start();;            
127
		            new StreamConsumer(p.getInputStream(), 0).start();
128
		            
129
					p.waitFor();
130
					started = false;
131
				} catch (IOException e) {
132
					Log.severe(RCoreMessages.error_failedToKillRServe+Log.toString(e));
133
					System.out.println(RCoreMessages.error_failedToKillRServe+Log.toString(e));
134
					try {
135
						
136
						String cmd = "cmd /c FOR /F \"tokens=5 delims= \" %P IN ('netstat -ano ^| findstr :6330 ^| findstr LISTENING') DO tskill %P"; //$NON-NLS-1$
137
						Log.finest("Executing command: " + cmd);
138
						
139
						Process p = Runtime.getRuntime().exec(
140
								//"tskill Rserve.exe"); //$NON-NLS-1$
141
								cmd
142
								);
143
						p.waitFor();
144
						started = false;
145
					} catch (IOException e2) {
146
						Log.severe(RCoreMessages.error_failedToKillRServe2+Log.toString(e2));
147
						Log.printStackTrace(e2);
148
					} catch (InterruptedException e3) {
149
						Log.severe(Log.toString(e3));
150
						Log.printStackTrace(e3);
151
					}
152
				}
153
				catch (InterruptedException e) {
154
					Log.severe("Error while closing R: " + e.getLocalizedMessage());
155
					Log.printStackTrace(e);
156
				}
157
			}
158
			// Mac, Linux
159
			else {
160
				try {
161
					Process p = Runtime.getRuntime().exec(
162
							"kill -9 `lsof -t -a -c Rserve-bin.so -i : " + TXMPreferences.getInt(RPreferences.PORT, RPreferences.PREFERENCES_NODE) + "`"); //$NON-NLS-1$ //$NON-NLS-2$
163
					p.waitFor();
164
					started = false;
165
				} catch (IOException e) {
166
					Log.severe(RCoreMessages.error_failedToKillRServe3 + Log.toString(e));
167
					System.out.println(RCoreMessages.error_failedToKillRServe3 + Log.toString(e));
168
					org.txm.utils.logger.Log.printStackTrace(e);
169
				} catch (InterruptedException e) {
170
					Log.severe("Error while closing R: " + e.getLocalizedMessage());
171
					System.out.println(Log.toString(e));
172
					org.txm.utils.logger.Log.printStackTrace(e);
173
				}
174
			}
129
			// FIXME: 
130
//			// Windows OS
131
//			if (OSDetector.isFamilyWindows()) {
132
//				try {
133
//					String cmd = "taskkill /IM Rterm.exe /F"; //$NON-NLS-1$
134
//					//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$ 
135
//					Log.finest("Executing command: " + cmd);
136
//
137
//					Process p = Runtime.getRuntime().exec(
138
//							//"taskkill /IM Rserve.exe /F"); //$NON-NLS-1$
139
//							cmd
140
//							);
141
//
142
//					// consumes streams
143
//		            new StreamConsumer(p.getErrorStream(), -1).start();;            
144
//		            new StreamConsumer(p.getInputStream(), 0).start();
145
//		            
146
//					p.waitFor();
147
//					started = false;
148
//				} catch (IOException e) {
149
//					Log.severe(RCoreMessages.error_failedToKillRServe + Log.toString(e));
150
//					try {
151
//						
152
//						String cmd = "cmd /c FOR /F \"tokens=5 delims= \" %P IN ('netstat -ano ^| findstr :6330 ^| findstr LISTENING') DO tskill %P"; //$NON-NLS-1$
153
//						Log.finest("Executing command: " + cmd);
154
//						
155
//						Process p = Runtime.getRuntime().exec(
156
//								//"tskill Rserve.exe"); //$NON-NLS-1$
157
//								cmd
158
//								);
159
//						p.waitFor();
160
//						started = false;
161
//					} catch (IOException e2) {
162
//						Log.severe(RCoreMessages.error_failedToKillRServe2+Log.toString(e2));
163
//						Log.printStackTrace(e2);
164
//					} catch (InterruptedException e3) {
165
//						Log.severe(Log.toString(e3));
166
//						Log.printStackTrace(e3);
167
//					}
168
//				}
169
//				catch (InterruptedException e) {
170
//					Log.severe("Error while closing R: " + e.getLocalizedMessage());
171
//					Log.printStackTrace(e);
172
//				}
173
//			}
174
//			// Mac, Linux
175
//			else {
176
//				try {
177
//					Process p = Runtime.getRuntime().exec(
178
//							"kill -9 `lsof -t -a -c Rserve-bin.so -i : " + TXMPreferences.getInt(RPreferences.PORT, RPreferences.PREFERENCES_NODE) + "`"); //$NON-NLS-1$ //$NON-NLS-2$
179
//					p.waitFor();
180
//					started = false;
181
//				} catch (IOException e) {
182
//					Log.severe(RCoreMessages.error_failedToKillRServe3 + Log.toString(e));
183
//					org.txm.utils.logger.Log.printStackTrace(e);
184
//				} catch (InterruptedException e) {
185
//					Log.severe("Error while closing R: " + e.getLocalizedMessage());
186
//					org.txm.utils.logger.Log.printStackTrace(e);
187
//				}
188
//			}
175 189
		}
176 190
	}
177 191
	
178 192
	
179
	/**
180
	 * Shut the stats engine down.
181
	 */
182
	public static void shutdown() {
183
		try {
184
			RWorkspace.shutdown();
185
			RStatsEngine.kill();
186
			started = false;
187
		} catch (Exception e) {
188
			Log.severe("Error while closing R.");
189
			e.printStackTrace();
190
		}
191
	}
192 193
	
193 194
	/**
194 195
	 * Returns the running state of the stats engine.
......
218 219
		return true;
219 220
	}
220 221

  
221

  
222 222
	@Override
223
	public boolean stop() {
224
		kill();
225
		return true;
226
	}
227

  
228

  
229
	@Override
230 223
	public String getName() {
231 224
		return "R";
232 225
	}
tmp/org.txm.statsengine.r.core/src/org/txm/statsengine/r/core/RWorkspace.java (revision 925)
453 453
			// application,
454 454
			// the connection has never been initialized.
455 455

  
456
			if (connection != null) {
457
				if (connection.isConnected()) {
458
					// connection.close(); // No: invoquing "close()" make
459
					// "shutdown()" to throw a "not connected" exception.
460
					connection.shutdown();
461
					connection = null; // warning, after shuting down the
462
					// server, the connection appears still
463
					// ok but using it yield to a
464
					// "Broken pipe" exception.
465
				}
456
			if (connection != null && connection.isConnected()) {
457
				// connection.close(); // No: invoquing "close()" make
458
				// "shutdown()" to throw a "not connected" exception.
459
				connection.shutdown();
460
				connection.close();
461
				connection = null; // warning, after shuting down the
462
				// server, the connection appears still
463
				// ok but using it yield to a
464
				// "Broken pipe" exception.
466 465
			}
467
			if (RserveProcess != null) RserveProcess.destroy();
468 466
		} catch (RserveException e) {
469 467
			throw new RWorkspaceException(e);
470 468
		}

Also available in: Unified diff