KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > portalImpl > om > common > impl > PreferenceSetImpl


1 /*
2  * Copyright 2003,2004 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 /*
17
18  */

19
20 package org.apache.pluto.portalImpl.om.common.impl;
21
22 import java.util.Collection JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.portlet.PreferencesValidator;
28
29 import org.apache.pluto.om.common.Preference;
30 import org.apache.pluto.om.common.PreferenceSet;
31 import org.apache.pluto.om.common.PreferenceSetCtrl;
32 import org.apache.pluto.portalImpl.services.log.Log;
33 import org.apache.pluto.services.log.Logger;
34 import org.apache.pluto.util.StringUtils;
35
36 public class PreferenceSetImpl extends HashSet JavaDoc
37 implements PreferenceSet, PreferenceSetCtrl, java.io.Serializable JavaDoc {
38
39     private String JavaDoc castorPreferencesValidator;
40     private ClassLoader JavaDoc classLoader;
41     private Logger log = null;
42
43     public PreferenceSetImpl() {
44         this.log = Log.getService().getLogger(getClass());
45     }
46
47     // PreferenceSet implementation.
48

49     public Preference get(String JavaDoc name)
50     {
51         Iterator JavaDoc iterator = this.iterator();
52         while (iterator.hasNext()) {
53             Preference preference = (Preference)iterator.next();
54             if (preference.getName().equals(name)) {
55                 return preference;
56             }
57         }
58         return null;
59     }
60
61     public PreferencesValidator getPreferencesValidator()
62     {
63         if (this.classLoader == null)
64             throw new IllegalStateException JavaDoc("Portlet class loader not yet available to load preferences validator.");
65
66         if (castorPreferencesValidator == null)
67             return null;
68
69         try {
70             Object JavaDoc validator = classLoader.loadClass(castorPreferencesValidator).newInstance();
71             if (validator instanceof PreferencesValidator)
72                 return(PreferencesValidator)validator;
73             else
74                 log.error("Specified class " + castorPreferencesValidator +" is no preferences validator.");
75         } catch (Exception JavaDoc e) {
76             log.error(e.getMessage(), e);
77         }
78
79         return null;
80     }
81
82     // PreferenceSetCtrl implementation.
83

84     public Preference add(String JavaDoc name, List JavaDoc values)
85     {
86         PreferenceImpl preference = new PreferenceImpl();
87         preference.setName(name);
88         preference.setValues(values);
89
90         super.add(preference);
91
92         return preference;
93     }
94
95     public Preference remove(String JavaDoc name)
96     {
97         Iterator JavaDoc iterator = this.iterator();
98         while (iterator.hasNext()) {
99             Preference preference = (Preference)iterator.next();
100             if (preference.getName().equals(name)) {
101                 super.remove(preference);
102                 return preference;
103             }
104         }
105         return null;
106     }
107
108     public void remove(Preference preference)
109     {
110         super.remove(preference);
111     }
112
113     // additional methods.
114

115     public String JavaDoc toString()
116     {
117         return toString(0);
118     }
119
120     public String JavaDoc toString(int indent)
121     {
122         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(50);
123         StringUtils.newLine(buffer,indent);
124         buffer.append(getClass().toString());
125         buffer.append(": ");
126         Iterator JavaDoc iterator = this.iterator();
127         while (iterator.hasNext()) {
128             buffer.append(((PreferenceImpl)iterator.next()).toString(indent+2));
129         }
130         return buffer.toString();
131     }
132
133
134     // additional internal methods
135

136     public String JavaDoc getCastorPreferencesValidator()
137     {
138         return castorPreferencesValidator;
139     }
140
141     public void setCastorPreferencesValidator(String JavaDoc castorPreferencesValidator)
142     {
143         this.castorPreferencesValidator = castorPreferencesValidator;
144     }
145
146     public Collection JavaDoc getCastorPreferences()
147     {
148         return this;
149     }
150
151     public void setClassLoader(ClassLoader JavaDoc loader)
152     {
153         this.classLoader = loader;
154     }
155
156     /**
157      * @see java.util.Collection#addAll(Collection)
158      * makes a deep copy
159      */

160     public boolean addAll(Collection JavaDoc c) {
161         Iterator JavaDoc it = c.iterator();
162         while (it.hasNext()) {
163             PreferenceImpl pref = (PreferenceImpl) it.next();
164             this.add(pref.getName(), pref.getClonedCastorValuesAsList());
165         }
166
167         return true; //always assume something changed
168
}
169
170 }
171
Popular Tags