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: AbstractAccreditableManager.java 499783 2007-01-25 13:33:38Z andreas $ */
020:
021: package org.apache.lenya.ac.impl;
022:
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import org.apache.avalon.framework.container.ContainerUtil;
028: import org.apache.avalon.framework.logger.AbstractLogEnabled;
029: import org.apache.avalon.framework.logger.Logger;
030: import org.apache.lenya.ac.AccessControlException;
031: import org.apache.lenya.ac.AccreditableManager;
032: import org.apache.lenya.ac.GroupManager;
033: import org.apache.lenya.ac.IPRangeManager;
034: import org.apache.lenya.ac.Item;
035: import org.apache.lenya.ac.ItemManagerListener;
036: import org.apache.lenya.ac.RoleManager;
037: import org.apache.lenya.ac.UserManager;
038:
039: /**
040: * Abstract base class for accreditable managers.
041: */
042: public abstract class AbstractAccreditableManager extends
043: AbstractLogEnabled implements AccreditableManager,
044: ItemManagerListener {
045:
046: /**
047: * @param logger The logger.
048: */
049: public AbstractAccreditableManager(Logger logger) {
050: ContainerUtil.enableLogging(this , logger);
051: }
052:
053: private UserManager userManager = null;
054: private GroupManager groupManager = null;
055: private IPRangeManager ipRangeManager = null;
056: private RoleManager roleManager = null;
057:
058: private List itemManagerListeners = new ArrayList();
059:
060: /**
061: * Attaches an item manager listener to this accreditable manager.
062: * @param listener An item manager listener.
063: */
064: public void addItemManagerListener(ItemManagerListener listener) {
065: if (!this .itemManagerListeners.contains(listener)) {
066:
067: if (getLogger().isDebugEnabled()) {
068: getLogger()
069: .debug("Adding listener: [" + listener + "]");
070: }
071:
072: this .itemManagerListeners.add(listener);
073: }
074: }
075:
076: /**
077: * Removes an item manager listener from this accreditable manager.
078: * @param listener An item manager listener.
079: */
080: public void removeItemManagerListener(ItemManagerListener listener) {
081: if (getLogger().isDebugEnabled()) {
082: getLogger().debug("Removing listener: [" + listener + "]");
083: }
084:
085: this .itemManagerListeners.remove(listener);
086: }
087:
088: /**
089: * Notifies the listeners that an item was added.
090: * @param item The item that was added.
091: * @throws AccessControlException when a notified listener threw this exception.
092: */
093: protected void notifyAdded(Item item) throws AccessControlException {
094: List clone = new ArrayList(this .itemManagerListeners);
095: for (Iterator i = clone.iterator(); i.hasNext();) {
096: ItemManagerListener listener = (ItemManagerListener) i
097: .next();
098: listener.itemAdded(item);
099: }
100: }
101:
102: /**
103: * Notifies the listeners that an item was removed.
104: * @param item The item that was removed.
105: * @throws AccessControlException when a notified listener threw this exception.
106: */
107: protected void notifyRemoved(Item item)
108: throws AccessControlException {
109: List clone = new ArrayList(this .itemManagerListeners);
110: for (Iterator i = clone.iterator(); i.hasNext();) {
111: ItemManagerListener listener = (ItemManagerListener) i
112: .next();
113: listener.itemRemoved(item);
114: }
115: }
116:
117: /**
118: * @see org.apache.lenya.ac.ItemManagerListener#itemAdded(org.apache.lenya.ac.Item)
119: */
120: public void itemAdded(Item item) throws AccessControlException {
121: if (getLogger().isDebugEnabled()) {
122: getLogger().debug(
123: "Item was added: [" + item
124: + "] - notifying listeners");
125: }
126: notifyAdded(item);
127: }
128:
129: /**
130: * @see org.apache.lenya.ac.ItemManagerListener#itemRemoved(org.apache.lenya.ac.Item)
131: */
132: public void itemRemoved(Item item) throws AccessControlException {
133: if (getLogger().isDebugEnabled()) {
134: getLogger().debug(
135: "Item was removed: [" + item
136: + "] - notifying listeners");
137: }
138: notifyRemoved(item);
139: }
140:
141: /**
142: * @see org.apache.lenya.ac.AccreditableManager#getUserManager()
143: */
144: public UserManager getUserManager() throws AccessControlException {
145: if (this .userManager == null) {
146: this .userManager = initializeUserManager();
147: this .userManager.addItemManagerListener(this );
148: }
149: return this .userManager;
150: }
151:
152: /**
153: * @see org.apache.lenya.ac.AccreditableManager#getGroupManager()
154: */
155: public GroupManager getGroupManager() throws AccessControlException {
156: if (this .groupManager == null) {
157: this .groupManager = initializeGroupManager();
158: this .groupManager.addItemManagerListener(this );
159: }
160: return this .groupManager;
161: }
162:
163: /**
164: * @see org.apache.lenya.ac.AccreditableManager#getRoleManager()
165: */
166: public RoleManager getRoleManager() throws AccessControlException {
167: if (this .roleManager == null) {
168: this .roleManager = initializeRoleManager();
169: this .roleManager.addItemManagerListener(this );
170: }
171: return this .roleManager;
172: }
173:
174: /**
175: * @see org.apache.lenya.ac.AccreditableManager#getIPRangeManager()
176: */
177: public IPRangeManager getIPRangeManager()
178: throws AccessControlException {
179: if (this .ipRangeManager == null) {
180: this .ipRangeManager = initializeIPRangeManager();
181: this .ipRangeManager.addItemManagerListener(this );
182: }
183: return this .ipRangeManager;
184: }
185:
186: /**
187: * Initializes the group manager.
188: *
189: * @return A group manager.
190: * @throws AccessControlException when something went wrong.
191: */
192: protected abstract GroupManager initializeGroupManager()
193: throws AccessControlException;
194:
195: /**
196: * Initializes the IP range manager.
197: *
198: * @return An IP range manager.
199: * @throws AccessControlException when something went wrong.
200: */
201: protected abstract IPRangeManager initializeIPRangeManager()
202: throws AccessControlException;
203:
204: /**
205: * Initializes the role manager.
206: *
207: * @return A role manager.
208: * @throws AccessControlException when something went wrong.
209: */
210: protected abstract RoleManager initializeRoleManager()
211: throws AccessControlException;
212:
213: /**
214: * Initializes the user manager.
215: *
216: * @return A user manager.
217: * @throws AccessControlException when something went wrong.
218: */
219: protected abstract UserManager initializeUserManager()
220: throws AccessControlException;
221:
222: }
|