KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > ac > impl > AbstractAccreditableManager


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: AbstractAccreditableManager.java 43241 2004-08-16 16:36:57Z andreas $ */
19
20 package org.apache.lenya.ac.impl;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.avalon.framework.activity.Disposable;
27 import org.apache.avalon.framework.logger.AbstractLogEnabled;
28 import org.apache.lenya.ac.AccessControlException;
29 import org.apache.lenya.ac.AccreditableManager;
30 import org.apache.lenya.ac.GroupManager;
31 import org.apache.lenya.ac.IPRangeManager;
32 import org.apache.lenya.ac.Item;
33 import org.apache.lenya.ac.ItemManagerListener;
34 import org.apache.lenya.ac.RoleManager;
35 import org.apache.lenya.ac.UserManager;
36
37 /**
38  * Abstract base class for accreditable managers.
39  */

40 public abstract class AbstractAccreditableManager
41     extends AbstractLogEnabled
42     implements AccreditableManager, ItemManagerListener, Disposable {
43
44     private UserManager userManager = null;
45     private GroupManager groupManager = null;
46     private IPRangeManager ipRangeManager = null;
47     private RoleManager roleManager = null;
48
49     private List JavaDoc itemManagerListeners = new ArrayList JavaDoc();
50
51     /**
52      * Attaches an item manager listener to this accreditable manager.
53      *
54      * @param listener An item manager listener.
55      */

56     public void addItemManagerListener(ItemManagerListener listener) {
57         if (!itemManagerListeners.contains(listener)) {
58             
59             if (getLogger().isDebugEnabled()) {
60                 getLogger().debug("Adding listener: [" + listener + "]");
61             }
62             
63             itemManagerListeners.add(listener);
64         }
65     }
66
67     /**
68      * Removes an item manager listener from this accreditable manager.
69      *
70      * @param listener An item manager listener.
71      */

72     public void removeItemManagerListener(ItemManagerListener listener) {
73         if (getLogger().isDebugEnabled()) {
74             getLogger().debug("Removing listener: [" + listener + "]");
75         }
76             
77         itemManagerListeners.remove(listener);
78     }
79
80     /**
81      * Notifies the listeners that an item was added.
82      *
83      * @param item The item that was added.
84      * @throws AccessControlException when a notified listener threw this exception.
85      */

86     protected void notifyAdded(Item item) throws AccessControlException {
87         List JavaDoc clone = new ArrayList JavaDoc(itemManagerListeners);
88         for (Iterator JavaDoc i = clone.iterator(); i.hasNext();) {
89             ItemManagerListener listener = (ItemManagerListener) i.next();
90             listener.itemAdded(item);
91         }
92     }
93
94     /**
95      * Notifies the listeners that an item was removed.
96      *
97      * @param item The item that was removed.
98      * @throws AccessControlException when a notified listener threw this exception.
99      */

100     protected void notifyRemoved(Item item) throws AccessControlException {
101         
102         List JavaDoc clone = new ArrayList JavaDoc(itemManagerListeners);
103         for (Iterator JavaDoc i = clone.iterator(); i.hasNext();) {
104             ItemManagerListener listener = (ItemManagerListener) i.next();
105             listener.itemRemoved(item);
106         }
107     }
108
109     /**
110      * @see org.apache.lenya.ac.ItemManagerListener#itemAdded(org.apache.lenya.ac.Item)
111      */

112     public void itemAdded(Item item) throws AccessControlException {
113         if (getLogger().isDebugEnabled()) {
114             getLogger().debug("Item was added: [" + item + "] - notifying listeners");
115         }
116         notifyAdded(item);
117     }
118
119     /**
120      * @see org.apache.lenya.ac.ItemManagerListener#itemRemoved(org.apache.lenya.ac.Item)
121      */

122     public void itemRemoved(Item item) throws AccessControlException {
123         if (getLogger().isDebugEnabled()) {
124             getLogger().debug("Item was removed: [" + item + "] - notifying listeners");
125         }
126         notifyRemoved(item);
127     }
128
129     /**
130      * @see org.apache.avalon.framework.activity.Disposable#dispose()
131      */

132     public void dispose() {
133         if (userManager != null) {
134             userManager.removeItemManagerListener(this);
135         }
136         if (groupManager != null) {
137             groupManager.removeItemManagerListener(this);
138         }
139         if (ipRangeManager != null) {
140             this.ipRangeManager.removeItemManagerListener(this);
141         }
142         if (roleManager != null) {
143             this.roleManager.removeItemManagerListener(this);
144         }
145         
146         if (getLogger().isDebugEnabled()) {
147             getLogger().debug("Disposing [" + this + "]");
148         }
149     }
150
151     /**
152      * @see org.apache.lenya.ac.AccreditableManager#getUserManager()
153      */

154     public UserManager getUserManager() throws AccessControlException {
155         if (userManager == null) {
156             userManager = initializeUserManager();
157             userManager.addItemManagerListener(this);
158         }
159         return userManager;
160     }
161
162     /**
163      * @see org.apache.lenya.ac.AccreditableManager#getGroupManager()
164      */

165     public GroupManager getGroupManager() throws AccessControlException {
166         if (groupManager == null) {
167             groupManager = initializeGroupManager();
168             groupManager.addItemManagerListener(this);
169         }
170         return groupManager;
171     }
172
173     /**
174      * @see org.apache.lenya.ac.AccreditableManager#getRoleManager()
175      */

176     public RoleManager getRoleManager() throws AccessControlException {
177         if (roleManager == null) {
178             roleManager = initializeRoleManager();
179             roleManager.addItemManagerListener(this);
180         }
181         return roleManager;
182     }
183
184     /**
185      * @see org.apache.lenya.ac.AccreditableManager#getIPRangeManager()
186      */

187     public IPRangeManager getIPRangeManager() throws AccessControlException {
188         if (ipRangeManager == null) {
189             ipRangeManager = initializeIPRangeManager();
190             ipRangeManager.addItemManagerListener(this);
191         }
192         return ipRangeManager;
193     }
194
195     /**
196      * Initializes the group manager.
197      *
198      * @return A group manager.
199      * @throws AccessControlException when something went wrong.
200      */

201     protected abstract GroupManager initializeGroupManager() throws AccessControlException;
202
203     /**
204      * Initializes the IP range manager.
205      *
206      * @return An IP range manager.
207      * @throws AccessControlException when something went wrong.
208      */

209     protected abstract IPRangeManager initializeIPRangeManager() throws AccessControlException;
210
211     /**
212      * Initializes the role manager.
213      *
214      * @return A role manager.
215      * @throws AccessControlException when something went wrong.
216      */

217     protected abstract RoleManager initializeRoleManager() throws AccessControlException;
218
219     /**
220      * Initializes the user manager.
221      *
222      * @return A user manager.
223      * @throws AccessControlException when something went wrong.
224      */

225     protected abstract UserManager initializeUserManager() throws AccessControlException;
226
227 }
228
Popular Tags