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.Vector;
22  import java.util.Map;
23  import java.util.Hashtable;
24  import java.util.Iterator;
25  import java.util.Enumeration;
26  
27  /***
28   * The BasePortletInfoEntry is a bean like implementation of the PortletInfoEntry
29   * interface suitable for Castor XML serialization
30   *
31   * @see org.apache.jetspeed.om.registry.PortletInfoEntry
32   * @author <a href="mailto:raphael@apache.org">RaphaŽl Luta</a>
33   * @version $Id: BasePortletInfoEntry.java,v 1.9 2004/03/29 19:28:24 jford Exp $
34   */
35  public abstract class BasePortletInfoEntry extends BaseRegistryEntry
36  {
37  
38      protected String classname;
39  
40      protected Vector parameter = new Vector();
41  
42      protected transient Map nameIdx = null;
43  
44      protected Vector medias = new Vector();
45  
46      protected transient Map mediasIdx = null;
47  
48      protected Vector tools = new Vector();
49  
50      protected transient Map toolsIdx = null;
51  
52      /***
53       * Implements the equals operation so that 2 elements are equal if
54       * all their member values are equal.
55       */
56      public boolean equals(Object object)
57      {
58          if (object==null)
59          {
60              return false;
61          }
62  
63          BasePortletInfoEntry obj = (BasePortletInfoEntry)object;
64  
65          if (classname!=null)
66          {
67              if (!classname.equals(obj.getClassname()))
68              {
69                  return false;
70              }
71          }
72          else
73          {
74              if (obj.getClassname()!=null)
75              {
76                  return false;
77              }
78          }
79  
80          Iterator i = parameter.iterator();
81          Iterator i2 = obj.getParameters().iterator();
82          while(i.hasNext())
83          {
84              BaseParameter p1 = (BaseParameter)i.next();
85              BaseParameter p2 = null;
86  
87              if (i2.hasNext())
88              {
89                  p2 = (BaseParameter)i2.next();
90              }
91              else
92              {
93                  return false;
94              }
95  
96              if (!p1.equals(p2))
97              {
98                  return false;
99              }
100         }
101 
102         if (i2.hasNext())
103         {
104             return false;
105         }
106 
107         i = medias.iterator();
108         i2 = obj.getMediaTypes().iterator();
109         while(i.hasNext())
110         {
111             BaseMediaType m1 = (BaseMediaType)i.next();
112             BaseMediaType m2 = null;
113 
114             if (i2.hasNext())
115             {
116                 m2 = (BaseMediaType)i2.next();
117             }
118             else
119             {
120                 return false;
121             }
122 
123             if (!m1.equals(m2))
124             {
125                 return false;
126             }
127         }
128 
129         if (i2.hasNext())
130         {
131             return false;
132         }
133 
134         i = tools.iterator();
135         i2 = obj.getTools().iterator();
136         while(i.hasNext())
137         {
138             BaseToolDescriptor t1 = (BaseToolDescriptor)i.next();
139             BaseToolDescriptor t2 = null;
140 
141             if (i2.hasNext())
142             {
143                 t2 = (BaseToolDescriptor)i2.next();
144             }
145             else
146             {
147                 return false;
148             }
149 
150             if (!t1.equals(t2))
151             {
152                 return false;
153             }
154         }
155 
156         if (i2.hasNext())
157         {
158             return false;
159         }
160 
161         return super.equals(object);
162     }
163 
164     /*** @return the classname associated to this entry */
165     public String getClassname()
166     {
167         return this.classname;
168     }
169 
170     /*** Sets the classname for this entry. This classname is used for instanciating
171      *  the associated element
172      *
173      *  @param classname the classname used for instanciating the component associated with
174      *  this entry
175      */
176     public void setClassname( String classname )
177     {
178         this.classname = classname;
179     }
180 
181     /*** @return an enumeration of this entry parameter names */
182     public Iterator getParameterNames()
183     {
184         synchronized (parameter)
185         {
186             if (nameIdx == null)
187             {
188                 buildNameIndex();
189             }
190         }
191 
192         return nameIdx.keySet().iterator();
193     }
194 
195 
196     /*** Search for a named parameter and return the associated
197      *  parameter object. The search is case sensitive.
198      *
199      *  @return the parameter object for a given parameter name
200      *  @param name the parameter name to look for
201      */
202     public Parameter getParameter( String name )
203     {
204         synchronized (parameter)
205         {
206             if (nameIdx == null)
207             {
208                 buildNameIndex();
209             }
210         }
211 
212         if (name != null)
213         {
214             Integer pos = (Integer)nameIdx.get(name);
215 
216             if (pos != null)
217             {
218                 return (Parameter)parameter.elementAt(pos.intValue());
219             }
220         }
221 
222         return null;
223     }
224 
225 
226     /*** Returns a map of parameter values keyed on the parameter names
227      *  @return the parameter values map
228      */
229     public Map getParameterMap()
230     {
231         Hashtable params = new Hashtable();
232         Enumeration en = parameter.elements();
233         while(en.hasMoreElements())
234         {
235             Parameter param = (Parameter)en.nextElement();
236             String key = param.getName();
237             String value = param.getValue();
238             if(key != null && value != null)
239             {
240             	params.put(key, value);
241             }
242         }
243         return params;
244     }
245 
246     /*** Adds a new parameter for this entry
247      *  @param name the new parameter name
248      *  @param value the new parameter value
249      */
250     public void addParameter( String name, String value )
251     {
252         if (name != null)
253         {
254             Parameter p = getParameter(name);
255             if (p == null)
256             {
257                 if (this instanceof PortletEntry)
258                     p = new BaseCachedParameter();
259                else
260                     p = new BaseParameter();
261                 p.setName(name);
262             }
263 
264             p.setValue(value);
265 
266             addParameter(p);
267 
268         }
269     }
270 
271     /*** Adds a new parameter for this entry
272      *  @param parameter the new parameter to add
273      */
274     public void addParameter( Parameter param )
275     {
276         synchronized (parameter)
277         {
278             if (parameter == null)
279                 parameter = new Vector();
280 
281             if (nameIdx == null)
282                 buildNameIndex();
283 
284             parameter.addElement( param );
285             nameIdx.put( param.getName(), new Integer( parameter.size()-1 ) );
286         }
287     }
288 
289     /*** Removes all parameter values associated with the
290      *  name
291      *
292      * @param name the parameter name to remove
293      */
294     public void removeParameter( String name )
295     {
296         if (name == null) return;
297 
298         synchronized (parameter)
299         {
300             Iterator i = parameter.iterator();
301             while(i.hasNext())
302             {
303                 Parameter param = (Parameter)i.next();
304                 if (param.getName().equals(name))
305                 {
306                     i.remove();
307                 }
308             }
309 
310             buildNameIndex();
311         }
312     }
313 
314     /***
315      * Returns a list of the supported media type names
316      *
317      * @return an iterator on the supported media type names
318      */
319     public Iterator listMediaTypes()
320     {
321         if (mediasIdx == null)
322         {
323             synchronized( medias )
324             {
325                 buildMediasIndex();
326             }
327         }
328 
329         return mediasIdx.keySet().iterator();
330     }
331 
332     /***
333      * Test if a given media type is supported by this entry.
334      * The test is done by a case sensitive name comparison
335      *
336      * @param name the media type name to test for.
337      * @return true is the media type is supported false otherwise
338      */
339     public boolean hasMediaType(String name)
340     {
341         if (mediasIdx == null)
342         {
343             synchronized( medias )
344             {
345                 buildMediasIndex();
346             }
347         }
348         return ((name!=null)&&(mediasIdx.get(name)!=null));
349     }
350 
351     /***
352      * Add a new supported media type
353      *
354      * @param name the media type name to add.
355      */
356     public void addMediaType(String name)
357     {
358         if (name!=null)
359         {
360             synchronized (medias)
361             {
362                 if (mediasIdx == null)
363                 {
364                     buildMediasIndex();
365                 }
366                 
367                 BaseMediaType m = new BaseMediaType();
368                 m.setRef(name);
369                 mediasIdx.put(name,new Integer(medias.size()));
370                 medias.add(m);
371             }
372         }
373     }
374 
375     /***
376      * Remove support for a given media type
377      *
378      * @param name the media type name to remove.
379      */
380     public void removeMediaType(String name)
381     {
382         if (name != null)
383         {
384             synchronized (medias)
385             {
386                 mediasIdx.remove(name);
387                 
388                 BaseMediaType m = new BaseMediaType();
389                 m.setRef(name);
390 
391                 Iterator i = medias.iterator();
392                 while (i.hasNext())
393                 {
394                     if (i.next().equals(m))
395                     {
396                         i.remove();
397                         return;
398                     }
399                 }
400             }
401         }
402     }
403 
404     // Castor serialization accessor methods
405 
406     /*** Needed for Castor 0.8.11 XML serialization for retrieving the
407      *  parameters objects associated to this object
408      */
409     public Vector getParameters()
410     {
411         return this.parameter;
412     }
413 
414     public void setParameters(Vector parameters)
415     {
416         this.parameter = parameters;
417     }
418 
419     public void setMediaTypes(Vector mediaTypes)
420     {
421         this.medias = mediaTypes;
422     }
423 
424     /*** Needed for Castor 0.8.11 XML serialization for retrieving the
425      *  media type names associated to this object
426      */
427     public Vector getMediaTypes()
428     {
429         return this.medias;
430     }
431 
432     public Vector getTools()
433     {
434         return this.tools;
435     }
436 
437     public void setTools(Vector tools)
438     {
439         this.tools = tools;
440     }
441 
442     /*** This method recreates the paramter name index for quick retrieval
443      *  of parameters by name. Shoule be called whenever a complete index
444      *  of parameter should be rebuilt (eg removing a parameter or setting
445      *  a parameters vector)
446      */
447     protected void buildNameIndex()
448     {
449         Hashtable idx = new Hashtable();
450 
451         Iterator i = parameter.iterator();
452         int count = 0;
453         while( i.hasNext() )
454         {
455             Parameter p = (Parameter)i.next();
456             idx.put( p.getName(), new Integer(count) );
457             count++;
458         }
459 
460         this.nameIdx = idx;
461     }
462 
463     /*** This method recreates the media name index for quick retrieval
464      *  by name.
465      */
466     private void buildMediasIndex()
467     {
468         Hashtable idx = new Hashtable();
469 
470         Iterator i = medias.iterator();
471         int count = 0;
472         while( i.hasNext() )
473         {
474             BaseMediaType b = (BaseMediaType)i.next();
475             idx.put( b.getRef(), new Integer(count) );
476             count++;
477         }
478 
479         this.mediasIdx = idx;
480     }
481 
482     /*** @return an enumeration of this entry parameter names */
483     public Iterator getToolNames()
484     {
485         synchronized (tools)
486         {
487             if (toolsIdx == null)
488             {
489                 buildToolsIndex();
490             }
491         }
492 
493         return toolsIdx.keySet().iterator();
494     }
495 
496 
497     /*** Search for a named parameter and return the associated
498      *  parameter object. The search is case sensitive.
499      *
500      *  @return the parameter object for a given parameter name
501      *  @param name the parameter name to look for
502      */
503     public ToolDescriptor getTool( String name )
504     {
505         synchronized (tools)
506         {
507             if (toolsIdx == null)
508             {
509                 buildToolsIndex();
510             }
511         }
512 
513         if (name != null)
514         {
515             Integer pos = (Integer)toolsIdx.get(name);
516 
517             if (pos != null)
518             {
519                 return (ToolDescriptor)tools.elementAt(pos.intValue());
520             }
521         }
522 
523         return null;
524     }
525 
526 
527     /*** Returns a map of parameter values keyed on the parameter names
528      *  @return the parameter values map
529      */
530     public Map getToolMap()
531     {
532         Hashtable map = new Hashtable();
533         Enumeration en = tools.elements();
534         while(en.hasMoreElements())
535         {
536             ToolDescriptor desc = (ToolDescriptor)en.nextElement();
537             map.put(desc.getName(),desc);
538         }
539         return map;
540     }
541 
542     /*** Adds a new parameter for this entry
543      *  @param parameter the new parameter to add
544      */
545     public void addTool( ToolDescriptor tool )
546     {
547         synchronized (tools)
548         {
549             if (tools == null)
550                 tools = new Vector();
551 
552             if (toolsIdx == null)
553                 buildToolsIndex();
554 
555             tools.addElement( tool );
556             toolsIdx.put( tool.getName(), new Integer( tools.size()-1 ) );
557         }
558     }
559 
560     /*** Removes all parameter values associated with the
561      *  name
562      *
563      * @param name the parameter name to remove
564      */
565     public void removeTool( String name )
566     {
567         if (name == null) return;
568 
569         synchronized (tools)
570         {
571             Iterator i = tools.iterator();
572             while(i.hasNext())
573             {
574                 ToolDescriptor tool = (ToolDescriptor)i.next();
575                 if (tool.getName().equals(name))
576                 {
577                     i.remove();
578                 }
579             }
580 
581             buildToolsIndex();
582         }
583     }
584 
585     /*** This method recreates the media name index for quick retrieval
586      *  by name.
587      */
588     private void buildToolsIndex()
589     {
590         Hashtable idx = new Hashtable();
591 
592         Iterator i = tools.iterator();
593         int count = 0;
594         while( i.hasNext() )
595         {
596             ToolDescriptor b = (ToolDescriptor)i.next();
597             idx.put( b.getName(), new Integer(count) );
598             count++;
599         }
600 
601         this.toolsIdx = idx;
602     }
603 
604 }