View Javadoc

1   /*
2    * Copyright (c) 2005 Regents of the University of California (Regents). Created
3    * by TELS, Graduate School of Education, University of California at Berkeley.
4    *
5    * This software is distributed under the GNU Lesser General Public License, v2.
6    *
7    * Permission is hereby granted, without written agreement and without license
8    * or royalty fees, to use, copy, modify, and distribute this software and its
9    * documentation for any purpose, provided that the above copyright notice and
10   * the following two paragraphs appear in all copies of this software.
11   *
12   * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
13   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
14   * PURPOSE. THE SOFTWAREAND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
15   * HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
16   * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17   *
18   * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
19   * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
20   * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
21   * REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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  	{ // hack for now to ensure that this class is loaded
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 }