001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.core.impl.portlet.state;
023:
024: import org.jboss.portal.common.NotYetImplemented;
025: import org.jboss.portal.common.util.MapAccessor;
026: import org.jboss.portal.common.util.TypedMap;
027: import org.jboss.portal.registration.Consumer;
028: import org.jboss.portal.registration.Registration;
029: import org.jboss.portal.registration.RegistrationStatus;
030:
031: import javax.xml.namespace.QName;
032: import java.util.Collections;
033: import java.util.HashMap;
034: import java.util.HashSet;
035: import java.util.Map;
036: import java.util.Set;
037:
038: /**
039: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
040: * @version $Revision: 9218 $
041: */
042: public class PersistentRegistration implements Registration {
043:
044: // Persistent fields
045:
046: private Long key;
047: private String persistentHandle;
048: private RegistrationStatus persistentStatus;
049: private Map persistentProperties;
050:
051: // Relationships
052:
053: private PersistentConsumer relatedConsumer;
054: private Set relatedPortletStates;
055:
056: // Wrapper
057:
058: private Properties properties;
059:
060: /** Hibernate constructor. */
061: PersistentRegistration() {
062: this .key = null;
063: this .persistentHandle = null;
064: this .relatedConsumer = null;
065: this .persistentStatus = null;
066: this .persistentProperties = new HashMap();
067: this .properties = new Properties();
068: this .relatedPortletStates = null;
069: }
070:
071: public PersistentRegistration(Map properties,
072: RegistrationStatus status) {
073: this .key = null;
074: this .persistentHandle = null;
075: this .relatedConsumer = null;
076: this .persistentStatus = status;
077: this .persistentProperties = properties;
078: this .properties = new Properties();
079: this .relatedPortletStates = new HashSet();
080:
081: // Perform properties validation
082: this .properties.validate();
083: }
084:
085: // Hibernate
086:
087: public Long getKey() {
088: return key;
089: }
090:
091: public void setKey(Long key) {
092: this .key = key;
093: }
094:
095: public Set getRelatedPortletStates() {
096: return relatedPortletStates;
097: }
098:
099: public void setRelatedPortletStates(Set relatedPortletStates) {
100: this .relatedPortletStates = relatedPortletStates;
101: }
102:
103: public String getPersistentHandle() {
104: return persistentHandle;
105: }
106:
107: public void setPersistentHandle(String persistentHandle) {
108: this .persistentHandle = persistentHandle;
109: }
110:
111: public PersistentConsumer getRelatedConsumer() {
112: return relatedConsumer;
113: }
114:
115: public void setRelatedConsumer(PersistentConsumer relatedConsumer) {
116: this .relatedConsumer = relatedConsumer;
117: }
118:
119: public RegistrationStatus getPersistentStatus() {
120: return persistentStatus;
121: }
122:
123: public void setPersistentStatus(RegistrationStatus persistentStatus) {
124: this .persistentStatus = persistentStatus;
125: }
126:
127: public Map getPersistentProperties() {
128: return persistentProperties;
129: }
130:
131: public void setPersistentProperties(Map persistentProperties) {
132: this .persistentProperties = persistentProperties;
133: }
134:
135: //
136:
137: public String getId() {
138: if (key == null) {
139: // If that ever happens it is a bug
140: throw new IllegalStateException(
141: "Transient registration object");
142: }
143: return key.toString();
144: }
145:
146: public void setRegistrationHandle(String handle) {
147: this .persistentHandle = handle;
148: }
149:
150: public String getRegistrationHandle() {
151: return persistentHandle;
152: }
153:
154: public Consumer getConsumer() {
155: return relatedConsumer;
156: }
157:
158: public Map getProperties() {
159: return Collections.unmodifiableMap(properties);
160: }
161:
162: public void setPropertyValueFor(QName propertyName, Object value) {
163: properties.setProperty(propertyName, value);
164: }
165:
166: public void setPropertyValueFor(String propertyName, Object value) {
167: properties.setProperty(propertyName, value);
168: }
169:
170: public void removeProperty(QName propertyName) {
171: properties.removeProperty(propertyName);
172: }
173:
174: public void removeProperty(String propertyName) {
175: properties.removeProperty(propertyName);
176: }
177:
178: public Object getPropertyValueFor(QName propertyName) {
179: return properties.getProperty(propertyName);
180: }
181:
182: public Object getPropertyValueFor(String propertyName) {
183: return properties.getProperty(propertyName);
184: }
185:
186: public void updateProperties(Map registrationProperties) {
187: properties.replace(registrationProperties);
188: }
189:
190: public boolean hasEqualProperties(Registration registration) {
191: if (registration != null) {
192: PersistentRegistration preg = (PersistentRegistration) registration;
193:
194: //
195: return hasEqualProperties(preg.persistentProperties);
196: }
197:
198: //
199: return false;
200: }
201:
202: public boolean hasEqualProperties(Map properties) {
203: return this .properties.equals(properties);
204: }
205:
206: public RegistrationStatus getStatus() {
207: return persistentStatus;
208: }
209:
210: public void setStatus(RegistrationStatus status) {
211: this .persistentStatus = status;
212: }
213:
214: public void clearAssociatedState() {
215: throw new NotYetImplemented();
216: }
217:
218: // ***********
219:
220: private static final TypedMap.Converter KEY_CONVERTER = new TypedMap.Converter() {
221: protected Object getInternal(Object o)
222: throws IllegalArgumentException, ClassCastException {
223: if (o instanceof QName == false) {
224: throw new ClassCastException();
225: }
226: return o;
227: }
228:
229: protected Object getExternal(Object o) {
230: return o;
231: }
232:
233: protected boolean equals(Object o, Object o1) {
234: return o.equals(o1);
235: }
236: };
237:
238: private static final TypedMap.Converter VALUE_CONVERTER = new TypedMap.Converter() {
239: protected Object getInternal(Object o)
240: throws IllegalArgumentException, ClassCastException {
241: if (o instanceof String == false) {
242: throw new ClassCastException();
243: }
244: return o;
245: }
246:
247: protected Object getExternal(Object o) {
248: return o;
249: }
250:
251: protected boolean equals(Object o, Object o1) {
252: return o.equals(o1);
253: }
254: };
255:
256: private final MapAccessor accessor = new MapAccessor() {
257: public Map getMap(boolean b) {
258: return persistentProperties;
259: }
260: };
261:
262: /** Implement registration properties semantics, mostly validation and equality. */
263: public class Properties extends TypedMap {
264:
265: public Properties() {
266: super (accessor, KEY_CONVERTER, VALUE_CONVERTER);
267: }
268:
269: public void setProperty(String propertyName, Object value) {
270: if (propertyName == null) {
271: throw new IllegalArgumentException(
272: "No null property name accepted");
273: }
274:
275: //
276: setProperty(new QName(propertyName), value);
277: }
278:
279: public void setProperty(QName propertyName, Object value) {
280: if (propertyName == null) {
281: throw new IllegalArgumentException(
282: "No null property name accepted");
283: }
284:
285: //
286: put(propertyName, value);
287: }
288:
289: public void removeProperty(QName propertyName) {
290: if (propertyName == null) {
291: throw new IllegalArgumentException(
292: "No null property name accepted");
293: }
294:
295: //
296: remove(propertyName);
297: }
298:
299: public void removeProperty(String propertyName) {
300: if (propertyName == null) {
301: throw new IllegalArgumentException(
302: "No null property name accepted");
303: }
304:
305: //
306: removeProperty(new QName(propertyName));
307: }
308:
309: public Object getProperty(QName propertyName) {
310: if (propertyName == null) {
311: throw new IllegalArgumentException(
312: "No null property name accepted");
313: }
314:
315: //
316: return get(propertyName);
317: }
318:
319: public Object getProperty(String propertyName) {
320: if (propertyName == null) {
321: throw new IllegalArgumentException(
322: "No null property name accepted");
323: }
324:
325: //
326: return getProperty(new QName(propertyName));
327: }
328: }
329: }
|