View Javadoc

1   /*
2    * Copyright 2000-2001,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  package org.apache.jetspeed.om.registry.base;
18  
19  import org.apache.jetspeed.om.registry.*;
20  
21  import java.util.*;
22  
23  /***
24   * The BaseSkinEntry is a bean like implementation of the SkinEntry
25   * interface suitable for Castor XML serialization
26   *
27   * @see org.apache.jetspeed.om.registry.SkinEntry
28   * @author <a href="mailto:raphael@apache.org">Raphaël Luta</a>
29   * @version $Id: BaseSkinEntry.java,v 1.4 2004/02/23 03:08:26 jford Exp $
30   */
31  public class BaseSkinEntry extends BaseRegistryEntry implements SkinEntry
32  {
33  
34      private Vector parameter = new Vector();
35  
36      private transient Map nameIdx = null;
37  
38      /***
39       * Implements the equals operation so that 2 elements are equal if
40       * all their member values are equal.
41       */
42      public boolean equals(Object object)
43      {
44          if (object==null)
45          {
46              return false;
47          }
48  
49          BaseSkinEntry obj = (BaseSkinEntry)object;
50  
51          Iterator i = parameter.iterator();
52          Iterator i2 = obj.parameter.iterator();
53          while(i.hasNext())
54          {
55              BaseParameter c1 = (BaseParameter)i.next();
56              BaseParameter c2 = null;
57  
58              if (i2.hasNext())
59              {
60                  c2 = (BaseParameter)i2.next();
61              }
62              else
63              {
64                  return false;
65              }
66  
67              if (!c1.equals(c2))
68              {
69                  return false;
70              }
71          }
72  
73          if (i2.hasNext())
74          {
75              return false;
76          }
77  
78          return super.equals(object);
79      }
80  
81      /*** @return an enumeration of this entry parameter names */
82      public Iterator getParameterNames()
83      {
84          synchronized (parameter)
85          {
86              if (nameIdx == null)
87              {
88                  buildNameIndex();
89              }
90          }
91  
92          return nameIdx.keySet().iterator();
93      }
94  
95      /*** Search for a named parameter and return the associated
96       *  parameter object. The search is case sensitive.
97       *
98       *  @return the parameter object for a given parameter name
99       *  @param name the parameter name to look for
100      */
101     public Parameter getParameter( String name )
102     {
103         synchronized (parameter)
104         {
105             if (nameIdx == null)
106             {
107                 buildNameIndex();
108             }
109         }
110 
111         if (name != null)
112         {
113             Integer pos = (Integer)nameIdx.get(name);
114 
115             if (pos != null)
116             {
117                 return (Parameter)parameter.elementAt(pos.intValue());
118             }
119         }
120 
121         return null;
122     }
123 
124 
125     /*** Returns a map of parameter values keyed on the parameter names
126      *  @return the parameter values map
127      */
128     public Map getParameterMap()
129     {
130         Hashtable params = new Hashtable();
131         Enumeration en = parameter.elements();
132         while(en.hasMoreElements())
133         {
134             Parameter param = (Parameter)en.nextElement();
135             params.put(param.getName(),param.getValue());
136         }
137 
138         return params;
139 
140     }
141 
142     /*** Adds a new parameter for this entry
143      *  @param name the new parameter name
144      *  @param value the new parameter value
145      */
146     public void addParameter( String name, String value )
147     {
148         if (name != null)
149         {
150             Parameter p = getParameter(name);
151             if (p == null)
152             {
153                 p = new BaseParameter();
154                 p.setName(name);
155             }
156 
157             p.setValue(value);
158 
159             addParameter(p);
160 
161         }
162     }
163 
164     /*** Adds a new parameter for this entry
165      *  @param parameter the new parameter to add
166      */
167     public void addParameter( Parameter param )
168     {
169         synchronized (parameter)
170         {
171             parameter.addElement( param );
172             nameIdx.put( param.getName(), new Integer( parameter.size()-1 ) );
173         }
174     }
175 
176     /*** Removes all parameter values associated with the
177      *  name
178      *
179      * @param name the parameter name to remove
180      */
181     public void removeParameter( String name )
182     {
183         if (name == null) return;
184 
185         synchronized (parameter)
186         {
187             Iterator i = parameter.iterator();
188             while(i.hasNext())
189             {
190                 Parameter param = (Parameter)i.next();
191                 if (param.getName().equals(name))
192                 {
193                     i.remove();
194                 }
195             }
196 
197             buildNameIndex();
198         }
199     }
200 
201     /*** This method recreates the paramter name index for quick retrieval
202      *  of parameters by name. Shoule be called whenever a complete index
203      *  of parameter should be rebuilt (eg removing a parameter or setting
204      *  a parameters vector)
205      */
206     private void buildNameIndex()
207     {
208         Hashtable idx = new Hashtable();
209 
210         Iterator i = parameter.iterator();
211         int count = 0;
212         while( i.hasNext() )
213         {
214             Parameter p = (Parameter)i.next();
215             idx.put( p.getName(), new Integer(count) );
216             count++;
217         }
218 
219         this.nameIdx = idx;
220     }
221 
222     // Castor serialization accessor methods
223 
224     /*** Needed for Castor 0.8.11 XML serialization for retrieving the
225      *  parameters objects associated to this object
226      */
227     public Vector getParameters()
228     {
229         return this.parameter;
230     }
231 
232     public void setParameters(Vector parameters)
233     {
234         this.parameter = parameters;
235     }
236 
237 }