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.beans.assembly;
24  
25  import java.beans.DefaultPersistenceDelegate;
26  import java.util.Map;
27  import java.util.NoSuchElementException;
28  
29  import net.sf.sail.core.beans.Pod;
30  import net.sf.sail.core.uuid.PodUuid;
31  
32  /**
33   * Reference to a named podvar of a <b>Pod</b> that maintains over long-term
34   * persistance.
35   * 
36   * @author turadg
37   */
38  public class PodVarDelegate {
39  
40  	final static DefaultPersistenceDelegate delegate = new DefaultPersistenceDelegate(
41  			new String[] { "podId", "varName" });
42  
43  	PodUuid podId;
44  
45  	String varName;
46  
47  	public PodVarDelegate(PodUuid podId, String varName) {
48  		this.podId = podId;
49  		this.varName = varName;
50  	}
51  
52  	public PodUuid getPodId() {
53  		return podId;
54  	}
55  
56  	public PropertyRef getRef(PodRegistry registry) {
57  		Map<String, PropertyRef> slotMap = getRefMap(registry);
58  		return slotMap.get(varName);
59  	}
60  
61  	/**
62  	 * @param registry
63  	 *            TODO
64  	 * @return
65  	 */
66  	private Map<String, PropertyRef> getRefMap(PodRegistry registry) {
67  		Pod pod = registry.getPod(podId);
68  		if (pod == null)
69  			throw new NoSuchElementException("no known pod " + podId
70  					+ " in registry " + registry);
71  		Map<String, PropertyRef> slotMap = pod.getVars();
72  		return slotMap;
73  	}
74  
75  	public String getVarName() {
76  		return varName;
77  	}
78  
79  	public boolean isValid(PodRegistry registry) {
80  		Map<String, PropertyRef> slotMap = getRefMap(registry);
81  		return slotMap.containsKey(varName);
82  	}
83  
84  	/**
85  	 * @param newPodId
86  	 */
87  	public void setPodId(PodUuid newPodId) {
88  		this.podId = newPodId;
89  	}
90  
91  	/**
92  	 * @param varName The varName to set.
93  	 */
94  	public void setVarName(String varName) {
95  		this.varName = varName;
96  	}
97  
98  	@Override
99  	public String toString() {
100 		return "PodVarDelegate:" + podId + ":" + varName;
101 	}
102 
103 	public void validate(PodRegistry registry) {
104 		// FIXME make this a checked exception
105 		if (!isValid(registry))
106 			throw new RuntimeException(this + " refers to absent pod export");
107 	}
108 
109 }