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.producer.provider.driver;
22  
23  import java.util.HashMap;
24  import java.util.Iterator;
25  
26  import org.apache.wsrp4j.exception.WSRPException;
27  import org.apache.wsrp4j.log.LogManager;
28  import org.apache.wsrp4j.log.Logger;
29  import org.apache.wsrp4j.producer.provider.PortletRegistrationFilter;
30  import org.apache.wsrp4j.producer.provider.PortletRegistrationFilterWriter;
31  
32  /**
33   * This class implements the interfaces PortletRegistrationFilter and
34   * PortletRegistrationFilterWriter.
35   *
36   * @author <a href="mailto:stefan.behl@de.ibm.com">Stefan Behl</a>
37   * @author <a href="mailto:Ralf.Altrichter@de.ibm.com">Ralf Altrichter</a>
38   */
39  public class PortletRegistrationFilterImpl implements
40          PortletRegistrationFilter, PortletRegistrationFilterWriter {
41  
42      // a filter 
43      private static PortletRegistrationFilterImpl filter = null;
44  
45      // collects ConsumerPortletRegistration objects. RegistrationHandle
46      // is the key
47      private static HashMap consumerPortletRegistrationMap = new HashMap();
48  
49      // for logging and exception support
50      private Logger logger = LogManager.getLogManager().getLogger(
51              this.getClass());
52  
53      /**
54       * @return PortletRegistrationFilterWriter
55       */
56      public static PortletRegistrationFilterWriter createWriter() {
57          if (filter == null) {
58              filter = new PortletRegistrationFilterImpl();
59          }
60  
61          return filter;
62      }
63  
64      /**
65       * @return PortletRegistrationFilter
66       */
67      public static PortletRegistrationFilter createReader() {
68          if (filter == null) {
69              filter = new PortletRegistrationFilterImpl();
70          }
71  
72          return filter;
73      }
74  
75      /**
76       * Private constructor
77       */
78      private PortletRegistrationFilterImpl() {
79  
80          String MN = "Constructor";
81          if (logger.isLogging(Logger.TRACE_HIGH)) {
82              logger.entry(Logger.TRACE_HIGH, MN);
83          }
84  
85          if (logger.isLogging(Logger.TRACE_HIGH)) {
86              logger.exit(Logger.TRACE_HIGH, MN);
87          }
88      }
89  
90      /**
91       * Returns all portlet handles of portlets, a certain consumer (identified
92       * by regHandle) can utilize. Returns null if there are no entries for the provided
93       * regHandle.
94       *
95       * @param regHandle String representing the registration handle of a consumer
96       * 
97       * @return Iterator of portlet handles
98       */
99      public Iterator getAvailable(String regHandle) {
100         String MN = "getAvailable";
101         if (logger.isLogging(Logger.TRACE_HIGH)) {
102             logger.entry(Logger.TRACE_HIGH, MN);
103         }
104 
105         Iterator iter = null;
106 
107         if (consumerPortletRegistrationMap.containsKey(regHandle)) {
108             if (logger.isLogging(Logger.TRACE_MEDIUM)) {
109                 logger.text(Logger.TRACE_MEDIUM, MN, "RegistrationHandle "
110                         + regHandle + " found in Map.");
111             }
112 
113             ConsumerPortletRegistrationImpl cpr = (ConsumerPortletRegistrationImpl) consumerPortletRegistrationMap
114                     .get(regHandle);
115             iter = cpr.getPortletHandles().iterator();
116         }
117 
118         if (logger.isLogging(Logger.TRACE_HIGH)) {
119             logger.exit(Logger.TRACE_HIGH, MN);
120         }
121 
122         return iter;
123     }
124 
125     /**
126      * Indicates whether a certain consumer is allowed to utilize the portlet identified
127      * by portletHandle or not. Returns false if there is no entry for the provided handles.
128      *
129      * @param regHandle String representing the registration handle of a consumer
130      * @param portletHandle String representing the portlet handle of an portlet
131      * 
132      * @return boolean indicating whether the consumer corresponding to regHandle is
133      *         allowed to use the portlet identified by portletHandle
134      */
135     public boolean isAvailable(String regHandle, String portletHandle) {
136         String MN = "isAvailable";
137         if (logger.isLogging(Logger.TRACE_HIGH)) {
138             logger.entry(Logger.TRACE_HIGH, MN);
139         }
140 
141         boolean retVal = false;
142 
143         if (consumerPortletRegistrationMap.containsKey(regHandle)) {
144             if (logger.isLogging(Logger.TRACE_MEDIUM)) {
145                 logger.text(Logger.TRACE_MEDIUM, MN, "RegistrationHandle "
146                         + regHandle + "found in Map.");
147             }
148 
149             ConsumerPortletRegistrationImpl cpr = (ConsumerPortletRegistrationImpl) consumerPortletRegistrationMap
150                     .get(regHandle);
151             if (cpr.containsPortletHandle(portletHandle)) {
152                 retVal = true;
153 
154                 if (logger.isLogging(Logger.TRACE_MEDIUM)) {
155                     logger.text(Logger.TRACE_MEDIUM, MN, "PortletHandle "
156                             + portletHandle + "found for registration.");
157                 }
158             }
159         }
160 
161         if (logger.isLogging(Logger.TRACE_HIGH)) {
162             logger.exit(Logger.TRACE_HIGH, MN);
163         }
164 
165         return retVal;
166     }
167 
168     /**
169      * Makes a certain portlet (identified by portletHandle) available to a consumer
170      * (identified by regHandle). If there is no portlet in the portlet pool that
171      * corresponds to portletHandle, the method performs nothing. 
172      *
173      * @param regHandle String representing the registration handle of a consumer
174      * @param portletHandle String representing the portlet handle of a consumer
175      */
176     public void makeAvailable(String regHandle, String portletHandle)
177             throws WSRPException {
178         String MN = "makeAvailable";
179         if (logger.isLogging(Logger.TRACE_HIGH)) {
180             logger.entry(Logger.TRACE_HIGH, MN);
181         }
182 
183         ConsumerPortletRegistrationImpl cpr = null;
184 
185         if (!consumerPortletRegistrationMap.containsKey(regHandle)) {
186             cpr = new ConsumerPortletRegistrationImpl();
187             cpr.setRegistrationHandle(regHandle);
188             consumerPortletRegistrationMap.put(regHandle, cpr);
189         }
190         else {
191             cpr = (ConsumerPortletRegistrationImpl) consumerPortletRegistrationMap
192                     .get(regHandle);
193         }
194 
195         // add the current portlet handle to the registration
196         cpr.addPortletHandle(portletHandle);
197 
198         if (logger.isLogging(Logger.TRACE_HIGH)) {
199             logger.exit(Logger.TRACE_HIGH, MN);
200         }
201     }
202 
203     /**
204      * Makes several portlets (identified by portletHandles) available to
205      * a certain consumer (identified by regHandle). For portlet handles that do not
206      * correspond to portlets kept within the portlet pool, the method makes no
207      * availability-entry.
208      *
209      * @param regHandle String representing the registration handle of a consumer
210      * @param portletHandles Iterator containing some portlet handles of portlets
211      * 
212      * @throws WSRPException 
213      */
214     public void makeAvailable(String regHandle, Iterator portletHandles)
215             throws WSRPException {
216         String MN = "makeAvailable";
217         if (logger.isLogging(Logger.TRACE_HIGH)) {
218             logger.entry(Logger.TRACE_HIGH, MN);
219         }
220 
221         while (portletHandles.hasNext()) {
222             makeAvailable(regHandle, (String) portletHandles.next());
223         }
224 
225         if (logger.isLogging(Logger.TRACE_HIGH)) {
226             logger.exit(Logger.TRACE_HIGH, MN);
227         }
228     }
229 
230     /**
231      * Removes an entire entry for a certain consumer (identified by regHandle).
232      * If there are no entries for the provided regHandle, the method performs
233      * nothing. The method is useful when a consumer deregisters.
234      *
235      * @param regHandle String representing the registration handle of a consumer
236      */
237     public void remove(String regHandle) {
238         String MN = "remove";
239         if (logger.isLogging(Logger.TRACE_HIGH)) {
240             logger.entry(Logger.TRACE_HIGH, MN);
241         }
242 
243         if (consumerPortletRegistrationMap.containsKey(regHandle)) {
244             ConsumerPortletRegistrationImpl cpr = (ConsumerPortletRegistrationImpl) consumerPortletRegistrationMap
245                     .get(regHandle);
246 
247             consumerPortletRegistrationMap.remove(regHandle);
248         }
249 
250         if (logger.isLogging(Logger.TRACE_HIGH)) {
251             logger.exit(Logger.TRACE_HIGH, MN);
252         }
253 
254     }
255 
256     /**
257      * Abrogates the availability of a certain portlet (identified by portletHandle)
258      * regarding a certain consumer (identified by regHandle). If there is no entry
259      * for the provided regHandle and portletHandle, the method performs nothing.
260      *
261      * @param regHandle String representing the registration handle of a consumer
262      * @param portletHandle String representing the portlet handle of a consumer
263      */
264     public void remove(String regHandle, String portletHandle)
265             throws WSRPException {
266 
267         String MN = "remove";
268         if (logger.isLogging(Logger.TRACE_HIGH)) {
269             logger.entry(Logger.TRACE_HIGH, MN);
270         }
271 
272         if (consumerPortletRegistrationMap.containsKey(regHandle)) {
273             ConsumerPortletRegistrationImpl cpr = (ConsumerPortletRegistrationImpl) consumerPortletRegistrationMap
274                     .get(regHandle);
275             cpr.deletePortletHandle(portletHandle);
276 
277             // Is the registration list empty? No more Portlets are assigned?
278             if (cpr.isEmpty()) {
279                 // delete the registration list 
280                 consumerPortletRegistrationMap.remove(regHandle);
281             }
282         }
283 
284         if (logger.isLogging(Logger.TRACE_HIGH)) {
285             logger.exit(Logger.TRACE_HIGH, MN);
286         }
287 
288     }
289 
290     /**
291      * Abrogates the availability of several portlets (identified by portletHandles)
292      * regarding a certain consumer (identified by regHandle). For portlet handles
293      * that do not correspond to portlets kept within the portlet pool, the method
294      * performs nothing.
295      *
296      * @param regHandle String representing the registration handle of a consumer
297      * @param portletHandles Iterator containing some portlet handles of portlets
298      */
299     public void remove(String regHandle, Iterator portletHandles)
300             throws WSRPException {
301         String MN = "remove";
302         if (logger.isLogging(Logger.TRACE_HIGH)) {
303             logger.entry(Logger.TRACE_HIGH, MN);
304         }
305 
306         if (portletHandles != null) {
307             while (portletHandles.hasNext()) {
308                 remove(regHandle, (String) portletHandles.next());
309             }
310         }
311 
312         if (logger.isLogging(Logger.TRACE_HIGH)) {
313             logger.exit(Logger.TRACE_HIGH, MN);
314         }
315     }
316 
317 }