OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library input.transformer; | 5 library input.transformer; |
6 | 6 |
7 import 'dart:convert'; | 7 import 'dart:convert'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import 'package:analysis_server/src/constants.dart'; | 10 import 'package:analysis_server/src/constants.dart'; |
11 import 'package:analysis_server/src/protocol.dart'; | 11 import 'package:analysis_server/src/protocol.dart'; |
12 import 'package:analyzer/src/generated/java_engine.dart'; | 12 import 'package:analyzer/src/generated/java_engine.dart'; |
13 import 'package:logging/logging.dart'; | 13 import 'package:logging/logging.dart'; |
| 14 import 'package:path/path.dart' as path; |
14 | 15 |
15 import 'instrumentation_input_converter.dart'; | 16 import 'instrumentation_input_converter.dart'; |
16 import 'log_file_input_converter.dart'; | 17 import 'log_file_input_converter.dart'; |
17 import 'operation.dart'; | 18 import 'operation.dart'; |
18 | 19 |
19 /** | 20 /** |
20 * Common input converter superclass for sharing implementation. | 21 * Common input converter superclass for sharing implementation. |
21 */ | 22 */ |
22 abstract class CommonInputConverter extends Converter<String, Operation> { | 23 abstract class CommonInputConverter extends Converter<String, Operation> { |
23 static final ERROR_PREFIX = 'Server responded with an error: '; | 24 static final ERROR_PREFIX = 'Server responded with an error: '; |
24 final Logger logger = new Logger('InstrumentationInputConverter'); | 25 final Logger logger = new Logger('InstrumentationInputConverter'); |
25 final Set<String> eventsSeen = new Set<String>(); | 26 final Set<String> eventsSeen = new Set<String>(); |
26 | 27 |
27 /** | 28 /** |
28 * A mapping from request/response id to expected error message. | 29 * A mapping from request/response id to expected error message. |
29 */ | 30 */ |
30 final Map<String, dynamic> expectedErrors = new Map<String, dynamic>(); | 31 final Map<String, dynamic> expectedErrors = new Map<String, dynamic>(); |
31 | 32 |
32 /** | 33 /** |
| 34 * A mapping of current overlay content |
| 35 * parallel to what is in the analysis server |
| 36 * so that we can update the file system. |
| 37 */ |
| 38 final Map<String, String> overlays = new Map<String, String>(); |
| 39 |
| 40 /** |
| 41 * The prefix used to determine if a request parameter is a file path. |
| 42 */ |
| 43 final String rootPrefix = path.rootPrefix(path.current); |
| 44 |
| 45 /** |
33 * A mapping of source path prefixes | 46 * A mapping of source path prefixes |
34 * from location where instrumentation or log file was generated | 47 * from location where instrumentation or log file was generated |
35 * to the target location of the source using during performance measurement. | 48 * to the target location of the source using during performance measurement. |
36 */ | 49 */ |
37 final Map<String, String> srcPathMap; | 50 final Map<String, String> srcPathMap; |
38 | 51 |
39 /** | 52 /** |
40 * A mapping of current overlay content | 53 * The root directory for all source being modified |
41 * parallel to what is in the analysis server | 54 * during performance measurement. |
42 * so that we can update the file system. | |
43 */ | 55 */ |
44 final Map<String, String> overlays = new Map<String, String>(); | 56 final String tmpSrcDirPath; |
45 | 57 |
46 CommonInputConverter(this.srcPathMap); | 58 CommonInputConverter(this.tmpSrcDirPath, this.srcPathMap); |
47 | 59 |
48 /** | 60 /** |
49 * Return an operation for the notification or `null` if none. | 61 * Return an operation for the notification or `null` if none. |
50 */ | 62 */ |
51 Operation convertNotification(Map<String, dynamic> json) { | 63 Operation convertNotification(Map<String, dynamic> json) { |
52 String event = json['event']; | 64 String event = json['event']; |
53 if (event == SERVER_STATUS) { | 65 if (event == SERVER_STATUS) { |
54 // {"event":"server.status","params":{"analysis":{"isAnalyzing":false}}} | 66 // {"event":"server.status","params":{"analysis":{"isAnalyzing":false}}} |
55 Map<String, dynamic> params = json['params']; | 67 Map<String, dynamic> params = json['params']; |
56 if (params != null) { | 68 if (params != null) { |
(...skipping 12 matching lines...) Expand all Loading... |
69 } | 81 } |
70 return null; | 82 return null; |
71 } | 83 } |
72 | 84 |
73 /** | 85 /** |
74 * Return an operation for the request or `null` if none. | 86 * Return an operation for the request or `null` if none. |
75 */ | 87 */ |
76 Operation convertRequest(Map<String, dynamic> origJson) { | 88 Operation convertRequest(Map<String, dynamic> origJson) { |
77 Map<String, dynamic> json = translateSrcPaths(origJson); | 89 Map<String, dynamic> json = translateSrcPaths(origJson); |
78 String method = json['method']; | 90 String method = json['method']; |
79 if (method == ANALYSIS_GET_HOVER || | |
80 method == ANALYSIS_SET_ANALYSIS_ROOTS || | |
81 method == ANALYSIS_SET_PRIORITY_FILES || | |
82 method == ANALYSIS_SET_SUBSCRIPTIONS || | |
83 method == ANALYSIS_UPDATE_OPTIONS || | |
84 method == COMPLETION_GET_SUGGESTIONS || | |
85 method == EDIT_GET_ASSISTS || | |
86 method == EDIT_GET_AVAILABLE_REFACTORINGS || | |
87 method == EDIT_GET_FIXES || | |
88 method == EDIT_GET_REFACTORING || | |
89 method == EDIT_SORT_MEMBERS || | |
90 method == EXECUTION_CREATE_CONTEXT || | |
91 method == EXECUTION_DELETE_CONTEXT || | |
92 method == EXECUTION_MAP_URI || | |
93 method == EXECUTION_SET_SUBSCRIPTIONS || | |
94 method == SERVER_GET_VERSION || | |
95 method == SERVER_SET_SUBSCRIPTIONS) { | |
96 return new RequestOperation(this, json); | |
97 } | |
98 // Sanity check operations that modify source | 91 // Sanity check operations that modify source |
99 // to ensure that the operation is on source in temp space | 92 // to ensure that the operation is on source in temp space |
100 if (method == ANALYSIS_UPDATE_CONTENT) { | 93 if (method == ANALYSIS_UPDATE_CONTENT) { |
101 try { | 94 try { |
102 validateSrcPaths(json); | 95 validateSrcPaths(json); |
103 } catch (e, s) { | 96 } catch (e, s) { |
104 throw new AnalysisException('invalid src path in update request\n$json', | 97 throw new AnalysisException('invalid src path in update request\n$json', |
105 new CaughtException(e, s)); | 98 new CaughtException(e, s)); |
106 } | 99 } |
107 // Track overlays in parallel with the analysis server | 100 // Track overlays in parallel with the analysis server |
(...skipping 19 matching lines...) Expand all Loading... |
127 throw 'expected cached overlay content\n$json'; | 120 throw 'expected cached overlay content\n$json'; |
128 } | 121 } |
129 validateSrcPaths(path); | 122 validateSrcPaths(path); |
130 new File(path).writeAsStringSync(content); | 123 new File(path).writeAsStringSync(content); |
131 } else { | 124 } else { |
132 throw 'unknown overlay change $change\n$json'; | 125 throw 'unknown overlay change $change\n$json'; |
133 } | 126 } |
134 }); | 127 }); |
135 return new RequestOperation(this, json); | 128 return new RequestOperation(this, json); |
136 } | 129 } |
| 130 // TODO(danrubel) replace this with code |
| 131 // that just forwards the translated request |
| 132 if (method == ANALYSIS_GET_HOVER || |
| 133 method == ANALYSIS_SET_ANALYSIS_ROOTS || |
| 134 method == ANALYSIS_SET_PRIORITY_FILES || |
| 135 method == ANALYSIS_SET_SUBSCRIPTIONS || |
| 136 method == ANALYSIS_UPDATE_OPTIONS || |
| 137 method == COMPLETION_GET_SUGGESTIONS || |
| 138 method == EDIT_GET_ASSISTS || |
| 139 method == EDIT_GET_AVAILABLE_REFACTORINGS || |
| 140 method == EDIT_GET_FIXES || |
| 141 method == EDIT_GET_REFACTORING || |
| 142 method == EDIT_SORT_MEMBERS || |
| 143 method == EXECUTION_CREATE_CONTEXT || |
| 144 method == EXECUTION_DELETE_CONTEXT || |
| 145 method == EXECUTION_MAP_URI || |
| 146 method == EXECUTION_SET_SUBSCRIPTIONS || |
| 147 method == SEARCH_FIND_ELEMENT_REFERENCES || |
| 148 method == SERVER_GET_VERSION || |
| 149 method == SERVER_SET_SUBSCRIPTIONS) { |
| 150 return new RequestOperation(this, json); |
| 151 } |
137 throw 'unknown request: $method\n $json'; | 152 throw 'unknown request: $method\n $json'; |
138 } | 153 } |
139 | 154 |
140 /** | 155 /** |
141 * Determine if the given request is expected to fail | 156 * Determine if the given request is expected to fail |
142 * and log an exception if not. | 157 * and log an exception if not. |
143 */ | 158 */ |
144 void recordErrorResponse(Map<String, dynamic> jsonRequest, exception) { | 159 void recordErrorResponse(Map<String, dynamic> jsonRequest, exception) { |
145 var actualErr; | 160 var actualErr; |
146 if (exception is UnimplementedError) { | 161 if (exception is UnimplementedError) { |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 return json; | 231 return json; |
217 } | 232 } |
218 | 233 |
219 /** | 234 /** |
220 * Recursively verify that the source paths in the specified JSON | 235 * Recursively verify that the source paths in the specified JSON |
221 * only reference the temporary source used during performance measurement. | 236 * only reference the temporary source used during performance measurement. |
222 */ | 237 */ |
223 void validateSrcPaths(json) { | 238 void validateSrcPaths(json) { |
224 if (json is String) { | 239 if (json is String) { |
225 if (json != null && | 240 if (json != null && |
226 json.startsWith('/Users/') && | 241 path.isWithin(rootPrefix, json) && |
227 !srcPathMap.values.any((String prefix) => json.startsWith(prefix))) { | 242 !path.isWithin(tmpSrcDirPath, json)) { |
228 throw 'found path referencing source outside temp space\n$json'; | 243 throw 'found path referencing source outside temp space\n$json'; |
229 } | 244 } |
230 } else if (json is List) { | 245 } else if (json is List) { |
231 for (int i = json.length - 1; i >= 0; --i) { | 246 for (int i = json.length - 1; i >= 0; --i) { |
232 validateSrcPaths(json[i]); | 247 validateSrcPaths(json[i]); |
233 } | 248 } |
234 } else if (json is Map) { | 249 } else if (json is Map) { |
235 json.forEach((String key, value) { | 250 json.forEach((String key, value) { |
236 validateSrcPaths(key); | 251 validateSrcPaths(key); |
237 validateSrcPaths(value); | 252 validateSrcPaths(value); |
(...skipping 11 matching lines...) Expand all Loading... |
249 final Logger logger = new Logger('InputConverter'); | 264 final Logger logger = new Logger('InputConverter'); |
250 | 265 |
251 /** | 266 /** |
252 * A mapping of source path prefixes | 267 * A mapping of source path prefixes |
253 * from location where instrumentation or log file was generated | 268 * from location where instrumentation or log file was generated |
254 * to the target location of the source using during performance measurement. | 269 * to the target location of the source using during performance measurement. |
255 */ | 270 */ |
256 final Map<String, String> srcPathMap; | 271 final Map<String, String> srcPathMap; |
257 | 272 |
258 /** | 273 /** |
| 274 * The root directory for all source being modified |
| 275 * during performance measurement. |
| 276 */ |
| 277 final String tmpSrcDirPath; |
| 278 |
| 279 /** |
259 * The number of lines read before the underlying converter was determined | 280 * The number of lines read before the underlying converter was determined |
260 * or the end of file was reached. | 281 * or the end of file was reached. |
261 */ | 282 */ |
262 int headerLineCount = 0; | 283 int headerLineCount = 0; |
263 | 284 |
264 /** | 285 /** |
265 * The underlying converter used to translate lines into operations | 286 * The underlying converter used to translate lines into operations |
266 * or `null` if it has not yet been determined. | 287 * or `null` if it has not yet been determined. |
267 */ | 288 */ |
268 Converter<String, Operation> converter; | 289 Converter<String, Operation> converter; |
269 | 290 |
270 /** | 291 /** |
271 * [active] is `true` if converting lines to operations | 292 * [active] is `true` if converting lines to operations |
272 * or `false` if an exception has occurred. | 293 * or `false` if an exception has occurred. |
273 */ | 294 */ |
274 bool active = true; | 295 bool active = true; |
275 | 296 |
276 InputConverter(this.srcPathMap); | 297 InputConverter(this.tmpSrcDirPath, this.srcPathMap); |
277 | 298 |
278 @override | 299 @override |
279 Operation convert(String line) { | 300 Operation convert(String line) { |
280 if (!active) { | 301 if (!active) { |
281 return null; | 302 return null; |
282 } | 303 } |
283 if (converter != null) { | 304 if (converter != null) { |
284 try { | 305 try { |
285 return converter.convert(line); | 306 return converter.convert(line); |
286 } catch (e) { | 307 } catch (e) { |
287 active = false; | 308 active = false; |
288 rethrow; | 309 rethrow; |
289 } | 310 } |
290 } | 311 } |
291 if (headerLineCount == 20) { | 312 if (headerLineCount == 20) { |
292 throw 'Failed to determine input file format'; | 313 throw 'Failed to determine input file format'; |
293 } | 314 } |
294 if (InstrumentationInputConverter.isFormat(line)) { | 315 if (InstrumentationInputConverter.isFormat(line)) { |
295 converter = new InstrumentationInputConverter(srcPathMap); | 316 converter = new InstrumentationInputConverter(tmpSrcDirPath, srcPathMap); |
296 } else if (LogFileInputConverter.isFormat(line)) { | 317 } else if (LogFileInputConverter.isFormat(line)) { |
297 converter = new LogFileInputConverter(srcPathMap); | 318 converter = new LogFileInputConverter(tmpSrcDirPath, srcPathMap); |
298 } | 319 } |
299 if (converter != null) { | 320 if (converter != null) { |
300 return converter.convert(line); | 321 return converter.convert(line); |
301 } | 322 } |
302 logger.log(Level.INFO, 'skipped input line: $line'); | 323 logger.log(Level.INFO, 'skipped input line: $line'); |
303 return null; | 324 return null; |
304 } | 325 } |
305 | 326 |
306 @override | 327 @override |
307 _InputSink startChunkedConversion(outSink) { | 328 _InputSink startChunkedConversion(outSink) { |
(...skipping 13 matching lines...) Expand all Loading... |
321 if (op != null) { | 342 if (op != null) { |
322 outSink.add(op); | 343 outSink.add(op); |
323 } | 344 } |
324 } | 345 } |
325 | 346 |
326 @override | 347 @override |
327 void close() { | 348 void close() { |
328 outSink.close(); | 349 outSink.close(); |
329 } | 350 } |
330 } | 351 } |
OLD | NEW |