001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.wsrp;
022:
023: import com.liferay.util.CollectionFactory;
024:
025: import java.util.Iterator;
026: import java.util.Map;
027:
028: import oasis.names.tc.wsrp.v1.types.RegistrationContext;
029: import oasis.names.tc.wsrp.v1.types.RegistrationData;
030:
031: import org.apache.wsrp4j.exception.WSRPException;
032: import org.apache.wsrp4j.log.LogManager;
033: import org.apache.wsrp4j.log.Logger;
034: import org.apache.wsrp4j.producer.ConsumerRegistry;
035: import org.apache.wsrp4j.producer.Registration;
036: import org.apache.wsrp4j.producer.driver.RegistrationImpl;
037: import org.apache.wsrp4j.producer.provider.DescriptionHandler;
038: import org.apache.wsrp4j.producer.provider.Provider;
039: import org.apache.wsrp4j.util.HandleGenerator;
040: import org.apache.wsrp4j.util.HandleGeneratorFactoryImpl;
041:
042: /**
043: * <a href="ConsumerRegistryImpl.java.html"><b><i>View Source</i></b></a>
044: *
045: * @author Michael Young
046: *
047: */
048: public class ConsumerRegistryImpl implements ConsumerRegistry {
049:
050: public ConsumerRegistryImpl(Provider provider) throws WSRPException {
051: String MN = "Constructor";
052: if (_logger.isLogging(Logger.TRACE_HIGH)) {
053: _logger.entry(Logger.TRACE_HIGH, MN);
054: }
055:
056: this ._provider = provider;
057:
058: if (provider != null) {
059:
060: DescriptionHandler descrHandler = null;
061:
062: if ((descrHandler = provider.getDescriptionHandler()) != null) {
063: _requiresRegistration = descrHandler
064: .isRegistrationRequired();
065: }
066: }
067:
068: _genFactory = new HandleGeneratorFactoryImpl();
069: _generator = _genFactory.getHandleGenerator();
070:
071: _registrations = CollectionFactory.getSyncHashMap();
072:
073: // restore registration objects from persistent file store
074: if (_logger.isLogging(Logger.TRACE_MEDIUM)) {
075: _logger.text(Logger.TRACE_MEDIUM, MN,
076: "ConsumerRegistry successfully constructed.");
077: }
078:
079: if (_logger.isLogging(Logger.TRACE_HIGH)) {
080: _logger.exit(Logger.TRACE_HIGH, MN);
081: }
082:
083: }
084:
085: /**
086: * Provides information about whether this producer requires registration or
087: * not. Queries the DescriptionHandler to figure this out.
088: *
089: * @return A boolean indicating whether registration is required or not
090: */
091: public boolean isRegistrationRequired() {
092:
093: return this ._requiresRegistration;
094:
095: }
096:
097: /**
098: * Creates a new registration-object for a certain consumer, adds it to the
099: * 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(),
130: 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: "
220: + regHandle + " 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: }
|