1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.sf.sail.core.curnit;
24
25 import java.util.Collection;
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.NoSuchElementException;
29 import java.util.Set;
30
31 import net.sf.sail.common.beansupport.ITitleAware;
32 import net.sf.sail.core.beans.Pod;
33 import net.sf.sail.core.beans.assembly.PodRegistry;
34 import net.sf.sail.core.util.SailBeanUtils;
35 import net.sf.sail.core.uuid.CurnitUuid;
36 import net.sf.sail.core.uuid.PodUuid;
37
38 /***
39 * @author turadg
40 */
41 public class Curnit implements ITitleAware {
42
43 private static final long serialVersionUID = 1L;
44
45 {
46 PodArchiveResolver.getSystemResolver();
47 }
48
49 PodUuid rootPodId;
50
51 Set<Pod> referencedPods = new HashSet<Pod>();
52
53 CurnitUuid curnitId;
54
55 String title = "undefined title";
56
57 private boolean assembled = false;
58
59 public CurnitUuid getCurnitId() {
60 return curnitId;
61 }
62
63 public void setCurnitId(CurnitUuid curnitId) {
64 this.curnitId = curnitId;
65 }
66
67 public String getTitle() {
68 return title;
69 }
70
71 public void setTitle(String curnitTitle) {
72 this.title = curnitTitle;
73 }
74
75 private void collectReferences(Pod pod) {
76 referencedPods.add(pod);
77 for (Iterator iter = pod.getChildPodIds().iterator(); iter.hasNext();) {
78 PodUuid childPodId = (PodUuid) iter.next();
79 Pod childPod = PodRegistry.getRegistry().getPod(childPodId);
80 collectReferences(childPod);
81 }
82 }
83
84 public void assemble() {
85 if (assembled)
86 throw new IllegalStateException("curnit already assembled");
87
88 if (rootPodId == null)
89 throw new IllegalStateException(
90 "cannot assemble() before setRootPodId()");
91
92 getRootPod().assemble();
93 SailBeanUtils.printTree(System.out, 0, getRootPod());
94 assembled = true;
95 }
96
97 /***
98 *
99 */
100 private Pod getRootPod() {
101 Pod rootPod = PodRegistry.getRegistry().getPod(rootPodId);
102 if (rootPod == null)
103 throw new NoSuchElementException("could not find pod " + rootPodId);
104 return rootPod;
105 }
106
107 public boolean isAssembled() {
108 return assembled;
109 }
110
111 /***
112 *
113 */
114 private void refreshReferences() {
115 referencedPods.clear();
116 collectReferences(getRootPod());
117 }
118
119 public Collection getReferencedPods() {
120 refreshReferences();
121 return referencedPods;
122 }
123
124 public PodUuid getRootPodId() {
125 return rootPodId;
126 }
127
128 public void setRootPodId(PodUuid rootPodId) {
129 this.rootPodId = rootPodId;
130 }
131
132 }