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.awt.Component;
26  import java.io.File;
27  import java.io.FileNotFoundException;
28  import java.util.Collection;
29  import java.util.Collections;
30  import java.util.HashSet;
31  import java.util.Set;
32  
33  import net.sf.sail.common.beansupport.ITitleAware;
34  import net.sf.sail.core.beans.Pod;
35  import net.sf.sail.core.beans.SessionContext;
36  import net.sf.sail.core.beans.UnknownPodException;
37  import net.sf.sail.core.beans.assembly.PodRegistry;
38  import net.sf.sail.core.uuid.CurnitUuid;
39  import net.sf.sail.core.uuid.PodUuid;
40  
41  /**
42   * @author turadg
43   */
44  public class Curnit implements ITitleAware, ICurnit {
45  
46  	/**
47  	 * Registry of pods to use for this curnit
48  	 */
49  	private static final PodRegistry REGISTRY = PodRegistry
50  			.getDefaultRegistry();
51  
52  	private static final long serialVersionUID = 1L;
53  
54  	{ // hack for now to ensure that this class is loaded
55  		PodArchiveResolver.getSystemResolver();
56  	}
57  
58  	Pod rootPod;
59  
60  	Set<Pod> referencedPods = new HashSet<Pod>();
61  
62  	CurnitUuid curnitId;
63  
64  	String title = "undefined title";
65  
66  	private boolean assembled = false;
67  
68  	// set to store the pod id that specifies the root pod, until the root pod
69  	// can be found in the registry
70  	private PodUuid pendingLookup = null;
71  
72  	public CurnitUuid getCurnitId() {
73  		return curnitId;
74  	}
75  
76  	public void setCurnitId(CurnitUuid curnitId) {
77  		this.curnitId = curnitId;
78  	}
79  
80  	public String getTitle() {
81  		return title;
82  	}
83  
84  	public void setTitle(String curnitTitle) {
85  		this.title = curnitTitle;
86  	}
87  
88  	private void collectReferences(Pod pod) {
89  		referencedPods.add(pod);
90  		Set<PodUuid> childPodIds = pod.getChildPodIds();
91  		for (PodUuid childPodId : childPodIds) {
92  			Pod childPod = REGISTRY.getPod(childPodId);
93  			collectReferences(childPod);
94  		}
95  	}
96  
97  	public void assemble() {
98  		if (assembled)
99  			throw new IllegalStateException("curnit already assembled");
100 
101 		refreshRootPod();
102 
103 		try {
104 			rootPod.assemble(REGISTRY);
105 		} catch (UnknownPodException e) {
106 			e.printStackTrace();
107 			throw new RuntimeException(e);
108 		}
109 		assembled = true;
110 	}
111 
112 	/**
113 	 * 
114 	 */
115 	private void refreshRootPod() {
116 		if (rootPod == null) {
117 			// pendingLookup gets set in setRootPodId()
118 			if (pendingLookup == null) {
119 				throw new IllegalStateException(
120 						"cannot assemble() before setRootPodId()");
121 			} else {
122 				rootPod = REGISTRY.getPod(pendingLookup);
123 				pendingLookup = null;
124 			}
125 		}
126 	}
127 
128 	public boolean isAssembled() {
129 		return assembled;
130 	}
131 
132 	/**
133 	 * @throws UnknownPodException
134 	 * 
135 	 */
136 	private void refreshReferences() throws UnknownPodException {
137 		referencedPods.clear();
138 		refreshRootPod();
139 		collectReferences(rootPod);
140 	}
141 
142 	public Collection<Pod> getReferencedPods() {
143 		try {
144 			refreshReferences();
145 		} catch (UnknownPodException e) {
146 			// TODO Auto-generated catch block
147 			e.printStackTrace();
148 		}
149 		return Collections.unmodifiableCollection(referencedPods);
150 	}
151 
152 	public PodUuid getRootPodId() {
153 		if (pendingLookup != null) {
154 			return pendingLookup;
155 		}
156 		if (rootPod == null)
157 			return null;
158 		else
159 			return rootPod.getPodId();
160 	}
161 
162 	public void setRootPodId(PodUuid rootPodId) throws UnknownPodException {
163 		// the only way the rootPod field gets set
164 		this.rootPod = REGISTRY.getPod(rootPodId);
165 		// if we can't find the pod for the id right now, delay it to later
166 		if (rootPod == null)
167 			pendingLookup = rootPodId;
168 	}
169 
170 	/* (non-Javadoc)
171 	 * @see net.sf.sail.core.curnit.ICurnit#getRootBean()
172 	 */
173 	public Object getRootBean() {
174 		// get the root pod from the curnit and add it to the SessionContext
175 		PodUuid rootPodId = getRootPodId();
176 		Pod rootPod = PodRegistry.getDefaultRegistry().getPod(rootPodId);
177 
178 		return rootPod;
179 	}
180 
181 	/* (non-Javadoc)
182 	 * @see net.sf.sail.core.curnit.ICurnit#initialize()
183 	 */
184 	public void initialize() {
185 		if (!isAssembled()) {
186 			assemble();
187 		}		
188 	}
189 
190 	public void saveCurnit(File file) throws FileNotFoundException,
191 			Exception {
192 	}
193 
194 	/* (non-Javadoc)
195 	 * @see net.sf.sail.core.curnit.ICurnit#setSessionDataService(net.sf.sail.core.session.SessionDataService)
196 	 */
197 	public void setSessionContext(SessionContext sessionContext) {
198 		// don't need to deal with the session context because the beans inside 
199 		// handle it themselves		
200 	}
201 
202 
203 
204 	/* (non-Javadoc)
205 	 * @see net.sf.sail.core.curnit.ICurnit#previewCurnit(java.awt.Component)
206 	 */
207 	public void previewCurnit(Component component) throws Exception {
208 		// TODO Auto-generated method stub
209 		
210 	}
211 
212 
213 }