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