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  /* 
18  
19   */
20  
21  package org.apache.wsrp4j.consumer.driver;
22  
23  import java.util.Hashtable;
24  import java.util.Iterator;
25  
26  import org.apache.wsrp4j.consumer.PortletKey;
27  import org.apache.wsrp4j.consumer.PortletRegistry;
28  import org.apache.wsrp4j.consumer.WSRPPortlet;
29  import org.apache.wsrp4j.exception.WSRPException;
30  
31  public abstract class GenericPortletRegistryImpl implements PortletRegistry {
32  
33      // maps portlet keys to portlets
34      private Hashtable portlets = null;
35  
36      public GenericPortletRegistryImpl() {
37          portlets = new Hashtable();
38      }
39  
40      /**
41       * Add a portlet to the registry
42       *
43       * @param portlet The portlet to add
44       */
45      public void addPortlet(WSRPPortlet portlet) throws WSRPException {
46          if (portlet != null) {
47  
48              portlets.put(portlet.getPortletKey().toString(), portlet);
49  
50          }
51      }
52  
53      /**
54       * Get the portlet for the given producer and portlet handle
55       *
56       * @param portletKey The portlet key identifying the portlet
57       *
58       * @return The portlet with the given portlet key
59       **/
60      public WSRPPortlet getPortlet(PortletKey portletKey) {
61  
62          if (portletKey == null)
63              return null;
64  
65          WSRPPortlet portlet = (WSRPPortlet) portlets.get(portletKey.toString());
66  
67          return portlet;
68      }
69  
70      /**
71       * Remove the portlet with the given portlet key
72       *
73       * @param portletKey The portlet key identifying the portlet
74       * @return The portlet which has been removed or null
75       **/
76      public WSRPPortlet removePortlet(PortletKey portletKey) {
77          if (portletKey == null)
78              return null;
79  
80          return (WSRPPortlet) portlets.remove(portletKey.toString());
81      }
82  
83      /**
84       *  Remove all portlets from the registry 
85       **/
86      public void removeAllPortlets() {
87          portlets.clear();
88      }
89  
90      /**
91       * Tests if a portlet with the given portlet key
92       * 
93       * @param portletKey The portlet key identifying the portlet
94       * 
95       * @return True if portlet exists with this portlet key
96       **/
97      public boolean existsPortlet(PortletKey portletKey) {
98          if (portletKey == null)
99              return false;
100 
101         return portlets.containsKey(portletKey.toString());
102     }
103 
104     /**
105      * Get all the portlets in the register
106      * 
107      * @return Iterator with all portlets in the registry
108      **/
109     public Iterator getAllPortlets() {
110         return portlets.values().iterator();
111     }
112 }