OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library domain.server; | |
6 | |
7 import 'package:analyzer/src/generated/engine.dart'; | |
8 import 'package:analyzer/src/generated/java_io.dart'; | |
9 import 'package:analyzer/src/generated/sdk_io.dart'; | |
10 import 'package:analyzer/src/generated/source.dart'; | |
11 import 'package:analyzer/src/generated/source_io.dart'; | |
12 | |
13 import 'analysis_server.dart'; | |
14 import 'protocol.dart'; | |
15 | |
16 /** | |
17 * Instances of the class [ServerDomainHandler] implement a [RequestHandler] | |
18 * that handles requests in the server domain. | |
19 */ | |
20 class ServerDomainHandler implements RequestHandler { | |
21 /** | |
22 * The name of the server.createContext request. | |
23 */ | |
24 static const String CREATE_CONTEXT_METHOD = 'server.createContext'; | |
25 | |
26 /** | |
27 * The name of the server.deleteContext request. | |
28 */ | |
29 static const String DELETE_CONTEXT_METHOD = 'server.deleteContext'; | |
30 | |
31 /** | |
32 * The name of the server.shutdown request. | |
33 */ | |
34 static const String SHUTDOWN_METHOD = 'server.shutdown'; | |
35 | |
36 /** | |
37 * The name of the server.version request. | |
38 */ | |
39 static const String VERSION_METHOD = 'server.version'; | |
40 | |
41 /** | |
42 * The name of the contextId parameter. | |
43 */ | |
44 static const String CONTEXT_ID_PARAM = 'contextId'; | |
45 | |
46 /** | |
47 * The name of the packageMap parameter. | |
48 */ | |
49 static const String PACKAGE_MAP_PARAM = 'packageMap'; | |
50 | |
51 /** | |
52 * The name of the sdkDirectory parameter. | |
53 */ | |
54 static const String SDK_DIRECTORY_PARAM = 'sdkDirectory'; | |
55 | |
56 /** | |
57 * The name of the contextId result value. | |
58 */ | |
59 static const String CONTEXT_ID_RESULT = 'contextId'; | |
60 | |
61 /** | |
62 * The name of the version result value. | |
63 */ | |
64 static const String VERSION_RESULT = 'version'; | |
65 | |
66 /** | |
67 * The analysis server that is using this handler to process requests. | |
68 */ | |
69 final AnalysisServer server; | |
70 | |
71 /** | |
72 * Initialize a newly created handler to handle requests for the given [server ]. | |
73 */ | |
74 ServerDomainHandler(this.server); | |
75 | |
76 @override | |
77 Response handleRequest(Request request) { | |
78 try { | |
79 String requestName = request.method; | |
80 if (requestName == CREATE_CONTEXT_METHOD) { | |
81 createContext(request); | |
82 } else if (requestName == DELETE_CONTEXT_METHOD) { | |
83 deleteContext(request); | |
84 } else if (requestName == SHUTDOWN_METHOD) { | |
85 shutdown(request); | |
86 } else if (requestName == VERSION_METHOD) { | |
87 version(request); | |
danrubel
2014/01/22 01:04:07
Do these need to return the response that is retur
Brian Wilkerson
2014/01/24 21:49:27
Yes, done.
| |
88 } | |
89 } on RequestFailure catch (exception) { | |
90 return exception.response; | |
91 } | |
92 return null; | |
93 } | |
94 | |
95 /** | |
96 * Create a new context in which analysis can be performed. The context that | |
97 * is created will persist until server.deleteContext is used to delete it. | |
98 * Clients, therefore, are responsible for managing the lifetime of contexts. | |
99 */ | |
100 Response createContext(Request request) { | |
101 String sdkDirectory = request.getRequiredParameter(SDK_DIRECTORY_PARAM); | |
102 Map<String, String> packageMap = request.getParameter(PACKAGE_MAP_PARAM); | |
103 | |
104 String baseContextId = new DateTime.now().millisecondsSinceEpoch.toRadixStri ng(16); | |
105 String contextId = baseContextId; | |
106 int index = 1; | |
107 while (server.contextMap.containsKey(contextId)) { | |
108 contextId = '$baseContextId-$index'; | |
109 } | |
110 AnalysisContext context = AnalysisEngine.instance.createAnalysisContext(); | |
111 // TODO(brianwilkerson) Use the information from the request to set the | |
112 // source factory in the context. | |
113 context.sourceFactory = new SourceFactory.con2([ | |
114 new DartUriResolver(new DirectoryBasedDartSdk(new JavaFile(sdkDirectory))) , | |
115 new FileUriResolver(), | |
116 // new PackageUriResolver(), | |
117 ]); | |
118 server.contextMap[contextId] = context; | |
119 | |
120 Response response = new Response(request.id); | |
121 response.setResult(CONTEXT_ID_RESULT, contextId); | |
122 return response; | |
123 } | |
124 | |
125 /** | |
126 * Delete the context with the given id. Future attempts to use the context id | |
127 * will result in an error being returned. | |
128 */ | |
129 Response deleteContext(Request request) { | |
130 String contextId = request.getRequiredParameter(CONTEXT_ID_PARAM); | |
131 | |
132 AnalysisContext removedContext = server.contextMap.remove(contextId); | |
133 if (removedContext == null) { | |
134 return new Response.contextDoesNotExist(request); | |
135 } | |
136 Response response = new Response(request.id); | |
137 return response; | |
138 } | |
139 | |
140 /** | |
141 * Cleanly shutdown the analysis server. | |
142 */ | |
143 Response shutdown(Request request) { | |
144 server.running = false; | |
145 Response response = new Response(request.id); | |
146 return response; | |
147 } | |
148 | |
149 /** | |
150 * Return the version number of the analysis server. | |
151 */ | |
152 Response version(Request request) { | |
153 Response response = new Response(request.id); | |
154 response.setResult(VERSION_RESULT, '0.0.1'); | |
155 return response; | |
156 } | |
157 } | |
OLD | NEW |