KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > activedirectory > PrincipalContainer


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.activedirectory;
21
22 import java.io.File JavaDoc;
23 import java.io.Serializable JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.regex.Pattern JavaDoc;
28
29 import org.apache.commons.cache.BaseStorageListener;
30 import org.apache.commons.cache.Cache;
31 import org.apache.commons.cache.FileStash;
32 import org.apache.commons.cache.GroupMapImpl;
33 import org.apache.commons.cache.LRUEvictionPolicy;
34 import org.apache.commons.cache.MemoryStash;
35 import org.apache.commons.cache.SimpleCache;
36 import org.apache.commons.cache.Stash;
37 import org.apache.commons.cache.StorageListener;
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40
41 import com.sslexplorer.boot.ContextHolder;
42 import com.sslexplorer.boot.Util;
43 import com.sslexplorer.core.BundleActionMessage;
44 import com.sslexplorer.core.CoreUtil;
45 import com.sslexplorer.core.GlobalWarning;
46 import com.sslexplorer.policyframework.Principal;
47
48 public class PrincipalContainer<T extends Principal> {
49     private static final Log logger = LogFactory.getLog(PrincipalContainer.class);
50     private final int cacheSize;
51     private final boolean inMemoryCache;
52     private final String JavaDoc cacheType;
53     private final Cache principalCache;
54     
55     PrincipalContainer(int cacheSize, boolean inMemoryCache, String JavaDoc cacheType, String JavaDoc cacheFullMessageText) {
56         this.cacheSize = cacheSize;
57         this.inMemoryCache = inMemoryCache;
58         this.cacheType = cacheType;
59         principalCache = createCache(cacheFullMessageText);
60     }
61     
62     synchronized boolean containsPrincipal(String JavaDoc principalName) {
63         return retrievePrincipal(principalName) != null;
64     }
65     
66     @SuppressWarnings JavaDoc("unchecked")
67     synchronized T retrievePrincipal(String JavaDoc principalName) {
68         return (T) principalCache.retrieve(principalName);
69     }
70     
71     synchronized Collection JavaDoc<String JavaDoc> retrievePrincipalNames() {
72         Serializable JavaDoc[] keysForGroup = principalCache.getKeysForGroup(cacheType);
73         Collection JavaDoc<String JavaDoc> principalNames = new ArrayList JavaDoc<String JavaDoc>(keysForGroup.length);
74         for (Serializable JavaDoc principalName : keysForGroup) {
75             principalNames.add((String JavaDoc) principalName);
76         }
77         return principalNames;
78     }
79     
80     @SuppressWarnings JavaDoc("unchecked")
81     synchronized Collection JavaDoc<T> retrievePrincipals(String JavaDoc filter) {
82         String JavaDoc regex = Util.parseSimplePatternToRegExp(filter);
83         Pattern JavaDoc pattern = Pattern.compile(regex);
84
85         Serializable JavaDoc[] keysForGroup = principalCache.getKeysForGroup(cacheType);
86         Collection JavaDoc<T> principals = new HashSet JavaDoc<T>(keysForGroup.length);
87         for (Serializable JavaDoc principalName : keysForGroup) {
88             boolean matches = pattern.matcher((String JavaDoc) principalName).matches();
89             if (matches) {
90                 T retrieve = (T) principalCache.retrieve(principalName);
91                 if (retrieve != null) {
92                     principals.add(retrieve);
93                 }
94             }
95         }
96         
97         if (logger.isDebugEnabled()) {
98             if (principals.isEmpty()) {
99                 logger.debug("No principals in cache");
100             } else {
101                 logger.debug("Got " + principals.size() + " principals from the cache");
102             }
103         }
104         return principals;
105     }
106
107     synchronized String JavaDoc storePrincipal(T principal) {
108         String JavaDoc principalName = principal.getPrincipalName();
109         storePrinciple(principalName, principal);
110         return principalName;
111     }
112     
113     synchronized String JavaDoc storePrinciple(String JavaDoc key, T principal) {
114         if (logger.isDebugEnabled()) {
115             logger.debug("Caching " + principal);
116         }
117         principalCache.store(key, (Serializable JavaDoc) principal, Long.MAX_VALUE, null, cacheType);
118         return key;
119     }
120     
121     synchronized void updateRemovedPrincipals(Collection JavaDoc<String JavaDoc> missingPrincipals) {
122         for (String JavaDoc principalName : missingPrincipals) {
123             removePrincipal(principalName);
124         }
125     }
126     
127     public synchronized void removePrincipal(T principal) {
128         removePrincipal(principal.getPrincipalName());
129     }
130     
131     void removePrincipal(String JavaDoc principleName) {
132         principalCache.store(principleName, null, 0L, null, cacheType);
133     }
134     
135     synchronized void close() {
136         closeCache(principalCache);
137     }
138     
139     Cache createCache(String JavaDoc cacheFullMessageText) {
140         File JavaDoc cacheDirectory = new File JavaDoc(ContextHolder.getContext().getTempDirectory(), "cache");
141         File JavaDoc cacheTypeDirectory = new File JavaDoc(cacheDirectory, cacheType);
142         Stash stash = inMemoryCache ? new MemoryStash(cacheSize) : new FileStash(Long.MAX_VALUE, cacheSize, new File JavaDoc[]{cacheTypeDirectory}, true);
143         SimpleCache cache = new SimpleCache(stash, new LRUEvictionPolicy(), null, new GroupMapImpl());
144         cache.registerStorageListener(getStorageListener(cacheFullMessageText));
145         return cache;
146     }
147
148     private StorageListener getStorageListener(final String JavaDoc messageKey) {
149         return new BaseStorageListener() {
150             private static final long serialVersionUID = 4283488241230531541L;
151             private int storageCounter = 0;
152             private boolean addedWarning;
153
154             public synchronized void stored(Serializable JavaDoc arg0, Serializable JavaDoc arg1, Long JavaDoc arg2, Long JavaDoc arg3, Serializable JavaDoc arg4) {
155                 storageCounter++;
156             }
157
158             public synchronized void cleared(Serializable JavaDoc arg0) {
159                 if (storageCounter == cacheSize && !addedWarning) {
160                     BundleActionMessage message = new BundleActionMessage("activeDirectory", messageKey, String.valueOf(cacheSize));
161                     CoreUtil.addMultipleGlobalWarning(GlobalWarning.MANAGEMENT_USERS, message);
162                     addedWarning = true;
163                 }
164                 storageCounter--;
165             }
166
167             public synchronized void cleared() {
168                 storageCounter = 0;
169                 addedWarning = false;
170                 CoreUtil.removeGlobalWarningFromAllSessions(messageKey);
171             }
172         };
173     }
174     
175     static void closeCache(Cache cache) {
176         try {
177             cache.clear();
178             cache.unregisterStorageListeners();
179         } catch (Exception JavaDoc e) {
180             logger.error("Failed to close cache", e);
181         }
182     }
183 }
Popular Tags