1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 * Trace model is a collection of trace groups |
|
17 * |
|
18 */ |
|
19 package com.nokia.tracecompiler.model; |
|
20 |
|
21 import java.util.ArrayList; |
|
22 import java.util.Collections; |
|
23 import java.util.Enumeration; |
|
24 import java.util.Iterator; |
|
25 import java.util.Properties; |
|
26 |
|
27 import com.nokia.tracecompiler.engine.TraceCompilerEngineGlobals; |
|
28 import com.nokia.tracecompiler.engine.TraceCompilerEngineErrorCodes.TraceCompilerErrorCode; |
|
29 import com.nokia.tracecompiler.engine.project.SortedProperties; |
|
30 import com.nokia.tracecompiler.project.FormattingUtils; |
|
31 import com.nokia.tracecompiler.source.SourceConstants; |
|
32 |
|
33 /** |
|
34 * Trace model is a collection of trace groups. Listener interfaces can be |
|
35 * attached to a trace model to receive change notifications when traces are |
|
36 * added, removed or modified. |
|
37 * |
|
38 */ |
|
39 public class TraceModel extends TraceObject implements Iterable<TraceGroup> { |
|
40 |
|
41 /** |
|
42 * Group property prefix |
|
43 */ |
|
44 public final String GROUP_PROPERTY_PREFIX = "[GROUP]"; //$NON-NLS-1$ |
|
45 |
|
46 /** |
|
47 * Trace property prefix |
|
48 */ |
|
49 public final String TRACE_PROPERTY_PREFIX = "[TRACE]"; //$NON-NLS-1$ |
|
50 |
|
51 /** |
|
52 * Obsolete property prefix |
|
53 */ |
|
54 public final String OBSOLETE_PROPERTY_PREFIX = "[[OBSOLETE]]"; //$NON-NLS-1$ |
|
55 |
|
56 /** |
|
57 * Group Id prefix |
|
58 */ |
|
59 public final String GROUP_ID_PREFIX = "["; //$NON-NLS-1$ |
|
60 |
|
61 /** |
|
62 * Group Id suffix |
|
63 */ |
|
64 public final String GROUP_ID_SUFFIX = "]"; //$NON-NLS-1$ |
|
65 |
|
66 /** |
|
67 * Factory object for creating other trace objects |
|
68 */ |
|
69 private TraceObjectFactory factory; |
|
70 |
|
71 /** |
|
72 * Property verifier interface |
|
73 */ |
|
74 private TraceObjectPropertyVerifier verifier; |
|
75 |
|
76 /** |
|
77 * List of trace groups |
|
78 */ |
|
79 private ArrayList<TraceGroup> groups = new ArrayList<TraceGroup>(); |
|
80 |
|
81 /** |
|
82 * List of traces, sorted by name |
|
83 */ |
|
84 private ArrayList<Trace> tracesByName = new ArrayList<Trace>(); |
|
85 |
|
86 /** |
|
87 * List of model listeners |
|
88 */ |
|
89 private ArrayList<TraceModelListener> modelListeners = new ArrayList<TraceModelListener>(); |
|
90 |
|
91 /** |
|
92 * List of extension listeners |
|
93 */ |
|
94 private ArrayList<TraceModelExtensionListener> extensionListeners = new ArrayList<TraceModelExtensionListener>(); |
|
95 |
|
96 /** |
|
97 * List of reset listeners |
|
98 */ |
|
99 private ArrayList<TraceModelResetListener> resetListeners = new ArrayList<TraceModelResetListener>(); |
|
100 |
|
101 /** |
|
102 * Processing listeners |
|
103 */ |
|
104 private ArrayList<TraceProcessingListener> processingListeners = new ArrayList<TraceProcessingListener>(); |
|
105 |
|
106 /** |
|
107 * List of constant tables |
|
108 */ |
|
109 private ArrayList<TraceConstantTable> constantTables = new ArrayList<TraceConstantTable>(); |
|
110 |
|
111 /** |
|
112 * Validity flag |
|
113 */ |
|
114 private boolean valid; |
|
115 |
|
116 /** |
|
117 * Number of nested calls to startProcessing |
|
118 */ |
|
119 private int isProcessing; |
|
120 |
|
121 /** |
|
122 * Model was changed during processing |
|
123 */ |
|
124 private boolean modelChangedDuringProcessing; |
|
125 |
|
126 /** |
|
127 * Fixed Ids from fixed Ids definition file |
|
128 */ |
|
129 private SortedProperties fixedIds; |
|
130 |
|
131 /** |
|
132 * Hex radix |
|
133 */ |
|
134 public int HEX_RADIX = 16; // CodForChk_Dis_Magic |
|
135 |
|
136 /** |
|
137 * Constructor |
|
138 * |
|
139 * @param factory |
|
140 * the rule factory |
|
141 * @param verifier |
|
142 * the property verifier |
|
143 * @throws TraceCompilerException |
|
144 */ |
|
145 public TraceModel(TraceObjectRuleFactory factory, |
|
146 TraceObjectPropertyVerifier verifier) throws TraceCompilerException { |
|
147 // Stored for callback purposes |
|
148 setModel(this); |
|
149 this.factory = new TraceObjectFactory(this, factory); |
|
150 this.verifier = verifier; |
|
151 } |
|
152 |
|
153 /** |
|
154 * Adds a new trace model listener to this model |
|
155 * |
|
156 * @param listener |
|
157 * the new listener |
|
158 */ |
|
159 public void addModelListener(TraceModelListener listener) { |
|
160 modelListeners.add(listener); |
|
161 } |
|
162 |
|
163 /** |
|
164 * Removes a trace model listener. Does nothing if the listener is not found |
|
165 * |
|
166 * @param listener |
|
167 * the listener to be removed |
|
168 */ |
|
169 public void removeModelListener(TraceModelListener listener) { |
|
170 modelListeners.remove(listener); |
|
171 } |
|
172 |
|
173 /** |
|
174 * Adds a new trace model extension listener to this model |
|
175 * |
|
176 * @param listener |
|
177 * the new listener |
|
178 */ |
|
179 public void addExtensionListener(TraceModelExtensionListener listener) { |
|
180 extensionListeners.add(listener); |
|
181 } |
|
182 |
|
183 /** |
|
184 * Removes a trace model extension listener. Does nothing if the listener is |
|
185 * not found |
|
186 * |
|
187 * @param listener |
|
188 * the listener to be removed |
|
189 */ |
|
190 public void removeExtensionListener(TraceModelExtensionListener listener) { |
|
191 extensionListeners.remove(listener); |
|
192 } |
|
193 |
|
194 /** |
|
195 * Adds a new trace model reset listener to this model |
|
196 * |
|
197 * @param listener |
|
198 * the new listener |
|
199 */ |
|
200 public void addResetListener(TraceModelResetListener listener) { |
|
201 resetListeners.add(listener); |
|
202 } |
|
203 |
|
204 /** |
|
205 * Removes a trace model reset listener. Does nothing if the listener is not |
|
206 * found |
|
207 * |
|
208 * @param listener |
|
209 * the listener to be removed |
|
210 */ |
|
211 public void removeResetListener(TraceModelResetListener listener) { |
|
212 resetListeners.remove(listener); |
|
213 } |
|
214 |
|
215 /** |
|
216 * Adds a new trace model listener to this model |
|
217 * |
|
218 * @param listener |
|
219 * the new listener |
|
220 */ |
|
221 public void addProcessingListener(TraceProcessingListener listener) { |
|
222 processingListeners.add(listener); |
|
223 } |
|
224 |
|
225 /** |
|
226 * Removes a processing listener. Does nothing if the listener is not found |
|
227 * |
|
228 * @param listener |
|
229 * the listener to be removed |
|
230 */ |
|
231 public void removeProcessingListener(TraceProcessingListener listener) { |
|
232 processingListeners.remove(listener); |
|
233 } |
|
234 |
|
235 /** |
|
236 * Returns highest group ID + 1. Can be used to create an unique ID for a |
|
237 * new trace group. |
|
238 * |
|
239 * @return the next trace group ID |
|
240 * @throws TraceCompilerException |
|
241 */ |
|
242 public int getNextGroupID() throws TraceCompilerException { |
|
243 int currentMaxGroupId = 0; |
|
244 int nextGroupId = 0; |
|
245 // Check if there are some fixed Ids |
|
246 if (fixedIds != null) { |
|
247 Enumeration<Object> keys = this.fixedIds.keys(); |
|
248 // Go through fixed Ids and check if there are fixed group Ids |
|
249 while (keys.hasMoreElements()) { |
|
250 String key = (String) keys.nextElement(); |
|
251 if (key.startsWith(GROUP_PROPERTY_PREFIX) |
|
252 || key.startsWith(OBSOLETE_PROPERTY_PREFIX |
|
253 + GROUP_PROPERTY_PREFIX)) { |
|
254 // Fixed group Id found. Try to covert it to int value. |
|
255 String value = fixedIds.getProperty(key); |
|
256 int fixedId = 0; |
|
257 try { |
|
258 fixedId = Integer.decode(value).intValue(); |
|
259 } catch (NumberFormatException e) { |
|
260 // Corrupted. Get next group Id later on. |
|
261 currentMaxGroupId = 0; |
|
262 break; |
|
263 } |
|
264 // Check if found fixed Id is bigger than current max group |
|
265 // Id |
|
266 if (fixedId > currentMaxGroupId) { |
|
267 currentMaxGroupId = fixedId; |
|
268 } |
|
269 } |
|
270 } |
|
271 } |
|
272 |
|
273 // If there were fixed group Ids. Set next group Id to be current max |
|
274 // group Id + 1 |
|
275 if (currentMaxGroupId != 0) { |
|
276 nextGroupId = currentMaxGroupId + 1; |
|
277 } |
|
278 |
|
279 // Get current max group id in model |
|
280 int maxGroupIdInModel = 0; |
|
281 for (TraceGroup group : groups) { |
|
282 int groupIdInModel = group.getID(); |
|
283 if (groupIdInModel > maxGroupIdInModel) { |
|
284 maxGroupIdInModel = groupIdInModel; |
|
285 } |
|
286 } |
|
287 |
|
288 // If next group Id is zero or smaller than current max group id in |
|
289 // model + 1. Set it be same as current max group id in model + 1. This |
|
290 // is done in case that we have added more than one new group after last |
|
291 // fixed Id update |
|
292 if (nextGroupId == 0 || nextGroupId < maxGroupIdInModel + 1) { |
|
293 nextGroupId = maxGroupIdInModel + 1; |
|
294 } |
|
295 |
|
296 // Check that next group Id is not bigger than max group Id |
|
297 if (nextGroupId > TraceCompilerEngineGlobals.MAX_GROUP_ID) { |
|
298 throw new TraceCompilerException( |
|
299 TraceCompilerErrorCode.RUN_OUT_OF_GROUP_IDS); |
|
300 } |
|
301 |
|
302 return nextGroupId; |
|
303 } |
|
304 |
|
305 /** |
|
306 * Returns highest constant table ID + 1. Can be used to create an unique ID |
|
307 * for a new constant table. |
|
308 * |
|
309 * @return the next constant table ID |
|
310 */ |
|
311 public int getNextConstantTableID() { |
|
312 int max = 0; |
|
313 for (TraceConstantTable table : constantTables) { |
|
314 int id = table.getID(); |
|
315 if (id > max) { |
|
316 max = id; |
|
317 } |
|
318 } |
|
319 return max + 1; |
|
320 } |
|
321 |
|
322 /** |
|
323 * Removes a trace group from this model. Create groupRemoved event to model |
|
324 * listeners |
|
325 * |
|
326 * @param group |
|
327 * the group to be removed |
|
328 * @throws TraceCompilerException |
|
329 */ |
|
330 public void removeGroup(TraceGroup group) throws TraceCompilerException { |
|
331 if (groups.remove(group)) { |
|
332 notifyObjectRemoved(this, group); |
|
333 group.reset(); |
|
334 } |
|
335 } |
|
336 |
|
337 /** |
|
338 * Determines if this model contains any trace groups |
|
339 * |
|
340 * @return true if there are trace groups |
|
341 */ |
|
342 public boolean hasGroups() { |
|
343 return !groups.isEmpty(); |
|
344 } |
|
345 |
|
346 /** |
|
347 * Gets the number of trace groups |
|
348 * |
|
349 * @return trace group count |
|
350 */ |
|
351 public int getGroupCount() { |
|
352 return groups.size(); |
|
353 } |
|
354 |
|
355 /** |
|
356 * Returns the trace groups of this model |
|
357 * |
|
358 * @return the iterator over the groups |
|
359 */ |
|
360 public Iterator<TraceGroup> getGroups() { |
|
361 return groups.iterator(); |
|
362 } |
|
363 |
|
364 /* |
|
365 * (non-Javadoc) |
|
366 * |
|
367 * @see java.lang.Iterable#iterator() |
|
368 */ |
|
369 public Iterator<TraceGroup> iterator() { |
|
370 return groups.iterator(); |
|
371 } |
|
372 |
|
373 /** |
|
374 * Removes all trace groups and parameters from this model. Extensions are |
|
375 * not removed. Notifies the reset listeners with modelResetting and |
|
376 * modelReset |
|
377 * |
|
378 * @see TraceModelResetListener#modelResetting |
|
379 * @see TraceModelResetListener#modelReset |
|
380 */ |
|
381 @Override |
|
382 public void reset() { |
|
383 notifyModelResetting(); |
|
384 // Properties are removed, other extensions are left |
|
385 removeExtensions(TraceObjectPropertyList.class); |
|
386 groups.clear(); |
|
387 tracesByName.clear(); |
|
388 constantTables.clear(); |
|
389 fixedIds = null; |
|
390 super.reset(); |
|
391 notifyModelReset(); |
|
392 } |
|
393 |
|
394 /** |
|
395 * Gets the group which has given ID |
|
396 * |
|
397 * @param id |
|
398 * the id |
|
399 * @return group or null |
|
400 */ |
|
401 public TraceGroup findGroupByID(int id) { |
|
402 TraceGroup retval = null; |
|
403 for (TraceGroup group : groups) { |
|
404 if (group.getID() == id) { |
|
405 retval = group; |
|
406 break; |
|
407 } |
|
408 } |
|
409 return retval; |
|
410 } |
|
411 |
|
412 /** |
|
413 * Locates a trace group which has the given name. |
|
414 * |
|
415 * @param name |
|
416 * the name of the trace group |
|
417 * @return the group or null if not found |
|
418 * @see TraceObject#getName |
|
419 */ |
|
420 public TraceGroup findGroupByName(String name) { |
|
421 TraceGroup retval = null; |
|
422 for (TraceGroup group : groups) { |
|
423 if (group.getName().equals(name)) { |
|
424 retval = group; |
|
425 break; |
|
426 } |
|
427 } |
|
428 return retval; |
|
429 } |
|
430 |
|
431 /** |
|
432 * Locates a trace which has the given name. |
|
433 * |
|
434 * @param name |
|
435 * the name of the trace |
|
436 * @return the trace or null if not found |
|
437 * @see TraceObject#getName |
|
438 */ |
|
439 public Trace findTraceByName(String name) { |
|
440 Trace retval; |
|
441 int index = Collections.binarySearch(tracesByName, name, |
|
442 TraceObjectUtils.traceToNameComparator); |
|
443 if (index >= 0) { |
|
444 retval = tracesByName.get(index); |
|
445 } else { |
|
446 retval = null; |
|
447 } |
|
448 return retval; |
|
449 } |
|
450 |
|
451 /** |
|
452 * Returns the group at given index |
|
453 * |
|
454 * @param index |
|
455 * the group index |
|
456 * @return the group |
|
457 */ |
|
458 public TraceGroup getGroupAt(int index) { |
|
459 return groups.get(index); |
|
460 } |
|
461 |
|
462 /** |
|
463 * Removes a constant table from this model. Creates objectRemoved event to |
|
464 * model listeners |
|
465 * |
|
466 * @see TraceModelListener#objectRemoved(TraceObject, TraceObject) |
|
467 * @param table |
|
468 * the table to be removed |
|
469 * @throws TraceCompilerException |
|
470 */ |
|
471 public void removeConstantTable(TraceConstantTable table) throws TraceCompilerException { |
|
472 if (constantTables.remove(table)) { |
|
473 notifyObjectRemoved(this, table); |
|
474 table.reset(); |
|
475 } |
|
476 } |
|
477 |
|
478 /** |
|
479 * Gets the constant tables of this model |
|
480 * |
|
481 * @return the tables iterator |
|
482 */ |
|
483 public Iterator<TraceConstantTable> getConstantTables() { |
|
484 return constantTables.iterator(); |
|
485 } |
|
486 |
|
487 /** |
|
488 * Gets a constant table by ID |
|
489 * |
|
490 * @param id |
|
491 * the ID |
|
492 * @return the table or null |
|
493 */ |
|
494 public TraceConstantTable findConstantTableByID(int id) { |
|
495 TraceConstantTable retval = null; |
|
496 for (TraceConstantTable table : constantTables) { |
|
497 if (table.getID() == id) { |
|
498 retval = table; |
|
499 break; |
|
500 } |
|
501 } |
|
502 return retval; |
|
503 } |
|
504 |
|
505 /** |
|
506 * Gets a constant table by name |
|
507 * |
|
508 * @param tableName |
|
509 * the name |
|
510 * @return the table or null |
|
511 */ |
|
512 public TraceConstantTable findConstantTableByName(String tableName) { |
|
513 TraceConstantTable retval = null; |
|
514 for (TraceConstantTable table : constantTables) { |
|
515 if (table.getName().equals(tableName)) { |
|
516 retval = table; |
|
517 break; |
|
518 } |
|
519 } |
|
520 return retval; |
|
521 } |
|
522 |
|
523 /** |
|
524 * Checks if this model contains constant tables |
|
525 * |
|
526 * @return true if there are constant tables |
|
527 */ |
|
528 public boolean hasConstantTables() { |
|
529 return !constantTables.isEmpty(); |
|
530 } |
|
531 |
|
532 /** |
|
533 * Returns the constant table at given index |
|
534 * |
|
535 * @param index |
|
536 * the group index |
|
537 * @return the group |
|
538 */ |
|
539 public TraceConstantTable getConstantTableAt(int index) { |
|
540 return constantTables.get(index); |
|
541 } |
|
542 |
|
543 /** |
|
544 * Adds a new trace group to this model. Creates objectAdded event to model |
|
545 * listeners. This is only intended to be called from TraceGroup |
|
546 * constructor, so this is not public. |
|
547 * |
|
548 * @see TraceModelListener#objectAdded(TraceObject, TraceObject) |
|
549 * @param group |
|
550 * the group to be added |
|
551 */ |
|
552 void addGroup(TraceGroup group) { |
|
553 groups.add(group); |
|
554 notifyObjectAdded(this, group); |
|
555 } |
|
556 |
|
557 /** |
|
558 * Adds a constant table to this model. Created objectAdded event to model |
|
559 * listeners. This is only intended to be called from constant table |
|
560 * constructor, so this is not public |
|
561 * |
|
562 * @see TraceModelListener#objectAdded(TraceObject, TraceObject) |
|
563 * @param table |
|
564 * the constant table |
|
565 */ |
|
566 void addConstantTable(TraceConstantTable table) { |
|
567 constantTables.add(table); |
|
568 notifyObjectAdded(this, table); |
|
569 } |
|
570 |
|
571 /** |
|
572 * Fires propertiesUpdated event. Called from trace objects when their |
|
573 * properties change. |
|
574 * |
|
575 * @see TraceModelListener#propertyUpdated(TraceObject, int) |
|
576 * @param source |
|
577 * the object that changed |
|
578 * @param property |
|
579 * the property that changed |
|
580 * @throws TraceCompilerException |
|
581 */ |
|
582 void notifyPropertyUpdated(TraceObject source, int property) throws TraceCompilerException { |
|
583 if (source instanceof Trace) { |
|
584 if (property == TraceModelListener.NAME) { |
|
585 Collections.sort(tracesByName, |
|
586 TraceObjectUtils.traceObjectNameComparator); |
|
587 } |
|
588 ((Trace) source).getGroup().tracePropertyUpdated(source, property); |
|
589 } |
|
590 for (TraceModelListener l : modelListeners) { |
|
591 l.propertyUpdated(source, property); |
|
592 } |
|
593 modelChangedDuringProcessing = true; |
|
594 } |
|
595 |
|
596 /** |
|
597 * Fires objectAdded event to listeners |
|
598 * |
|
599 * @see TraceModelListener#objectAdded(TraceObject, TraceObject) |
|
600 * @param owner |
|
601 * the owner object |
|
602 * @param object |
|
603 * the object that was added |
|
604 */ |
|
605 void notifyObjectAdded(TraceObject owner, TraceObject object) { |
|
606 // List is sorted when the name is set to the trace |
|
607 if (object instanceof Trace) { |
|
608 tracesByName.add((Trace) object); |
|
609 } |
|
610 for (TraceModelListener l : modelListeners) { |
|
611 l.objectAdded(owner, object); |
|
612 } |
|
613 modelChangedDuringProcessing = true; |
|
614 } |
|
615 |
|
616 /** |
|
617 * Fires objectRemoved event to listeners |
|
618 * |
|
619 * @see TraceModelListener#objectRemoved(TraceObject, TraceObject) |
|
620 * @param owner |
|
621 * the owner object |
|
622 * @param object |
|
623 * the object that was removed |
|
624 * @throws TraceCompilerException |
|
625 */ |
|
626 void notifyObjectRemoved(TraceObject owner, TraceObject object) throws TraceCompilerException { |
|
627 if (object instanceof Trace) { |
|
628 int index = Collections.binarySearch(tracesByName, (Trace) object, |
|
629 TraceObjectUtils.traceObjectNameComparator); |
|
630 tracesByName.remove(index); |
|
631 } |
|
632 notifyOnDelete(object); |
|
633 for (TraceModelListener l : modelListeners) { |
|
634 l.objectRemoved(owner, object); |
|
635 } |
|
636 modelChangedDuringProcessing = true; |
|
637 } |
|
638 |
|
639 /** |
|
640 * Notifies that an object creation is complete |
|
641 * |
|
642 * @see TraceModelListener#objectCreationComplete(TraceObject) |
|
643 * @param object |
|
644 * the object |
|
645 * @throws TraceCompilerException |
|
646 */ |
|
647 void notifyObjectCreationComplete(TraceObject object) throws TraceCompilerException { |
|
648 for (TraceModelListener l : modelListeners) { |
|
649 l.objectCreationComplete(object); |
|
650 } |
|
651 } |
|
652 |
|
653 /** |
|
654 * Fires modelResetting event to all listeners |
|
655 * |
|
656 * @see TraceModelResetListener#modelResetting() |
|
657 */ |
|
658 private void notifyModelResetting() { |
|
659 for (TraceModelResetListener l : resetListeners) { |
|
660 l.modelResetting(); |
|
661 } |
|
662 } |
|
663 |
|
664 /** |
|
665 * Fires modelReset event to all listeners |
|
666 * |
|
667 * @see TraceModelResetListener#modelReset() |
|
668 */ |
|
669 private void notifyModelReset() { |
|
670 for (TraceModelResetListener l : resetListeners) { |
|
671 l.modelReset(); |
|
672 } |
|
673 } |
|
674 |
|
675 /** |
|
676 * Fires extensionAdded event. Called from TraceObject when extension is |
|
677 * added to it |
|
678 * |
|
679 * @see TraceModelExtensionListener#extensionAdded(TraceObject, |
|
680 * TraceModelExtension) |
|
681 * @param object |
|
682 * the trace object |
|
683 * @param extension |
|
684 * the new extension |
|
685 */ |
|
686 void notifyExtensionAdded(TraceObject object, TraceModelExtension extension) { |
|
687 for (TraceModelExtensionListener l : extensionListeners) { |
|
688 l.extensionAdded(object, extension); |
|
689 } |
|
690 modelChangedDuringProcessing = true; |
|
691 } |
|
692 |
|
693 /** |
|
694 * Fires extensionRemoved event. Called from TraceObject when extension is |
|
695 * removed from it |
|
696 * |
|
697 * @see TraceModelExtensionListener#extensionRemoved(TraceObject, |
|
698 * TraceModelExtension) |
|
699 * @param object |
|
700 * the object |
|
701 * @param extension |
|
702 * the removed extension |
|
703 */ |
|
704 void notifyExtensionRemoved(TraceObject object, |
|
705 TraceModelExtension extension) { |
|
706 for (TraceModelExtensionListener l : extensionListeners) { |
|
707 l.extensionRemoved(object, extension); |
|
708 } |
|
709 modelChangedDuringProcessing = true; |
|
710 } |
|
711 |
|
712 /** |
|
713 * Gets the validity flag of this model |
|
714 * |
|
715 * @return the validity flag |
|
716 */ |
|
717 public boolean isValid() { |
|
718 return valid; |
|
719 } |
|
720 |
|
721 /** |
|
722 * Sets the validity flag |
|
723 * |
|
724 * @param valid |
|
725 * new flag value |
|
726 * @throws TraceCompilerException |
|
727 */ |
|
728 public void setValid(boolean valid) throws TraceCompilerException { |
|
729 if (valid != this.valid) { |
|
730 this.valid = valid; |
|
731 for (TraceModelResetListener l : resetListeners) { |
|
732 l.modelValid(valid); |
|
733 } |
|
734 } |
|
735 } |
|
736 |
|
737 /** |
|
738 * Gets the trace object factory |
|
739 * |
|
740 * @return the factory |
|
741 */ |
|
742 public TraceObjectFactory getFactory() { |
|
743 return factory; |
|
744 } |
|
745 |
|
746 /** |
|
747 * Gets the object verifier interface. The verifier should be used before |
|
748 * updating object properties |
|
749 * |
|
750 * @return the verifier |
|
751 */ |
|
752 public TraceObjectPropertyVerifier getVerifier() { |
|
753 return verifier; |
|
754 } |
|
755 |
|
756 /** |
|
757 * Notifies the process listeners that a process the results in multiple |
|
758 * listener updates is about to start |
|
759 */ |
|
760 public void startProcessing() { |
|
761 isProcessing++; |
|
762 if (isProcessing == 1) { |
|
763 modelChangedDuringProcessing = false; |
|
764 for (TraceProcessingListener l : processingListeners) { |
|
765 l.processingStarted(); |
|
766 } |
|
767 } |
|
768 } |
|
769 |
|
770 /** |
|
771 * Notifies the process listeners that a process the results in multiple |
|
772 * listener updates has finished |
|
773 */ |
|
774 public void processingComplete() { |
|
775 isProcessing--; |
|
776 if (isProcessing == 0) { |
|
777 for (TraceProcessingListener l : processingListeners) { |
|
778 l.processingComplete(modelChangedDuringProcessing); |
|
779 } |
|
780 modelChangedDuringProcessing = false; |
|
781 } |
|
782 } |
|
783 |
|
784 /** |
|
785 * Checks the state of the processing flag |
|
786 * |
|
787 * @return the processing flag |
|
788 */ |
|
789 public boolean isProcessing() { |
|
790 return isProcessing > 0; |
|
791 } |
|
792 |
|
793 /** |
|
794 * Checks if model has traces |
|
795 * |
|
796 * @return true if there's traces, false if not |
|
797 */ |
|
798 public boolean hasTraces() { |
|
799 boolean retval = false; |
|
800 for (TraceGroup group : groups) { |
|
801 if (group.hasTraces()) { |
|
802 retval = true; |
|
803 break; |
|
804 } |
|
805 } |
|
806 return retval; |
|
807 } |
|
808 |
|
809 /** |
|
810 * Gets group ID from properties |
|
811 * |
|
812 * @param properties |
|
813 * the properties |
|
814 * @param group |
|
815 * the group |
|
816 * @return the group ID |
|
817 * @throws TraceCompilerException |
|
818 */ |
|
819 public int getGroupID(Properties properties, TraceGroup group) |
|
820 throws TraceCompilerException { |
|
821 String value = properties.getProperty(GROUP_PROPERTY_PREFIX |
|
822 + group.getName()); |
|
823 int id; |
|
824 if (value == null) { |
|
825 // Not found, assign a proper ID |
|
826 id = getNewIdForGroup(group); |
|
827 |
|
828 } else { |
|
829 try { |
|
830 id = Integer.decode(value).intValue(); |
|
831 TraceGroup traceGroup = findGroupByID(id); |
|
832 if (traceGroup != null && !traceGroup.equals(group)) { |
|
833 // Id already in use, assign a proper ID |
|
834 id = getNewIdForGroup(group); |
|
835 } |
|
836 } catch (NumberFormatException e) { |
|
837 // Corrupted, assign a proper ID |
|
838 id = getNewIdForGroup(group); |
|
839 } |
|
840 } |
|
841 group.internalSetID(id); |
|
842 return id; |
|
843 } |
|
844 |
|
845 /** |
|
846 * Gets a new ID for this group |
|
847 * |
|
848 * @param group |
|
849 * the group |
|
850 * @return a new ID for this group |
|
851 * @throws TraceCompilerException |
|
852 */ |
|
853 private int getNewIdForGroup(TraceGroup group) throws TraceCompilerException { |
|
854 int id = FormattingUtils.getGroupID(group.getModel(), group.getName()); |
|
855 return id; |
|
856 } |
|
857 |
|
858 /** |
|
859 * Saves trace and group identifiers to given properties. |
|
860 * |
|
861 * @param properties |
|
862 * the properties to use |
|
863 */ |
|
864 public void saveIDs(Properties properties) { |
|
865 properties.clear(); |
|
866 StringBuffer sb = new StringBuffer(); |
|
867 for (TraceGroup group : this) { |
|
868 int groupId = group.getID(); |
|
869 properties.setProperty(GROUP_PROPERTY_PREFIX + group.getName(), |
|
870 SourceConstants.HEX_PREFIX + Integer.toHexString(groupId)); |
|
871 for (Trace trace : group) { |
|
872 int traceId = trace.getID(); |
|
873 properties.setProperty(TRACE_PROPERTY_PREFIX |
|
874 + createTraceName(sb, group, trace), |
|
875 SourceConstants.HEX_PREFIX |
|
876 + Integer.toHexString(traceId)); |
|
877 } |
|
878 } |
|
879 } |
|
880 |
|
881 /** |
|
882 * Merges the group and trace names together |
|
883 * |
|
884 * @param sb |
|
885 * the buffer where name is stored |
|
886 * @param group |
|
887 * group |
|
888 * @param trace |
|
889 * trace |
|
890 * @return the trace name |
|
891 */ |
|
892 public String createTraceName(StringBuffer sb, TraceGroup group, Trace trace) { |
|
893 sb.setLength(0); |
|
894 sb.append(group.getName()); |
|
895 sb.append(GROUP_ID_PREFIX); |
|
896 int groupId = group.getID(); |
|
897 String groupIdString = SourceConstants.HEX_PREFIX |
|
898 + Integer.toString(groupId, HEX_RADIX).toUpperCase(); |
|
899 sb.append(groupIdString); |
|
900 sb.append(GROUP_ID_SUFFIX); |
|
901 sb.append(SourceConstants.UNDERSCORE); |
|
902 sb.append(trace.getName()); |
|
903 return sb.toString(); |
|
904 } |
|
905 |
|
906 /** |
|
907 * Gets fixed group and trace ids |
|
908 * |
|
909 * @return fixed Ids |
|
910 */ |
|
911 public SortedProperties getFixedIds() { |
|
912 return fixedIds; |
|
913 } |
|
914 |
|
915 /** |
|
916 * Set fixed group and trace ids |
|
917 * |
|
918 * @param fixedIds |
|
919 * fixed Ids |
|
920 */ |
|
921 public void setFixedIds(SortedProperties fixedIds) { |
|
922 this.fixedIds = fixedIds; |
|
923 } |
|
924 |
|
925 /** |
|
926 * Gets next trace ID |
|
927 * |
|
928 * @param group |
|
929 * the group |
|
930 * @return the trace ID |
|
931 * @throws TraceCompilerException |
|
932 */ |
|
933 public int getNextTraceId(TraceGroup group) throws TraceCompilerException { |
|
934 int currentMaxTraceID = 0; |
|
935 int nextTraceId = 0; |
|
936 // Check if there are some fixed Ids |
|
937 if (fixedIds != null) { |
|
938 Enumeration<Object> keys = this.fixedIds.keys(); |
|
939 String groupName = group.getName(); |
|
940 int groupId = group.getID(); |
|
941 String groupIdString = SourceConstants.HEX_PREFIX |
|
942 + Integer.toString(groupId, HEX_RADIX).toUpperCase(); |
|
943 // Go through fixed Ids and check if there are fixed trace Ids to |
|
944 // this group |
|
945 while (keys.hasMoreElements()) { |
|
946 String key = (String) keys.nextElement(); |
|
947 if (key.startsWith(TRACE_PROPERTY_PREFIX + groupName |
|
948 + GROUP_ID_PREFIX + groupIdString + GROUP_ID_SUFFIX |
|
949 + SourceConstants.UNDERSCORE) |
|
950 || key.startsWith(OBSOLETE_PROPERTY_PREFIX |
|
951 + TRACE_PROPERTY_PREFIX + groupName |
|
952 + GROUP_ID_PREFIX + groupIdString |
|
953 + GROUP_ID_SUFFIX + SourceConstants.UNDERSCORE)) { |
|
954 // Fixed trace Id to in this group found. Try to covert it |
|
955 // to int value. |
|
956 String value = fixedIds.getProperty(key); |
|
957 int fixedId = 0; |
|
958 try { |
|
959 fixedId = Integer.decode(value).intValue(); |
|
960 } catch (NumberFormatException e) { |
|
961 // Corrupted. Get next trace Id later on. |
|
962 nextTraceId = 0; |
|
963 break; |
|
964 } |
|
965 // Check if found fixed Id is bigger than current max trace |
|
966 // Id in this group |
|
967 if (fixedId > currentMaxTraceID) { |
|
968 currentMaxTraceID = fixedId; |
|
969 } |
|
970 } |
|
971 } |
|
972 // If there were fixed trace Ids to this group. Set next trace Id to |
|
973 // be current max trace Id + 1 |
|
974 if (currentMaxTraceID != 0) { |
|
975 nextTraceId = currentMaxTraceID + 1; |
|
976 } |
|
977 // If next trace Id is zero or smaller than trace Id that group |
|
978 // suggest. Set it be same as group suggest. This is done in case |
|
979 // that we have added more than one new trace to same group after |
|
980 // last fixed Id update |
|
981 if (nextTraceId == 0 || nextTraceId < group.getNextTraceID()) { |
|
982 nextTraceId = group.getNextTraceID(); |
|
983 } |
|
984 } else { |
|
985 // No fixed Ids. Get next trace Id from group |
|
986 nextTraceId = group.getNextTraceID(); |
|
987 } |
|
988 // Check that next trace Id is not bigger than max trace Id |
|
989 if (nextTraceId > TraceCompilerEngineGlobals.MAX_TRACE_ID) { |
|
990 throw new TraceCompilerException( |
|
991 TraceCompilerErrorCode.RUN_OUT_OF_TRACE_IDS); |
|
992 } |
|
993 |
|
994 return nextTraceId; |
|
995 } |
|
996 |
|
997 } |
|