1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.wsrp;
24  
25  import java.util.Iterator;
26  import java.util.Map;
27  import java.util.concurrent.ConcurrentHashMap;
28  
29  import oasis.names.tc.wsrp.v1.types.RegistrationContext;
30  import oasis.names.tc.wsrp.v1.types.RegistrationData;
31  
32  import org.apache.wsrp4j.exception.WSRPException;
33  import org.apache.wsrp4j.log.LogManager;
34  import org.apache.wsrp4j.log.Logger;
35  import org.apache.wsrp4j.producer.ConsumerRegistry;
36  import org.apache.wsrp4j.producer.Registration;
37  import org.apache.wsrp4j.producer.driver.RegistrationImpl;
38  import org.apache.wsrp4j.producer.provider.DescriptionHandler;
39  import org.apache.wsrp4j.producer.provider.Provider;
40  import org.apache.wsrp4j.util.HandleGenerator;
41  import org.apache.wsrp4j.util.HandleGeneratorFactoryImpl;
42  
43  /**
44   * <a href="ConsumerRegistryImpl.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Michael Young
47   *
48   */
49  public class ConsumerRegistryImpl implements ConsumerRegistry {
50  
51      public ConsumerRegistryImpl(Provider provider) throws WSRPException {
52          String MN = "Constructor";
53          if (_logger.isLogging(Logger.TRACE_HIGH)) {
54              _logger.entry(Logger.TRACE_HIGH, MN);
55          }
56  
57          this._provider = provider;
58  
59          if (provider != null) {
60  
61              DescriptionHandler descrHandler = null;
62  
63              if ((descrHandler = provider.getDescriptionHandler()) != null) {
64                  _requiresRegistration = descrHandler.isRegistrationRequired();
65              }
66          }
67  
68          _genFactory = new HandleGeneratorFactoryImpl();
69          _generator = _genFactory.getHandleGenerator();
70  
71          _registrations = new ConcurrentHashMap();
72  
73          // restore registration objects from persistent file store
74          if (_logger.isLogging(Logger.TRACE_MEDIUM)) {
75              _logger.text(Logger.TRACE_MEDIUM, MN,
76                      "ConsumerRegistry successfully constructed.");
77          }
78  
79          if (_logger.isLogging(Logger.TRACE_HIGH)) {
80              _logger.exit(Logger.TRACE_HIGH, MN);
81          }
82  
83      }
84  
85      /**
86       * Provides information about whether this producer requires registration or
87       * not. Queries the DescriptionHandler to figure this out.
88       *
89       * @return A boolean indicating whether registration is required or not
90       */
91      public boolean isRegistrationRequired() {
92  
93          return this._requiresRegistration;
94  
95      }
96  
97      /**
98       * Creates a new registration-object for a certain consumer, adds it to the
99       * hashmap and returns it.
100      *
101      * @param registrationData
102      *            RegistrationData-object
103      *
104      * @return Registration Registration-object
105      *
106      * @throws WSRPException
107      */
108     public Registration register(RegistrationData registrationData)
109             throws WSRPException {
110 
111         String MN = "register";
112         if (_logger.isLogging(Logger.TRACE_HIGH)) {
113             _logger.entry(Logger.TRACE_HIGH, MN);
114         }
115 
116         Registration newRegistration = new RegistrationImpl();
117 
118         RegistrationContext newContext = new RegistrationContext();
119 
120         newContext.setRegistrationHandle(_generator.generateHandle());
121         newContext.setRegistrationState(null);
122         newContext.setExtensions(null);
123 
124         // set RegistrationData, RegistrationContext etc.
125         newRegistration.setRegistrationData(registrationData);
126         newRegistration.setRegistrationContext(newContext);
127 
128         // add new registration to hashmap
129         _registrations.put(newContext.getRegistrationHandle(), newRegistration);
130 
131         if (_logger.isLogging(Logger.TRACE_MEDIUM)) {
132             _logger.text(Logger.TRACE_MEDIUM, MN,
133                     "Consumer with registration handle: "
134                             + newContext.getRegistrationHandle()
135                             + " is registered");
136         }
137 
138         if (_logger.isLogging(Logger.TRACE_HIGH)) {
139             _logger.exit(Logger.TRACE_HIGH, MN);
140         }
141 
142         return newRegistration;
143     }
144 
145     /**
146      * Returns a certain registration identified by regHandle.
147      *
148      * @param regHandle
149      *            String representing the regHandle.
150      *
151      * @return Registration Registration-object identified by regHandle.
152      */
153     public Registration get(String regHandle) {
154         return (Registration) _registrations.get(regHandle);
155     }
156 
157     /**
158      * Returns all registrations (of all consumers) currently stored in the
159      * hashmap.
160      *
161      * @return Iterator of an registration collection containing all
162      *         registrations.
163      */
164     public Iterator getAll() {
165         return _registrations.values().iterator();
166     }
167 
168     /**
169      * Deletes the registration of a certain consumer (identified by regHandle)
170      * from the hashmap.
171      *
172      * @param regHandle
173      *            String representing the regHandle.
174      */
175     public void deregister(String regHandle) {
176 
177         String MN = "deregister";
178         if (_logger.isLogging(Logger.TRACE_HIGH)) {
179             _logger.entry(Logger.TRACE_HIGH, MN);
180         }
181 
182         _registrations.remove(regHandle);
183 
184         if (_logger.isLogging(Logger.TRACE_MEDIUM)) {
185             _logger.text(Logger.TRACE_MEDIUM, MN,
186                     "Consumer with registration handle: " + regHandle
187                             + " is now deregistered.");
188         }
189 
190         if (_logger.isLogging(Logger.TRACE_HIGH)) {
191             _logger.exit(Logger.TRACE_HIGH, MN);
192         }
193     }
194 
195     /**
196      * Evaluates whether a registration with regHandle exists or not. Returns
197      * true if registration exists, else false.
198      *
199      * @param regHandle
200      *            String representing the regHandle.
201      *
202      * @return Returns true if registration exists, else false.
203      */
204     public boolean check(String regHandle) {
205 
206         String MN = "check";
207         if (_logger.isLogging(Logger.TRACE_HIGH)) {
208             _logger.entry(Logger.TRACE_HIGH, MN);
209         }
210 
211         // check registration
212         boolean regExists = false;
213         if (_registrations.get(regHandle) != null) {
214             regExists = true;
215 
216             if (_logger.isLogging(Logger.TRACE_MEDIUM)) {
217                 _logger.text(Logger.TRACE_MEDIUM, MN,
218                         "Consumer with registration handle: " + regHandle
219                                 + " is registered");
220             }
221         }
222 
223         if (_logger.isLogging(Logger.TRACE_HIGH)) {
224             _logger.exit(Logger.TRACE_HIGH, MN);
225         }
226         return regExists;
227     }
228 
229     // initialized handle generator factory
230     private HandleGeneratorFactoryImpl _genFactory = null;
231 
232     // initialized handle factory
233     private HandleGenerator _generator = null;
234 
235     // indicates whether the consumer requires registration
236     private boolean _requiresRegistration = false;
237 
238     // refernce to the provider
239     private Provider _provider = null;
240 
241     // holds the actual consumer information
242     private Map _registrations = null;
243 
244     // log and trace support
245     private Logger _logger = LogManager.getLogManager().getLogger(
246             this.getClass());
247 
248 }