001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.registration.impl;
023:
024: import org.jboss.logging.Logger;
025: import org.jboss.portal.common.util.ParameterValidation;
026: import org.jboss.portal.jems.as.system.AbstractJBossService;
027: import org.jboss.portal.registration.Consumer;
028: import org.jboss.portal.registration.ConsumerGroup;
029: import org.jboss.portal.registration.InvalidConsumerDataException;
030: import org.jboss.portal.registration.NoSuchRegistrationException;
031: import org.jboss.portal.registration.PropertyDescription;
032: import org.jboss.portal.registration.Registration;
033: import org.jboss.portal.registration.RegistrationException;
034: import org.jboss.portal.registration.RegistrationManager;
035: import org.jboss.portal.registration.RegistrationPersistenceManager;
036: import org.jboss.portal.registration.RegistrationPolicy;
037: import org.jboss.portal.registration.RegistrationStatus;
038:
039: import javax.xml.namespace.QName;
040: import java.util.ArrayList;
041: import java.util.Collection;
042: import java.util.Collections;
043: import java.util.Iterator;
044: import java.util.Map;
045:
046: /**
047: * @author <a href="mailto:chris.laprun@jboss.com">Chris Laprun</a>
048: * @version $Revision: 9177 $
049: * @since 2.6
050: */
051: public class RegistrationManagerImpl extends AbstractJBossService
052: implements RegistrationManager {
053: private static final Logger log = Logger
054: .getLogger(RegistrationManager.class);
055:
056: private RegistrationPolicy policy;
057: private RegistrationPersistenceManager persistenceManager;
058:
059: public RegistrationManagerImpl() {
060: }
061:
062: public RegistrationPolicy getPolicy() {
063: return policy;
064: }
065:
066: public void setPolicy(RegistrationPolicy policy) {
067: this .policy = policy;
068: policy.setManager(this );
069: }
070:
071: public RegistrationPersistenceManager getPersistenceManager() {
072: return persistenceManager;
073: }
074:
075: public void setPersistenceManager(
076: RegistrationPersistenceManager persistenceManager) {
077: this .persistenceManager = persistenceManager;
078: }
079:
080: public Registration addRegistrationTo(String consumerName,
081: Map registrationProperties, boolean createConsumerIfNeeded)
082: throws RegistrationException {
083: // the policy determines the identity of the consumer based on the given information (note that this might be obsoleted by using WS-Security)
084: String identity = policy.getConsumerIdFrom(consumerName,
085: registrationProperties);
086:
087: // validate the registration information
088: policy.validateRegistrationDataFor(registrationProperties,
089: identity);
090:
091: Consumer consumer = getOrCreateConsumer(identity,
092: createConsumerIfNeeded, consumerName);
093:
094: // create the actual registration
095: Registration registration = persistenceManager
096: .addRegistrationFor(identity, registrationProperties);
097:
098: // let the policy decide what the handle should be
099: String handle = policy.createRegistrationHandleFor(registration
100: .getId());
101: registration.setRegistrationHandle(handle);
102:
103: return registration;
104: }
105:
106: public Consumer createConsumer(String name)
107: throws RegistrationException, InvalidConsumerDataException {
108: // check with policy if we allow the consumer
109: policy.validateConsumerName(name);
110:
111: String identity = policy.getConsumerIdFrom(name,
112: Collections.EMPTY_MAP);
113:
114: Consumer consumer = persistenceManager.createConsumer(identity,
115: name);
116:
117: // deal with group if needed
118: // let the policy decide if there should be a group associated with the Consumer and if yes, with which id
119: String groupName = policy.getAutomaticGroupNameFor(name);
120: if (groupName != null) {
121: addConsumerToGroupNamed(name, groupName, true, false);
122: }
123:
124: return consumer;
125: }
126:
127: public Consumer addConsumerToGroupNamed(String consumerName,
128: String groupName, boolean createGroupIfNeeded,
129: boolean createConsumerIfNeeded)
130: throws RegistrationException {
131: // check with the policy if we allow the group name in case we need to create it
132: if (createGroupIfNeeded) {
133: policy.validateConsumerGroupName(groupName);
134: }
135:
136: // check with policy if we allow the consumer name in case we need to create it
137: if (createConsumerIfNeeded) {
138: policy.validateConsumerName(consumerName);
139: }
140:
141: ConsumerGroup group = getConsumerGroup(groupName);
142: boolean justCreatedGroup = false;
143: if (group == null) {
144: if (createGroupIfNeeded) {
145: createConsumerGroup(groupName);
146: justCreatedGroup = true;
147: } else {
148: throw new NoSuchRegistrationException(
149: "There is no existing ConsumerGroup named '"
150: + groupName + "'.");
151: }
152: }
153:
154: String identity = policy.getConsumerIdFrom(consumerName,
155: Collections.EMPTY_MAP);
156: try {
157: getOrCreateConsumer(identity, createConsumerIfNeeded,
158: consumerName);
159: } catch (NoSuchRegistrationException e) {
160: if (justCreatedGroup) {
161: removeConsumerGroup(groupName);
162: }
163: }
164:
165: return persistenceManager.addConsumerToGroupNamed(identity,
166: groupName);
167: }
168:
169: public ConsumerGroup createConsumerGroup(String groupName)
170: throws RegistrationException {
171: // check with the policy if we allow the group
172: policy.validateConsumerGroupName(groupName);
173:
174: return persistenceManager.createConsumerGroup(groupName);
175: }
176:
177: public void removeConsumer(String identity)
178: throws RegistrationException, NoSuchRegistrationException {
179: Consumer consumer = getOrCreateConsumer(identity, false, null);
180:
181: ConsumerGroup group = consumer.getGroup();
182: if (group != null) {
183: group.removeConsumer(consumer);
184: }
185:
186: // cascade delete the registrations
187: for (Iterator i = new ArrayList(consumer.getRegistrations())
188: .iterator(); i.hasNext();) {
189: removeRegistration((Registration) i.next());
190: }
191:
192: // let the registry do the actual deletion
193: persistenceManager.removeConsumer(identity);
194: }
195:
196: public void removeConsumer(Consumer consumer)
197: throws RegistrationException, NoSuchRegistrationException {
198: ParameterValidation.throwIllegalArgExceptionIfNull(consumer,
199: "Consumer");
200:
201: removeConsumer(consumer.getId());
202: }
203:
204: public Consumer getConsumerByIdentity(String identity)
205: throws RegistrationException {
206: ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(
207: identity, "identity", null);
208: return persistenceManager.getConsumerById(identity);
209: }
210:
211: public Consumer getConsumerFor(String registrationHandle)
212: throws RegistrationException {
213: return (Consumer) getConsumerOrRegistration(registrationHandle,
214: true);
215: }
216:
217: public Registration getRegistration(String registrationHandle)
218: throws RegistrationException {
219: return (Registration) getConsumerOrRegistration(
220: registrationHandle, false);
221: }
222:
223: public void removeRegistration(String registrationHandle)
224: throws RegistrationException, NoSuchRegistrationException {
225: Registration registration = getRegistration(registrationHandle);
226: if (registration == null) {
227: throw new NoSuchRegistrationException(
228: "There is no Registration with handle '"
229: + registrationHandle + "'");
230: }
231: removeRegistration(registration);
232: }
233:
234: public void removeRegistration(Registration registration)
235: throws RegistrationException {
236: ParameterValidation.throwIllegalArgExceptionIfNull(
237: registration, "Registration");
238:
239: registration.setStatus(RegistrationStatus.INVALID); // just in case...
240: // registration.clearAssociatedState(); // todo: do we need to clear associated state?
241:
242: persistenceManager.removeRegistration(registration.getId());
243: }
244:
245: public ConsumerGroup getConsumerGroup(String groupName)
246: throws RegistrationException {
247: ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(
248: groupName, "ConsumerGroup name", null);
249: return persistenceManager.getConsumerGroup(groupName);
250: }
251:
252: private Consumer getOrCreateConsumer(String identity,
253: boolean createConsumerIfNeeded, String consumerName)
254: throws RegistrationException {
255: Consumer consumer = getConsumerByIdentity(identity);
256: if (consumer == null) {
257: if (createConsumerIfNeeded) {
258: consumer = createConsumer(consumerName);
259: } else {
260: throw new NoSuchRegistrationException(
261: "There is no Consumer named '" + consumerName
262: + "'.");
263: }
264: }
265: return consumer;
266: }
267:
268: private Object getConsumerOrRegistration(String registrationHandle,
269: boolean getConsumer) {
270: ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(
271: registrationHandle, "registration handle", null);
272:
273: Registration registration = persistenceManager
274: .getRegistration(registrationHandle);
275: if (registration == null) {
276: return null;
277: } else {
278: return getConsumer ? registration.getConsumer()
279: : registration;
280: }
281: }
282:
283: public Collection getConsumerGroups() {
284: return persistenceManager.getConsumerGroups();
285: }
286:
287: public void removeConsumerGroup(ConsumerGroup group)
288: throws RegistrationException {
289: ParameterValidation.throwIllegalArgExceptionIfNull(group,
290: "ConsumerGroup");
291:
292: for (Iterator consumers = group.getConsumers().iterator(); consumers
293: .hasNext();) {
294: removeConsumer((Consumer) consumers.next());
295: }
296:
297: persistenceManager.removeConsumerGroup(group.getName());
298: }
299:
300: public void removeConsumerGroup(String name)
301: throws RegistrationException {
302: ParameterValidation.throwIllegalArgExceptionIfNull(name,
303: "ConsumerGroup name");
304: removeConsumerGroup(getConsumerGroup(name));
305: }
306:
307: public Collection getConsumers() {
308: return persistenceManager.getConsumers();
309: }
310:
311: public void clear() throws RegistrationException {
312: Collection tmpColl = new ArrayList(getConsumers());
313: for (Iterator consumers = tmpColl.iterator(); consumers
314: .hasNext();) {
315: removeConsumer((Consumer) consumers.next());
316: }
317:
318: tmpColl = new ArrayList(getConsumerGroups());
319: for (Iterator groups = tmpColl.iterator(); groups.hasNext();) {
320: removeConsumerGroup((ConsumerGroup) groups.next());
321: }
322: }
323:
324: /**
325: * We listen to registration property changes on the producer configuration so that we can invalidate the current
326: * registrations. Consumers will need to call modifyRegistration since properties have changed... which requires
327: * throwing an OperationFailedFault... not an InvalidRegistrationFault!
328: *
329: * @param registrationProperties
330: */
331: public void propertiesHaveChanged(
332: Map<QName, ? extends PropertyDescription> registrationProperties) {
333: log
334: .debug("Registration properties have changed, existing registrations will be invalidated...");
335: Collection registrations = persistenceManager
336: .getRegistrations();
337: for (Object registration : registrations) {
338: Registration reg = (Registration) registration;
339:
340: // pending instead of invalid as technically, the registration is not yet invalid
341: reg.setStatus(RegistrationStatus.PENDING);
342: // reg.clearAssociatedState(); //todo: do we need to clear the associated state? If we do, should we wait until current operations are done?
343: }
344:
345: // make policy aware of new registration properties
346: policy.setExpectations(registrationProperties);
347: }
348:
349: /**
350: * We listen for RegistrationPolicy changes so that we can provide the proper behavior at all time if the policy has
351: * been changed by users since this RegistrationManager was initialized...
352: *
353: * @param policy
354: */
355: public void policyUpdatedTo(RegistrationPolicy policy) {
356: setPolicy(policy);
357: }
358: }
|