Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 domain.server; | 5 library domain.server; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/analysis_server.dart'; | 7 import 'package:analysis_server/src/analysis_server.dart'; |
| 8 import 'package:analysis_server/src/protocol.dart'; | 8 import 'package:analysis_server/src/protocol.dart'; |
| 9 import 'package:analyzer/src/generated/engine.dart'; | |
| 10 import 'package:analyzer/src/generated/java_io.dart'; | |
| 11 import 'package:analyzer/src/generated/sdk_io.dart'; | |
| 12 import 'package:analyzer/src/generated/source.dart'; | |
| 13 import 'package:analyzer/src/generated/source_io.dart'; | |
| 14 | 9 |
| 15 /** | 10 /** |
| 16 * Instances of the class [ServerDomainHandler] implement a [RequestHandler] | 11 * Instances of the class [ServerDomainHandler] implement a [RequestHandler] |
| 17 * that handles requests in the server domain. | 12 * that handles requests in the server domain. |
| 18 */ | 13 */ |
| 19 class ServerDomainHandler implements RequestHandler { | 14 class ServerDomainHandler implements RequestHandler { |
| 20 /** | 15 /** |
| 21 * The name of the server.createContext request. | 16 * The name of the `server.version` request. |
|
Brian Wilkerson
2014/05/24 15:19:49
'version' --> 'getVersion'
scheglov
2014/05/24 15:34:42
Done.
| |
| 22 */ | 17 */ |
| 23 static const String CREATE_CONTEXT_METHOD = 'server.createContext'; | 18 static const String GET_VERSION_METHOD = 'server.getVersion'; |
| 24 | 19 |
| 25 /** | 20 /// The name of the `server.setSubscriptions` request. |
| 26 * The name of the server.deleteContext request. | 21 static const String SET_SUBSCRIPTIONS_METHOD = 'server.setSubscriptions'; |
| 27 */ | |
| 28 static const String DELETE_CONTEXT_METHOD = 'server.deleteContext'; | |
| 29 | 22 |
| 30 /** | 23 /// The name of the `server.shutdown` request. |
| 31 * The name of the server.shutdown request. | |
| 32 */ | |
| 33 static const String SHUTDOWN_METHOD = 'server.shutdown'; | 24 static const String SHUTDOWN_METHOD = 'server.shutdown'; |
| 34 | 25 |
| 35 /** | 26 /// The name of the `subscriptions` parameter. |
| 36 * The name of the server.version request. | 27 static const String SUBSCRIPTIONS_PARAMETER = 'subscriptions'; |
| 37 */ | |
| 38 static const String VERSION_METHOD = 'server.version'; | |
| 39 | |
| 40 /** | |
| 41 * The name of the packageMap parameter. | |
| 42 */ | |
| 43 static const String PACKAGE_MAP_PARAM = 'packageMap'; | |
| 44 | |
| 45 /** | |
| 46 * The name of the sdkDirectory parameter. | |
| 47 */ | |
| 48 static const String SDK_DIRECTORY_PARAM = 'sdkDirectory'; | |
| 49 | 28 |
| 50 /** | 29 /** |
| 51 * The name of the version result value. | 30 * The name of the version result value. |
| 52 */ | 31 */ |
| 53 static const String VERSION_RESULT = 'version'; | 32 static const String VERSION_RESULT = 'version'; |
| 54 | 33 |
| 55 /** | 34 /** |
| 56 * The analysis server that is using this handler to process requests. | 35 * The analysis server that is using this handler to process requests. |
| 57 */ | 36 */ |
| 58 final AnalysisServer server; | 37 final AnalysisServer server; |
| 59 | 38 |
| 60 /** | 39 /** |
| 61 * Initialize a newly created handler to handle requests for the given [server ]. | 40 * Initialize a newly created handler to handle requests for the given [server ]. |
| 62 */ | 41 */ |
| 63 ServerDomainHandler(this.server); | 42 ServerDomainHandler(this.server); |
| 64 | 43 |
| 65 @override | 44 @override |
| 66 Response handleRequest(Request request) { | 45 Response handleRequest(Request request) { |
| 67 try { | 46 try { |
| 68 String requestName = request.method; | 47 String requestName = request.method; |
| 69 if (requestName == CREATE_CONTEXT_METHOD) { | 48 if (requestName == GET_VERSION_METHOD) { |
| 70 return createContext(request); | 49 return getVersion(request); |
| 71 } else if (requestName == DELETE_CONTEXT_METHOD) { | 50 } else if (requestName == SET_SUBSCRIPTIONS_METHOD) { |
| 72 return deleteContext(request); | 51 return setSubscriptions(request); |
| 73 } else if (requestName == SHUTDOWN_METHOD) { | 52 } else if (requestName == SHUTDOWN_METHOD) { |
| 74 return shutdown(request); | 53 return shutdown(request); |
| 75 } else if (requestName == VERSION_METHOD) { | |
| 76 return version(request); | |
| 77 } | 54 } |
| 78 } on RequestFailure catch (exception) { | 55 } on RequestFailure catch (exception) { |
| 79 return exception.response; | 56 return exception.response; |
| 80 } | 57 } |
| 81 return null; | 58 return null; |
| 82 } | 59 } |
| 83 | 60 |
| 84 /** | 61 /// Subscribe for services. |
| 85 * Create a new context in which analysis can be performed. The context that | 62 /// |
| 86 * is created will persist until server.deleteContext is used to delete it. | 63 /// All previous subscriptions are replaced by the given set of subscriptions. |
| 87 * Clients, therefore, are responsible for managing the lifetime of contexts. | 64 Response setSubscriptions(Request request) { |
| 88 */ | 65 var subDatum = request.getRequiredParameter(SUBSCRIPTIONS_PARAMETER); |
|
Brian Wilkerson
2014/05/24 15:19:49
Local variables should have full type information.
scheglov
2014/05/24 15:34:42
Done.
| |
| 89 Response createContext(Request request) { | 66 server.serverServices = subDatum.asEnumSet(ServerService.VALUES); |
| 90 String sdkDirectory = request.getRequiredParameter(SDK_DIRECTORY_PARAM).asSt ring(); | 67 return new Response(request.id); |
| 91 Map<String, String> packageMap = request.getParameter(PACKAGE_MAP_PARAM, {}) .asStringMap(); | |
| 92 | |
| 93 String contextId = request.getRequiredParameter(AnalysisServer.CONTEXT_ID_PA RAM).asString(); | |
| 94 if (server.contextMap.containsKey(contextId)) { | |
| 95 return new Response.contextAlreadyExists(request); | |
| 96 } | |
| 97 AnalysisContext context = AnalysisEngine.instance.createAnalysisContext(); | |
| 98 // TODO(brianwilkerson) Use the information from the request to set the | |
| 99 // source factory in the context. | |
| 100 DirectoryBasedDartSdk sdk; | |
| 101 try { | |
| 102 sdk = new DirectoryBasedDartSdk(new JavaFile(sdkDirectory)); | |
| 103 } on Exception catch (e) { | |
| 104 // TODO what error code should be returned here? | |
| 105 return new Response(request.id, new RequestError( | |
| 106 RequestError.CODE_SDK_ERROR, 'Failed to access sdk: $e')); | |
| 107 } | |
| 108 context.sourceFactory = new SourceFactory([ | |
| 109 new DartUriResolver(sdk), | |
| 110 new FileUriResolver(), | |
| 111 // new PackageUriResolver(), | |
| 112 ]); | |
| 113 server.contextMap[contextId] = context; | |
| 114 server.contextIdMap[context] = contextId; | |
| 115 | |
| 116 Response response = new Response(request.id); | |
| 117 return response; | |
| 118 } | 68 } |
| 119 | 69 |
| 120 /** | 70 // TODO(scheglov) remove or move to the 'analysis' domain |
|
Brian Wilkerson
2014/05/24 15:19:49
Is there a question about whether we want these AP
scheglov
2014/05/24 15:34:42
We don't want these API's.
But I'd not like to rem
| |
| 121 * Delete the context with the given id. Future attempts to use the context id | 71 // /** |
| 122 * will result in an error being returned. | 72 // * Create a new context in which analysis can be performed. The context that |
| 123 */ | 73 // * is created will persist until server.deleteContext is used to delete it. |
| 124 Response deleteContext(Request request) { | 74 // * Clients, therefore, are responsible for managing the lifetime of contexts . |
| 125 String contextId = request.getRequiredParameter(AnalysisServer.CONTEXT_ID_PA RAM).asString(); | 75 // */ |
| 126 | 76 // Response createContext(Request request) { |
| 127 AnalysisContext removedContext = server.contextMap.remove(contextId); | 77 // String sdkDirectory = request.getRequiredParameter(SDK_DIRECTORY_PARAM).as String(); |
| 128 if (removedContext == null) { | 78 // Map<String, String> packageMap = request.getParameter(PACKAGE_MAP_PARAM, { }).asStringMap(); |
| 129 return new Response.contextDoesNotExist(request); | 79 // |
| 130 } | 80 // String contextId = request.getRequiredParameter(AnalysisServer.CONTEXT_ID_ PARAM).asString(); |
| 131 server.contextIdMap.remove(removedContext); | 81 // if (server.contextMap.containsKey(contextId)) { |
| 132 Response response = new Response(request.id); | 82 // return new Response.contextAlreadyExists(request); |
| 133 return response; | 83 // } |
| 134 } | 84 // AnalysisContext context = AnalysisEngine.instance.createAnalysisContext(); |
| 85 // // TODO(brianwilkerson) Use the information from the request to set the | |
| 86 // // source factory in the context. | |
| 87 // DirectoryBasedDartSdk sdk; | |
| 88 // try { | |
| 89 // sdk = new DirectoryBasedDartSdk(new JavaFile(sdkDirectory)); | |
| 90 // } on Exception catch (e) { | |
| 91 // // TODO what error code should be returned here? | |
| 92 // return new Response(request.id, new RequestError( | |
| 93 // RequestError.CODE_SDK_ERROR, 'Failed to access sdk: $e')); | |
| 94 // } | |
| 95 // context.sourceFactory = new SourceFactory([ | |
| 96 // new DartUriResolver(sdk), | |
| 97 // new FileUriResolver(), | |
| 98 // // new PackageUriResolver(), | |
| 99 // ]); | |
| 100 // server.contextMap[contextId] = context; | |
| 101 // server.contextIdMap[context] = contextId; | |
| 102 // | |
| 103 // Response response = new Response(request.id); | |
| 104 // return response; | |
| 105 // } | |
| 106 // | |
| 107 // /** | |
| 108 // * Delete the context with the given id. Future attempts to use the context id | |
| 109 // * will result in an error being returned. | |
| 110 // */ | |
| 111 // Response deleteContext(Request request) { | |
| 112 // String contextId = request.getRequiredParameter(AnalysisServer.CONTEXT_ID_ PARAM).asString(); | |
| 113 // | |
| 114 // AnalysisContext removedContext = server.contextMap.remove(contextId); | |
| 115 // if (removedContext == null) { | |
| 116 // return new Response.contextDoesNotExist(request); | |
| 117 // } | |
| 118 // server.contextIdMap.remove(removedContext); | |
| 119 // Response response = new Response(request.id); | |
| 120 // return response; | |
| 121 // } | |
| 135 | 122 |
| 136 /** | 123 /** |
| 137 * Cleanly shutdown the analysis server. | 124 * Cleanly shutdown the analysis server. |
| 138 */ | 125 */ |
| 139 Response shutdown(Request request) { | 126 Response shutdown(Request request) { |
| 140 server.running = false; | 127 server.running = false; |
| 141 Response response = new Response(request.id); | 128 Response response = new Response(request.id); |
| 142 return response; | 129 return response; |
| 143 } | 130 } |
| 144 | 131 |
| 145 /** | 132 /** |
| 146 * Return the version number of the analysis server. | 133 * Return the version number of the analysis server. |
| 147 */ | 134 */ |
| 148 Response version(Request request) { | 135 Response getVersion(Request request) { |
| 149 Response response = new Response(request.id); | 136 Response response = new Response(request.id); |
| 150 response.setResult(VERSION_RESULT, '0.0.1'); | 137 response.setResult(VERSION_RESULT, '0.0.1'); |
| 151 return response; | 138 return response; |
| 152 } | 139 } |
| 153 } | 140 } |
| OLD | NEW |