001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /* $Id: AbstractGroup.java 485769 2006-12-11 17:41:23Z andreas $ */
020:
021: package org.apache.lenya.ac.impl;
022:
023: import java.util.HashSet;
024: import java.util.Set;
025:
026: import org.apache.avalon.framework.logger.Logger;
027: import org.apache.lenya.ac.AccessControlException;
028: import org.apache.lenya.ac.Accreditable;
029: import org.apache.lenya.ac.Group;
030: import org.apache.lenya.ac.Groupable;
031: import org.apache.lenya.ac.ItemManager;
032:
033: /**
034: * A group is a set of {@link Groupable}s.
035: */
036: public abstract class AbstractGroup extends AbstractItem implements
037: Accreditable, Group {
038:
039: /**
040: * Creates a new group.
041: * @param itemManager The item manager.
042: * @param logger The logger.
043: */
044: public AbstractGroup(ItemManager itemManager, Logger logger) {
045: super (itemManager, logger);
046: }
047:
048: /**
049: * Creates a new group.
050: * @param itemManager The item manager.
051: * @param logger The logger.
052: * @param id The group ID.
053: */
054: public AbstractGroup(ItemManager itemManager, Logger logger,
055: String id) {
056: super (itemManager, logger);
057: setId(id);
058: }
059:
060: private Set members = new HashSet();
061:
062: /**
063: * Returns the members of this group.
064: * @return An array of {@link Groupable}s.
065: */
066: public Groupable[] getMembers() {
067: return (Groupable[]) this .members
068: .toArray(new Groupable[this .members.size()]);
069: }
070:
071: /**
072: * Adds a member to this group.
073: * @param member The member to add.
074: */
075: public void add(Groupable member) {
076: assert (member != null) && !this .members.contains(member);
077: this .members.add(member);
078: member.addedToGroup(this );
079: }
080:
081: /**
082: * Removes a member from this group.
083: * @param member The member to remove.
084: */
085: public void remove(Groupable member) {
086: assert (member != null) && this .members.contains(member);
087: this .members.remove(member);
088: member.removedFromGroup(this );
089: }
090:
091: /**
092: * Removes all members from this group.
093: */
094: public void removeAllMembers() {
095: Groupable[] _members = getMembers();
096: for (int i = 0; i < _members.length; i++) {
097: remove(_members[i]);
098: }
099: }
100:
101: /**
102: * Returns if this group contains this member.
103: * @param member The member to check.
104: * @return A boolean value.
105: */
106: public boolean contains(Groupable member) {
107: return this .members.contains(member);
108: }
109:
110: /**
111: * @see org.apache.lenya.ac.Accreditable#getAccreditables()
112: */
113: public Accreditable[] getAccreditables() {
114: Accreditable[] accreditables = { this };
115: return accreditables;
116: }
117:
118: /**
119: * Delete a group
120: * @throws AccessControlException if the delete failed
121: */
122: public void delete() throws AccessControlException {
123: removeAllMembers();
124: }
125:
126: }
|