001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.pluto.om.common;
018:
019: import java.util.Collection;
020: import java.util.HashSet;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Set;
024:
025: import javax.portlet.PreferencesValidator;
026:
027: import org.apache.pluto.om.common.Preference;
028: import org.apache.pluto.om.common.PreferenceSet;
029: import org.apache.pluto.om.common.PreferenceSetCtrl;
030: import org.apache.pluto.util.StringUtils;
031:
032: /**
033: *
034: *
035: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
036: *
037: * @version CVS $Id: PreferenceSetImpl.java 433543 2006-08-22 06:22:54Z crossley $
038: */
039: public class PreferenceSetImpl implements PreferenceSet,
040: PreferenceSetCtrl, java.io.Serializable {
041:
042: private String castorPreferencesValidator;
043: private ClassLoader classLoader;
044: private Set preferences = new HashSet();
045:
046: /**
047: * @see org.apache.pluto.om.common.PreferenceSet#get(java.lang.String)
048: */
049: public Preference get(String name) {
050: Iterator iterator = this .preferences.iterator();
051: while (iterator.hasNext()) {
052: Preference preference = (Preference) iterator.next();
053: if (preference.getName().equals(name)) {
054: return preference;
055: }
056: }
057: return null;
058: }
059:
060: /**
061: * @see org.apache.pluto.om.common.PreferenceSet#iterator()
062: */
063: public Iterator iterator() {
064: return this .preferences.iterator();
065: }
066:
067: /**
068: * @see org.apache.pluto.om.common.PreferenceSet#getPreferencesValidator()
069: */
070: public PreferencesValidator getPreferencesValidator() {
071: if (this .classLoader == null)
072: throw new IllegalStateException(
073: "Portlet class loader not yet available to load preferences validator.");
074:
075: if (castorPreferencesValidator == null)
076: return null;
077:
078: try {
079: Object validator = classLoader.loadClass(
080: castorPreferencesValidator).newInstance();
081: if (validator instanceof PreferencesValidator) {
082: return (PreferencesValidator) validator;
083: }
084: } catch (Exception ignore) {
085: // ignore it
086: }
087:
088: return null;
089: }
090:
091: /**
092: * @see org.apache.pluto.om.common.PreferenceSetCtrl#add(java.lang.String, java.util.List)
093: */
094: public Preference add(String name, List values) {
095: PreferenceImpl preference = new PreferenceImpl();
096: preference.setName(name);
097: preference.setValues(values);
098:
099: this .preferences.add(preference);
100:
101: return preference;
102: }
103:
104: public boolean add(Preference preference) {
105: return this .preferences.add(preference);
106: }
107:
108: /**
109: * @see org.apache.pluto.om.common.PreferenceSetCtrl#remove(java.lang.String)
110: */
111: public Preference remove(String name) {
112: Iterator iterator = this .iterator();
113: while (iterator.hasNext()) {
114: Preference preference = (Preference) iterator.next();
115: if (preference.getName().equals(name)) {
116: this .preferences.remove(preference);
117: return preference;
118: }
119: }
120: return null;
121: }
122:
123: /**
124: * @see org.apache.pluto.om.common.PreferenceSetCtrl#remove(org.apache.pluto.om.common.Preference)
125: */
126: public void remove(Preference preference) {
127: this .preferences.remove(preference);
128: }
129:
130: /**
131: * @see java.lang.Object#toString()
132: */
133: public String toString() {
134: StringBuffer buffer = new StringBuffer(50);
135: buffer.append(StringUtils.lineSeparator);
136: buffer.append(getClass().toString());
137: buffer.append(": ");
138: Iterator iterator = this .iterator();
139: while (iterator.hasNext()) {
140: buffer.append(((PreferenceImpl) iterator.next())
141: .toString(2));
142: }
143: return buffer.toString();
144: }
145:
146: // additional internal methods
147:
148: public String getCastorPreferencesValidator() {
149: return castorPreferencesValidator;
150: }
151:
152: public void setCastorPreferencesValidator(
153: String castorPreferencesValidator) {
154: this .castorPreferencesValidator = castorPreferencesValidator;
155: }
156:
157: public Set getPreferences() {
158: return this .preferences;
159: }
160:
161: public void setClassLoader(ClassLoader loader) {
162: this .classLoader = loader;
163: }
164:
165: /**
166: * Makes a deep copy.
167: * @see java.util.Collection#addAll(Collection)
168: */
169: public boolean addAll(Collection c) {
170: boolean changed = false;
171: Iterator it = c.iterator();
172: while (it.hasNext()) {
173: changed = true;
174: PreferenceImpl pref = (PreferenceImpl) it.next();
175: this
176: .add(pref.getName(), pref
177: .getClonedCastorValuesAsList());
178: }
179:
180: return changed;
181: }
182:
183: }
|