001: /* Copyright 2004 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.container.om.common;
007:
008: import java.io.Serializable;
009: import java.util.HashMap;
010: import java.util.Iterator;
011: import java.util.List;
012: import java.util.Map;
013:
014: import javax.portlet.PreferencesValidator;
015:
016: import org.apache.pluto.om.common.Preference;
017: import org.apache.pluto.om.common.PreferenceSet;
018: import org.apache.pluto.om.common.PreferenceSetCtrl;
019:
020: /**
021: * Implementation of Apache Pluto object model.
022: * @author Ken Weiner, kweiner@unicon.net
023: * @version $Revision: 36827 $
024: */
025: public class PreferenceSetImpl implements PreferenceSet,
026: PreferenceSetCtrl, Serializable {
027:
028: Map preferences = null; // Preference name --> Preference
029: PreferencesValidator validator = null;
030: String validatorClassName = null;
031: ClassLoader classLoader = null;
032:
033: public PreferenceSetImpl() {
034: preferences = new HashMap();
035: }
036:
037: // PreferenceSet methods
038:
039: public Iterator iterator() {
040: return preferences.values().iterator();
041: }
042:
043: public Preference get(String name) {
044: return (Preference) preferences.get(name);
045: }
046:
047: public PreferencesValidator getPreferencesValidator() {
048: if (validator == null) {
049: if (this .classLoader == null) {
050: throw new IllegalStateException(
051: "Portlet class loader is not yet available to load preferences validator.");
052: }
053: try {
054: if ((validatorClassName != null)
055: && (!"".equals(validatorClassName))) {
056: Object o = classLoader
057: .loadClass(validatorClassName)
058: .newInstance();
059: if (o instanceof PreferencesValidator) {
060: validator = (PreferencesValidator) o;
061: }
062: }
063:
064: } catch (Exception e) {
065: // Do nothing
066: e.printStackTrace();
067: }
068: }
069: return validator;
070: }
071:
072: // PreferenceSetCtrl methods
073:
074: public Preference add(String name, List values) {
075: return add(name, values, false);
076: }
077:
078: public Preference remove(String name) {
079: return (Preference) preferences.remove(name);
080: }
081:
082: public void remove(Preference preference) {
083: preferences.remove(preference.getName());
084: }
085:
086: // Additional methods
087:
088: public void setClassLoader(ClassLoader classLoader) {
089: this .classLoader = classLoader;
090: }
091:
092: public void setPreferencesValidator(PreferencesValidator validator) {
093: this .validator = validator;
094: }
095:
096: public void setPreferencesValidator(String validatorClassName) {
097: this .validatorClassName = validatorClassName;
098: }
099:
100: public void clear() {
101: preferences.clear();
102: }
103:
104: public Preference add(String name, List values, boolean readOnly) {
105: PreferenceImpl preference = new PreferenceImpl();
106: preference.setName(name);
107: preference.setValues(values);
108: preference.setReadOnly(readOnly);
109: preferences.put(name, preference);
110: return preference;
111: }
112:
113: public void addAll(PreferenceSet preferences) {
114: Iterator iter = preferences.iterator();
115: while (iter.hasNext()) {
116: Preference preference = (Preference) iter.next();
117: this .preferences.put(preference.getName(), preference);
118: }
119: }
120:
121: public int size() {
122: return preferences.size();
123: }
124: }
|