KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > pluto > om > common > PreferenceSetImpl


1 /*
2  * Copyright 2004-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portal.pluto.om.common;
17
18 import java.util.Collection JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import javax.portlet.PreferencesValidator;
25
26 import org.apache.pluto.om.common.Preference;
27 import org.apache.pluto.om.common.PreferenceSet;
28 import org.apache.pluto.om.common.PreferenceSetCtrl;
29 import org.apache.pluto.util.StringUtils;
30
31 /**
32  *
33  *
34  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
35  *
36  * @version CVS $Id: PreferenceSetImpl.java 280338 2005-09-12 13:11:25Z cziegeler $
37  */

38 public class PreferenceSetImpl
39 implements PreferenceSet, PreferenceSetCtrl, java.io.Serializable JavaDoc {
40
41     private String JavaDoc castorPreferencesValidator;
42     private ClassLoader JavaDoc classLoader;
43     private Set JavaDoc preferences = new HashSet JavaDoc();
44
45     /**
46      * @see org.apache.pluto.om.common.PreferenceSet#get(java.lang.String)
47      */

48     public Preference get(String JavaDoc name) {
49         Iterator JavaDoc iterator = this.preferences.iterator();
50         while (iterator.hasNext()) {
51             Preference preference = (Preference)iterator.next();
52             if (preference.getName().equals(name)) {
53                 return preference;
54             }
55         }
56         return null;
57     }
58
59     /**
60      * @see org.apache.pluto.om.common.PreferenceSet#iterator()
61      */

62     public Iterator JavaDoc iterator() {
63         return this.preferences.iterator();
64     }
65
66     /**
67      * @see org.apache.pluto.om.common.PreferenceSet#getPreferencesValidator()
68      */

69     public PreferencesValidator getPreferencesValidator() {
70         if (this.classLoader == null)
71             throw new IllegalStateException JavaDoc("Portlet class loader not yet available to load preferences validator.");
72
73         if (castorPreferencesValidator == null)
74             return null;
75
76         try {
77             Object JavaDoc validator = classLoader.loadClass(castorPreferencesValidator).newInstance();
78             if (validator instanceof PreferencesValidator) {
79                 return(PreferencesValidator)validator;
80             }
81         } catch (Exception JavaDoc ignore) {
82             // ignore it
83
}
84
85         return null;
86     }
87
88     /**
89      * @see org.apache.pluto.om.common.PreferenceSetCtrl#add(java.lang.String, java.util.List)
90      */

91     public Preference add(String JavaDoc name, List JavaDoc values) {
92         PreferenceImpl preference = new PreferenceImpl();
93         preference.setName(name);
94         preference.setValues(values);
95
96         this.preferences.add(preference);
97
98         return preference;
99     }
100
101     public boolean add(Preference preference) {
102         return this.preferences.add(preference);
103     }
104
105     /**
106      * @see org.apache.pluto.om.common.PreferenceSetCtrl#remove(java.lang.String)
107      */

108     public Preference remove(String JavaDoc name) {
109         Iterator JavaDoc iterator = this.iterator();
110         while (iterator.hasNext()) {
111             Preference preference = (Preference)iterator.next();
112             if (preference.getName().equals(name)) {
113                 this.preferences.remove(preference);
114                 return preference;
115             }
116         }
117         return null;
118     }
119
120     /**
121      * @see org.apache.pluto.om.common.PreferenceSetCtrl#remove(org.apache.pluto.om.common.Preference)
122      */

123     public void remove(Preference preference) {
124         this.preferences.remove(preference);
125     }
126
127     /**
128      * @see java.lang.Object#toString()
129      */

130     public String JavaDoc toString() {
131         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(50);
132         buffer.append(StringUtils.lineSeparator);
133         buffer.append(getClass().toString());
134         buffer.append(": ");
135         Iterator JavaDoc iterator = this.iterator();
136         while (iterator.hasNext()) {
137             buffer.append(((PreferenceImpl)iterator.next()).toString(2));
138         }
139         return buffer.toString();
140     }
141
142
143     // additional internal methods
144

145     public String JavaDoc getCastorPreferencesValidator() {
146         return castorPreferencesValidator;
147     }
148
149     public void setCastorPreferencesValidator(String JavaDoc castorPreferencesValidator) {
150         this.castorPreferencesValidator = castorPreferencesValidator;
151     }
152
153     public Set JavaDoc getPreferences() {
154         return this.preferences;
155     }
156
157     public void setClassLoader(ClassLoader JavaDoc loader) {
158         this.classLoader = loader;
159     }
160
161     /**
162      * Makes a deep copy.
163      * @see java.util.Collection#addAll(Collection)
164      */

165     public boolean addAll(Collection JavaDoc c) {
166         boolean changed = false;
167         Iterator JavaDoc it = c.iterator();
168         while (it.hasNext()) {
169             changed = true;
170             PreferenceImpl pref = (PreferenceImpl) it.next();
171             this.add(pref.getName(), pref.getClonedCastorValuesAsList());
172         }
173
174         return changed;
175     }
176
177 }
178
Popular Tags