1 package rydeen;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import rydeen.spi.ProcessorService;
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public class Context{
58 private Environment env;
59 private List<String> targets = new ArrayList<String>();
60 private String destination;
61 private List<String> processorNames = new ArrayList<String>();
62 private Map<String, String> arguments = new HashMap<String, String>();
63
64
65
66
67 public Context(Environment env){
68 this.env = env;
69 }
70
71
72
73
74 public Context(Context context){
75 this(context.getEnvironment());
76 destination = context.getDestination();
77 arguments.putAll(context.arguments);
78 processorNames.addAll(context.processorNames);
79 targets.addAll(context.targets);
80 }
81
82
83
84
85 public Environment getEnvironment(){
86 return env;
87 }
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 public Processor[] getProcessors() throws ProcessorException{
105 String[] processorNames = getProcessorNames();
106 Processor[] processors = new Processor[processorNames.length];
107 Environment env = getEnvironment();
108 for(int i = 0; i < processors.length; i++){
109 ProcessorService service = env.getService(processorNames[i]);
110 if(service == null){
111 throw new UnknownProcessorException(processorNames[i] + " is not found");
112 }
113 processors[i] = service.getProcessor();
114 processors[i].setId(String.format("%s-%03d", processorNames[i], i + 1));
115 }
116 updateArguments(processors);
117 return processors;
118 }
119
120
121
122
123
124
125 public void addTarget(String arg){
126 targets.add(arg);
127 }
128
129
130
131
132
133
134
135 public void addProcessorName(String processorName){
136 processorNames.add(processorName);
137 }
138
139
140
141
142
143
144 public String[] getTargets(){
145 return targets.toArray(new String[targets.size()]);
146 }
147
148
149
150
151
152
153 public String getDestination(){
154 if(destination == null){
155 destination = ".";
156 }
157 return destination;
158 }
159
160
161
162
163 public int getProcessorCount(){
164 return processorNames.size();
165 }
166
167
168
169
170
171
172
173
174 public String[] getProcessorNames(){
175 return processorNames.toArray(new String[processorNames.size()]);
176 }
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 public Argument[] findArguments(String keyPrefix){
196 List<Argument> list = new ArrayList<Argument>();
197 for(Map.Entry<String, String> entry : arguments.entrySet()){
198 if(entry.getKey().startsWith(keyPrefix)){
199 list.add(new ArgumentImpl(entry.getKey(), entry.getValue()));
200 }
201 }
202 return list.toArray(new Argument[list.size()]);
203 }
204
205
206
207
208
209 public String getArgumentValue(String key){
210 if(key == null){
211 throw new NullPointerException();
212 }
213 return arguments.get(key);
214 }
215
216
217
218
219
220 public boolean hasArgument(String key){
221 if(key == null){
222 throw new NullPointerException();
223 }
224 return arguments.containsKey(key);
225 }
226
227
228
229
230
231
232 public void putArgument(String key, String value){
233 if(key == null){
234 throw new NullPointerException();
235 }
236 else{
237 arguments.put(key, value);
238 }
239 }
240
241
242
243
244
245
246 public void setDestination(String dest){
247 this.destination = dest;
248 }
249
250 private void updateArguments(Processor[] processors) throws ArgumentsConflictException{
251 Map<String, Map<String, Argument>> argmap = new HashMap<String, Map<String, Argument>>();
252 List<String> conflictList = new ArrayList<String>();
253
254 for(Processor processor: processors){
255 String id = processor.getId();
256 String number = id.substring(processor.getProcessorName().length() + 1);
257
258 updateArguments(id, id, argmap, findArguments(id + "."), conflictList);
259 updateArguments(number, id, argmap, findArguments(number + "."), conflictList);
260 }
261
262
263 for(Processor processor : processors){
264 String name = processor.getProcessorName();
265 updateArguments(name, processor.getId(), argmap,
266 findArguments(name + "."), conflictList);
267 }
268 if(conflictList.size() == 0){
269 for(Processor processor : processors){
270 Map<String, Argument> submap = argmap.get(processor.getId());
271 Arguments args = processor.getArguments();
272 if(submap != null){
273 for(Argument arg : submap.values()){
274 args.putValue(arg);
275 }
276 }
277 }
278 }
279 else{
280 throw new ArgumentsConflictException(
281 conflictList.toArray(new String[conflictList.size()]));
282 }
283 }
284
285 private void updateArguments(String prefix, String id,
286 Map<String, Map<String, Argument>> argmap, Argument[] args,
287 List<String> conflictList){
288 for(Argument arg : args){
289 Map<String, Argument> submap = argmap.get(id);
290 String key = arg.getName().substring(prefix.length() + 1);
291 if(submap == null){
292 submap = new HashMap<String, Argument>();
293 argmap.put(id, submap);
294 }
295 if(submap.get(key) != null){
296 Argument argument = submap.get(key);
297 String value = argument.getValue();
298 if((value != null && !value.equals(arg.getValue())) ||
299 (value == null && arg.getValue() != null)){
300 conflictList.add(id + "." + key);
301 }
302 }
303 else{
304 submap.put(key, new ArgumentImpl(key, arg.getValue()));
305 }
306 }
307 }
308 }