Skip to content

Package: RouterConfiguration$BuildingPhaseBuilder

RouterConfiguration$BuildingPhaseBuilder

Coverage

1: package model;
2:
3: import java.io.File;
4: import java.io.IOException;
5: import java.util.Collections;
6: import java.util.HashMap;
7: import java.util.Iterator;
8: import java.util.Locale;
9: import java.util.Map;
10: import java.util.Map.Entry;
11:
12: import controller.Controller;
13: import controller.buildingphase.ScanAndParseAssignmentFile;
14: import controller.checkingconditionphase.CheckConditions;
15: import generator.ArchiveGenerator;
16: import model.assignment.VariableAssignment;
17: import model.definition.AbstractVariableDefinition;
18: import model.definition.ConfigurationContext;
19: import model.definition.VariableContext;
20: import model.type.AssignmentFileManager;
21: import model.type.TypeDoesNotExistException;
22: import model.type.TypeExceptions;
23: import parser.ParserException;
24: import reader.PackageReader;
25: import reader.ReaderException;
26: import scanner.ScannerException;
27:
28: /**
29: * Represents the configuration of some RouterSoftware.
30: */
31: public final class RouterConfiguration {
32:         /**
33:          * The associated RouterSoftware.
34:          */
35:         private final RouterSoftware routerSoftware;
36:         /**
37:          * A map of configured packages by name.
38:          */
39:         private final transient Map<String, ConfiguredPackage> packages;
40:         /**
41:          * Archive generator of the Packages.
42:          */
43:         private final transient ArchiveGenerator archGen;
44:         /**
45:          * Path to the configuration directory.
46:          */
47:         private final String path;
48:
49:         /**
50:          * AssignmentFileManager to store a AssignmentFile.
51:          */
52:         private final AssignmentFileManager assignmentFileManager;
53:
54:         /**
55:          * Builds building phases.
56:          * <p>
57:          * TODO: merge with {@link RouterSoftware#BuildingPhaseBuilder}.
58:          */
59:         interface BuildingPhaseBuilder {
60:                 /**
61:                  * Adds a building phase to passed controller.
62:                  *
63:                  * @param controller
64:                  * The {@link Controller} to use.
65:                  * @param pkg
66:                  * The package the file belongs to.
67:                  * @param file
68:                  * The file.
69:                  */
70:                 void add(Controller controller, ConfiguredPackage pkg, File file);
71:         }
72:
73:         /**
74:          * Default constructor.
75:          *
76:          * @param routerSoftware
77:          * The associated RouterSoftware.
78:          */
79:         public RouterConfiguration(final RouterSoftware routerSoftware) {
80:                 super();
81:                 this.routerSoftware = routerSoftware;
82:                 this.packages = new HashMap<String, ConfiguredPackage>();
83:                 this.archGen = new ArchiveGenerator(this);
84:                 this.path = null;
85:                 this.assignmentFileManager = new AssignmentFileManager();
86:                 addAFMtoTFM();
87:                 for (final Package pkg : routerSoftware.getPackages().values()) {
88:                         this.add(pkg);
89:                 }
90:         }
91:
92:         /**
93:          * Default constructor.
94:          *
95:          * @param routerSoftware
96:          * The associated RouterSoftware.
97:          * @param path
98:          * The path to the configuration directory.
99:          * @throws TypeDoesNotExistException
100:          * if some referenced type is not found.
101:          * @throws ScannerException
102:          * if scanning fails.
103:          * @throws InterruptedException
104:          * if asynchronous scanning/parsing has been interrupted.
105:          * @throws IOException
106:          * if an I/O error occurs while reading files.
107:          * @throws ReaderException
108:          * if some necessary package file is missing.
109:          * @throws ParserException
110:          * if parsing files.
111:          * @throws TypeExceptions
112:          * if a cycle is detected or the reference is unresolved
113:          */
114:         public RouterConfiguration(final RouterSoftware routerSoftware, final String path)
115:                         throws TypeDoesNotExistException, ScannerException, InterruptedException, IOException,
116:                         ReaderException, ParserException, TypeExceptions {
117:                 super();
118:                 this.routerSoftware = routerSoftware;
119:                 this.packages = new HashMap<String, ConfiguredPackage>();
120:                 this.archGen = new ArchiveGenerator(this);
121:                 this.path = path;
122:                 this.assignmentFileManager = new AssignmentFileManager();
123:                 for (final Package pkg : routerSoftware.getPackages().values()) {
124:                         this.add(pkg);
125:                 }
126:                 final Controller controller = new Controller();
127:                 this.addBuildingPhases(controller, PackageReader.readFiles(this.path, ".txt"),
128:                                 new BuildingPhaseBuilder() {
129:                                         @Override
130:                                         public void add(final Controller controller, final ConfiguredPackage pkg,
131:                                                         final File file) {
132:                                                 controller.addToBulidingPhase(
133:                                                                 new ScanAndParseAssignmentFile(pkg, file, getThis()));
134:                                         }
135:                                 });
136:                 // TODO in separate Phase
137:                 addAFMtoTFM();
138:                 controller.addToCheckingConditionPhase(
139:                                 new CheckConditions(getRouterSoftware().getTypeFileManager()));
140:                 controller.start();
141:         }
142:
143:         /**
144:          * Adds building phases to passed controller.
145:          *
146:          * @param controller
147:          * The {@link Controller} to use.
148:          * @param files
149:          * The files to iterate over.
150:          * @param builder
151:          * Builds the building phases for the controller according to the files passed.
152:          */
153:         private void addBuildingPhases(final Controller controller, final Map<String, File> files,
154:                         final BuildingPhaseBuilder builder) {
155:                 for (Map.Entry<String, File> entry : files.entrySet()) {
156:                         final String name = entry.getKey();
157:                         if (this.hasPackage(name)) {
158:                                 builder.add(controller, this.getPackage(name), entry.getValue());
159:                         }
160:                 }
161:         }
162:
163:         @Override
164:         public boolean equals(final Object object) {
165:                 if (!(object instanceof RouterConfiguration)) {
166:                         return false;
167:                 }
168:                 final RouterConfiguration other = (RouterConfiguration) object;
169:                 return this.routerSoftware.equals(other.getRouterSoftware())
170:                                 && this.packages.equals(other.getPackages());
171:         }
172:
173:         @Override
174:         public int hashCode() {
175:                 return this.routerSoftware.hashCode() + this.packages.hashCode();
176:         }
177:
178:         /**
179:          * Adds a package, creating an empty configuration.
180:          *
181:          * @param pkg
182:          * The underlying Package.
183:          * @return The associated ConfiguredPackage.
184:          */
185:         public ConfiguredPackage add(final Package pkg) {
186:                 final ConfiguredPackage cpkg = new ConfiguredPackage(this, pkg);
187:                 this.packages.put(pkg.getName(), cpkg);
188:                 return cpkg;
189:         }
190:
191:         /**
192:          * @return The associated RouterSoftware.
193:          */
194:         public RouterSoftware getRouterSoftware() {
195:                 return this.routerSoftware;
196:         }
197:
198:         /**
199:          * @return the path to the configuration.
200:          */
201:         public String getPath() {
202:                 return this.path;
203:         }
204:
205:         /**
206:          * Returns true if a configured package exists.
207:          *
208:          * @param pkgName
209:          * the name of the package to search for
210:          * @return true if a package with passed name exists
211:          */
212:         public boolean hasPackage(final String pkgName) {
213:                 return this.packages.containsKey(pkgName);
214:         }
215:
216:         /**
217:          * Returns a configured package by name.
218:          *
219:          * @param pkgName
220:          * name of the desired package
221:          * @return the configured package
222:          */
223:         public ConfiguredPackage getPackage(final String pkgName) {
224:                 return this.packages.get(pkgName);
225:         }
226:
227:         /**
228:          * Getter for this Object.
229:          *
230:          * @return RouterConfiguration
231:          */
232:         private RouterConfiguration getThis() {
233:                 return this;
234:         }
235:
236:         /**
237:          * @return all configured packages.
238:          */
239:         public Map<String, ConfiguredPackage> getPackages() {
240:                 return Collections.unmodifiableMap(this.packages);
241:         }
242:
243:         /**
244:          * generating a list of all Assignments.
245:          *
246:          * @return a list of Assignments
247:          */
248:         private Map<String, VariableAssignment> generateAssLists() {
249:                 return this.generateAssLists(new ConfigurationContext());
250:         }
251:
252:         /**
253:          * generating a list of all Assignments.
254:          *
255:          * @param context
256:          * .
257:          * @return a lsit of Assignemnts
258:          */
259:         private Map<String, VariableAssignment> generateAssLists(final VariableContext context) {
260:
261:                 final Iterator<Entry<String, Package>> iter =
262:                                 this.routerSoftware.getPackages().entrySet().iterator();
263:
264:                 final Map<String, VariableAssignment> assignments =
265:                                 new HashMap<String, VariableAssignment>();
266:
267:                 while (iter.hasNext()) {
268:                         final ConfiguredPackage currentPackage =
269:                                         this.packages.get(iter.next().getValue().getName());
270:                         final Iterator<VariableAssignment> assignsToCurPack =
271:                                         currentPackage.getAssignmentFile().getAssignments().values().iterator();
272:                         while (assignsToCurPack.hasNext()) {
273:                                 final VariableAssignment currentAssignment = assignsToCurPack.next();
274:                                 if (currentAssignment.getDefinition().getContext().equals(context)) {
275:                                         assignments.put(currentAssignment.getNameOfAssignment(), currentAssignment);
276:                                 }
277:                         }
278:                 }
279:
280:                 return assignments;
281:         }
282:
283:         /**
284:          *
285:          * @param varDef
286:          * variableDefinition
287:          * @return HashMap
288:          */
289:         public Map<String, VariableAssignment>
290:                         findAssesByDefinition(final AbstractVariableDefinition varDef) {
291:
292:                 return this.findAssesByDefinition(varDef, new ConfigurationContext());
293:
294:         }
295:
296:         /**
297:          * @param context
298:          * .
299:          * @param varDef
300:          * variableDefinition
301:          * @return HashMap
302:          */
303:         public Map<String, VariableAssignment> findAssesByDefinition(
304:                         final AbstractVariableDefinition varDef, final VariableContext context) {
305:
306:                 final Map<String, VariableAssignment> asses = this.generateAssLists(context);
307:                 final Iterator<Entry<String, VariableAssignment>> iter = asses.entrySet().iterator();
308:
309:                 final Map<String, VariableAssignment> result = new HashMap<String, VariableAssignment>();
310:                 while (iter.hasNext()) {
311:                         final Entry<String, VariableAssignment> currentAssEntry = iter.next();
312:
313:                         final AbstractVariableDefinition varDev = currentAssEntry.getValue().getDefinition();
314:
315:                         if (varDev.equals(varDef) && varDev.getContext().equals(context)) {
316:                                 result.put(currentAssEntry.getValue().getNameOfAssignment(),
317:                                                 currentAssEntry.getValue());
318:                         }
319:                 }
320:                 return result;
321:
322:         }
323:
324:         /**
325:          * Retuniert die VariableAssignment zum Namen NAME.
326:          *
327:          * @param name
328:          * .
329:          * @return .
330:          */
331:         public VariableAssignment findAssignmentByName(final String name) {
332:                 return this.findAssignmentByName(name, new ConfigurationContext());
333:         }
334:
335:         /**
336:          * Retuniert die VariableAssignment zum Namen NAME. Nur Assignments die zum gegebenen Context
337:          * gehoeren.
338:          *
339:          * @param context
340:          * .
341:          * @param name
342:          * .
343:          * @return .
344:          */
345:         public VariableAssignment findAssignmentByName(final String name,
346:                         final VariableContext context) {
347:                 final Map<String, VariableAssignment> assignments = this.generateAssLists(context);
348:                 return assignments.get(name.toUpperCase(Locale.ENGLISH));
349:         }
350:
351:         /**
352:          * @return the archGen
353:          */
354:         public ArchiveGenerator getArchGen() {
355:                 return this.archGen;
356:         }
357:
358:         /**
359:          * @return the archGen
360:          */
361:         public Map<String, VariableAssignment> getAllAssignments() {
362:                 return this.generateAssLists();
363:         }
364:
365:         /**
366:          * Getter for assignmentFileManager.
367:          *
368:          * @return assignmentFileManager
369:          */
370:         public AssignmentFileManager getAssignmentFileManager() {
371:                 return assignmentFileManager;
372:         }
373:
374:         /**
375:          * Adds the AssignmentFileManager of this RouterConfiguration to the TypeFileManager with its
376:          * "path" for identification.
377:          */
378:         private void addAFMtoTFM() {
379:                 getRouterSoftware().getTypeFileManager().addAssignmentFileManager(path,
380:                                 this.assignmentFileManager);
381:         }
382:
383: }