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.registration.Consumer;
026: import org.jboss.portal.registration.ConsumerGroup;
027: import org.jboss.portal.registration.NoSuchRegistrationException;
028: import org.jboss.portal.registration.RegistrationException;
029: import org.jboss.portal.registration.RegistrationStatus;
030:
031: import java.util.Collection;
032: import java.util.Collections;
033: import java.util.HashMap;
034: import java.util.Map;
035:
036: /**
037: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
038: * @version $Revision:5672 $
039: */
040: public class ConsumerGroupImpl implements ConsumerGroup {
041:
042: private String name;
043: private Map consumers;
044: private RegistrationStatus status;
045:
046: private ConsumerGroupImpl() {
047: init();
048: }
049:
050: ConsumerGroupImpl(String name) {
051: this .name = name;
052: init();
053: }
054:
055: private void init() {
056: this .consumers = new HashMap();
057: status = RegistrationStatus.PENDING;
058: }
059:
060: public String getName() {
061: return name;
062: }
063:
064: public boolean equals(Object o) {
065: if (this == o) {
066: return true;
067: }
068: if (o == null || getClass() != o.getClass()) {
069: return false;
070: }
071:
072: ConsumerGroupImpl that = (ConsumerGroupImpl) o;
073:
074: return name.equals(that.name);
075: }
076:
077: public int hashCode() {
078: return name.hashCode();
079: }
080:
081: public RegistrationStatus getStatus() {
082: return status;
083: }
084:
085: public void setStatus(RegistrationStatus status) {
086: ParameterValidation.throwIllegalArgExceptionIfNull(status,
087: "RegistrationStatus");
088: this .status = status;
089: }
090:
091: public Collection getConsumers() throws RegistrationException {
092: return Collections.unmodifiableCollection(consumers.values());
093: }
094:
095: public Consumer getConsumer(String consumerId)
096: throws IllegalArgumentException, RegistrationException {
097: ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(
098: consumerId, "Consumer name", null);
099: return (Consumer) consumers.get(consumerId);
100: }
101:
102: public boolean isEmpty() {
103: return consumers.isEmpty();
104: }
105:
106: public void addConsumer(Consumer consumer)
107: throws RegistrationException {
108: ParameterValidation.throwIllegalArgExceptionIfNull(consumer,
109: "Consumer");
110: String identity = consumer.getId();
111: if (consumers.containsKey(identity)) {
112: throw new IllegalArgumentException("ConsumerGroup named '"
113: + name + "' already contains a Consumer named '"
114: + consumer.getName() + "' (identity: '" + identity
115: + "')");
116: }
117:
118: consumers.put(identity, consumer);
119: consumer.setGroup(this );
120: }
121:
122: public void removeConsumer(Consumer consumer)
123: throws RegistrationException {
124: ParameterValidation.throwIllegalArgExceptionIfNull(consumer,
125: "Consumer");
126:
127: if (consumers.remove(consumer.getId()) == null) {
128: throw new NoSuchRegistrationException(
129: "ConsumerGroup named '" + name
130: + "' does not contain a Consumer named '"
131: + consumer.getName() + "' (identity: '"
132: + consumer.getId() + "')");
133: }
134:
135: consumer.setGroup(null);
136: }
137:
138: public boolean contains(Consumer consumer) {
139: ParameterValidation.throwIllegalArgExceptionIfNull(consumer,
140: "Consumer");
141:
142: return consumers.containsKey(consumer.getId());
143: }
144: }
|