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.portal.common.util.ParameterValidation;
025: import org.jboss.portal.jems.as.system.AbstractJBossService;
026: import org.jboss.portal.registration.Consumer;
027: import org.jboss.portal.registration.ConsumerGroup;
028: import org.jboss.portal.registration.DuplicateRegistrationException;
029: import org.jboss.portal.registration.NoSuchRegistrationException;
030: import org.jboss.portal.registration.Registration;
031: import org.jboss.portal.registration.RegistrationException;
032: import org.jboss.portal.registration.RegistrationPersistenceManager;
033: import org.jboss.portal.registration.RegistrationStatus;
034:
035: import java.util.Collection;
036: import java.util.Collections;
037: import java.util.HashMap;
038: import java.util.Map;
039:
040: /**
041: * @author <a href="mailto:chris.laprun@jboss.com">Chris Laprun</a>
042: * @version $Revision: 8784 $
043: * @since 2.6
044: */
045: public class RegistrationPersistenceManagerImpl extends
046: AbstractJBossService implements RegistrationPersistenceManager {
047: private long lastRegistrationId;
048: private Map consumers = new HashMap();
049: private Map groups = new HashMap();
050: private Map registrations = new HashMap();
051:
052: public Consumer createConsumer(String consumerId,
053: String consumerName) throws RegistrationException {
054: ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(
055: consumerId, "Consumer identity", null);
056: ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(
057: consumerName, "Consumer name", null);
058:
059: ConsumerImpl consumer = new ConsumerImpl(consumerId,
060: consumerName);
061: consumer.setStatus(RegistrationStatus.PENDING);
062: consumers.put(consumerId, consumer);
063:
064: return consumer;
065: }
066:
067: public ConsumerGroup getConsumerGroup(String name)
068: throws RegistrationException {
069: ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(name,
070: "ConsumerGroup name", null);
071:
072: return (ConsumerGroup) groups.get(name);
073: }
074:
075: public ConsumerGroup createConsumerGroup(String name)
076: throws RegistrationException {
077: ConsumerGroup group = getConsumerGroup(name);
078: if (group != null) {
079: throw new DuplicateRegistrationException(
080: "A ConsumerGroup named '" + name
081: + "' has already been registered.");
082: } else {
083: group = new ConsumerGroupImpl(name);
084: groups.put(name, group);
085: return group;
086: }
087: }
088:
089: public void removeConsumerGroup(String name)
090: throws RegistrationException {
091: ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(name,
092: "ConsumerGroup name", null);
093: if (groups.remove(name) == null) {
094: throw new NoSuchRegistrationException(
095: "There is no ConsumerGroup named '" + name + "'.");
096: }
097: }
098:
099: public void removeConsumer(String consumerId)
100: throws RegistrationException {
101: ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(
102: consumerId, "Consumer identity", null);
103: if (consumers.remove(consumerId) == null) {
104: throw new RegistrationException(
105: "There is no Consumer with identity '" + consumerId
106: + "'.");
107: }
108: }
109:
110: public void removeRegistration(String registrationId)
111: throws RegistrationException {
112: ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(
113: registrationId, "Registration identity", null);
114:
115: RegistrationImpl registration = (RegistrationImpl) registrations
116: .get(registrationId);
117: if (registration == null) {
118: throw new NoSuchRegistrationException(
119: "There is no Registration with id '"
120: + registrationId + "'");
121: }
122:
123: ConsumerImpl consumer = (ConsumerImpl) registration
124: .getConsumer();
125: consumer.removeRegistration(registration);
126: registrations.remove(registrationId);
127: }
128:
129: public Consumer getConsumerById(String consumerId)
130: throws RegistrationException {
131: ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(
132: consumerId, "Consumer identity", null);
133:
134: return (Consumer) consumers.get(consumerId);
135: }
136:
137: public Registration addRegistrationFor(String consumerId,
138: Map registrationProperties) throws RegistrationException {
139: ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(
140: consumerId, "Consumer identity", null);
141: ParameterValidation.throwIllegalArgExceptionIfNull(
142: registrationProperties, "Registration properties");
143:
144: ConsumerImpl consumer = (ConsumerImpl) getConsumerById(consumerId);
145: if (consumer == null) {
146: throw new NoSuchRegistrationException(
147: "There is no Consumer with identity '" + consumerId
148: + "' to add a Registration to...");
149: }
150:
151: RegistrationImpl registration = new RegistrationImpl(""
152: + lastRegistrationId++, consumer,
153: RegistrationStatus.PENDING, registrationProperties);
154: consumer.addRegistration(registration);
155:
156: registrations.put(registration.getId(), registration);
157:
158: return registration;
159: }
160:
161: public Consumer addConsumerToGroupNamed(String consumerId,
162: String groupName) throws RegistrationException {
163: ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(
164: consumerId, "Consumer identity", null);
165: ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(
166: groupName, "ConsumerGroup name", null);
167:
168: ConsumerGroupImpl group = (ConsumerGroupImpl) getConsumerGroup(groupName);
169: if (group == null) {
170: throw new NoSuchRegistrationException(
171: "There is no ConsumerGroup named '" + groupName
172: + "' to add a Consumer to...");
173: }
174:
175: ConsumerImpl consumer = (ConsumerImpl) getConsumerById(consumerId);
176: if (consumer == null) {
177: throw new NoSuchRegistrationException(
178: "There is no Consumer with identity '" + consumerId
179: + "' to add to ConsumerGroup named '"
180: + groupName + "'. Did you create it?");
181: }
182:
183: group.addConsumer(consumer);
184:
185: return consumer;
186: }
187:
188: public Collection getConsumers() {
189: return Collections.unmodifiableCollection(consumers.values());
190: }
191:
192: public Collection getRegistrations() {
193: return Collections.unmodifiableCollection(registrations
194: .values());
195: }
196:
197: public Collection getConsumerGroups() {
198: return Collections.unmodifiableCollection(groups.values());
199: }
200:
201: public Registration getRegistration(String registrationId) {
202: ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(
203: registrationId, "Registration id", null);
204:
205: return (Registration) registrations.get(registrationId);
206: }
207: }
|