1   /**
2    * Copyright (c) 2000-2007 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 com.liferay.util.CollectionFactory;
26  
27  import java.util.Iterator;
28  import java.util.Map;
29  
30  import oasis.names.tc.wsrp.v1.types.RegistrationContext;
31  import oasis.names.tc.wsrp.v1.types.RegistrationData;
32  
33  import org.apache.wsrp4j.exception.WSRPException;
34  import org.apache.wsrp4j.log.LogManager;
35  import org.apache.wsrp4j.log.Logger;
36  import org.apache.wsrp4j.producer.ConsumerRegistry;
37  import org.apache.wsrp4j.producer.Registration;
38  import org.apache.wsrp4j.producer.driver.RegistrationImpl;
39  import org.apache.wsrp4j.producer.provider.DescriptionHandler;
40  import org.apache.wsrp4j.producer.provider.Provider;
41  import org.apache.wsrp4j.util.HandleGenerator;
42  import org.apache.wsrp4j.util.HandleGeneratorFactoryImpl;
43  
44  /**
45   * <a href="ConsumerRegistryImpl.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Michael Young
48   *
49   */
50  public class ConsumerRegistryImpl implements ConsumerRegistry {
51  
52      public ConsumerRegistryImpl(Provider provider) throws WSRPException {
53          String MN = "Constructor";
54          if (_logger.isLogging(Logger.TRACE_HIGH)) {
55              _logger.entry(Logger.TRACE_HIGH, MN);
56          }
57  
58          this._provider = provider;
59  
60          if (provider != null) {
61  
62              DescriptionHandler descrHandler = null;
63  
64              if ((descrHandler = provider.getDescriptionHandler()) != null) {
65                  _requiresRegistration = descrHandler.isRegistrationRequired();
66              }
67          }
68  
69          _genFactory = new HandleGeneratorFactoryImpl();
70          _generator = _genFactory.getHandleGenerator();
71  
72          _registrations = CollectionFactory.getSyncHashMap();
73  
74          // restore registration objects from persistent file store
75          if (_logger.isLogging(Logger.TRACE_MEDIUM)) {
76              _logger.text(Logger.TRACE_MEDIUM, MN,
77                      "ConsumerRegistry successfully constructed.");
78          }
79  
80          if (_logger.isLogging(Logger.TRACE_HIGH)) {
81              _logger.exit(Logger.TRACE_HIGH, MN);
82          }
83  
84      }
85  
86      /**
87       * Provides information about whether this producer requires registration or
88       * not. Queries the DescriptionHandler to figure this out.
89       *
90       * @return A boolean indicating whether registration is required or not
91       */
92      public boolean isRegistrationRequired() {
93  
94          return this._requiresRegistration;
95  
96      }
97  
98      /**
99       * Creates a new registration-object for a certain consumer, adds it to the
100      * hashmap and returns it.
101      *
102      * @param registrationData
103      *            RegistrationData-object
104      *
105      * @return Registration Registration-object
106      *
107      * @throws WSRPException
108      */
109     public Registration register(RegistrationData registrationData)
110             throws WSRPException {
111 
112         String MN = "register";
113         if (_logger.isLogging(Logger.TRACE_HIGH)) {
114             _logger.entry(Logger.TRACE_HIGH, MN);
115         }
116 
117         Registration newRegistration = new RegistrationImpl();
118 
119         RegistrationContext newContext = new RegistrationContext();
120 
121         newContext.setRegistrationHandle(_generator.generateHandle());
122         newContext.setRegistrationState(null);
123         newContext.setExtensions(null);
124 
125         // set RegistrationData, RegistrationContext etc.
126         newRegistration.setRegistrationData(registrationData);
127         newRegistration.setRegistrationContext(newContext);
128 
129         // add new registration to hashmap
130         _registrations.put(newContext.getRegistrationHandle(), newRegistration);
131 
132         if (_logger.isLogging(Logger.TRACE_MEDIUM)) {
133             _logger.text(Logger.TRACE_MEDIUM, MN,
134                     "Consumer with registration handle: "
135                             + newContext.getRegistrationHandle()
136                             + " is registered");
137         }
138 
139         if (_logger.isLogging(Logger.TRACE_HIGH)) {
140             _logger.exit(Logger.TRACE_HIGH, MN);
141         }
142 
143         return newRegistration;
144     }
145 
146     /**
147      * Returns a certain registration identified by regHandle.
148      *
149      * @param regHandle
150      *            String representing the regHandle.
151      *
152      * @return Registration Registration-object identified by regHandle.
153      */
154     public Registration get(String regHandle) {
155         return (Registration) _registrations.get(regHandle);
156     }
157 
158     /**
159      * Returns all registrations (of all consumers) currently stored in the
160      * hashmap.
161      *
162      * @return Iterator of an registration collection containing all
163      *         registrations.
164      */
165     public Iterator getAll() {
166         return _registrations.values().iterator();
167     }
168 
169     /**
170      * Deletes the registration of a certain consumer (identified by regHandle)
171      * from the hashmap.
172      *
173      * @param regHandle
174      *            String representing the regHandle.
175      */
176     public void deregister(String regHandle) {
177 
178         String MN = "deregister";
179         if (_logger.isLogging(Logger.TRACE_HIGH)) {
180             _logger.entry(Logger.TRACE_HIGH, MN);
181         }
182 
183         _registrations.remove(regHandle);
184 
185         if (_logger.isLogging(Logger.TRACE_MEDIUM)) {
186             _logger.text(Logger.TRACE_MEDIUM, MN,
187                     "Consumer with registration handle: " + regHandle
188                             + " is now deregistered.");
189         }
190 
191         if (_logger.isLogging(Logger.TRACE_HIGH)) {
192             _logger.exit(Logger.TRACE_HIGH, MN);
193         }
194     }
195 
196     /**
197      * Evaluates whether a registration with regHandle exists or not. Returns
198      * true if registration exists, else false.
199      *
200      * @param regHandle
201      *            String representing the regHandle.
202      *
203      * @return Returns true if registration exists, else false.
204      */
205     public boolean check(String regHandle) {
206 
207         String MN = "check";
208         if (_logger.isLogging(Logger.TRACE_HIGH)) {
209             _logger.entry(Logger.TRACE_HIGH, MN);
210         }
211 
212         // check registration
213         boolean regExists = false;
214         if (_registrations.get(regHandle) != null) {
215             regExists = true;
216 
217             if (_logger.isLogging(Logger.TRACE_MEDIUM)) {
218                 _logger.text(Logger.TRACE_MEDIUM, MN,
219                         "Consumer with registration handle: " + regHandle
220                                 + " is registered");
221             }
222         }
223 
224         if (_logger.isLogging(Logger.TRACE_HIGH)) {
225             _logger.exit(Logger.TRACE_HIGH, MN);
226         }
227         return regExists;
228     }
229 
230     // initialized handle generator factory
231     private HandleGeneratorFactoryImpl _genFactory = null;
232 
233     // initialized handle factory
234     private HandleGenerator _generator = null;
235 
236     // indicates whether the consumer requires registration
237     private boolean _requiresRegistration = false;
238 
239     // refernce to the provider
240     private Provider _provider = null;
241 
242     // holds the actual consumer information
243     private Map _registrations = null;
244 
245     // log and trace support
246     private Logger _logger = LogManager.getLogManager().getLogger(
247             this.getClass());
248 
249 }