KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > authentication > CachingAuthenticationScheme


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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 package org.outerj.daisy.authentication;
17
18 import org.outerj.daisy.authentication.AuthenticationScheme;
19 import org.outerj.daisy.authentication.AuthenticationException;
20 import org.outerj.daisy.repository.Credentials;
21 import org.outerj.daisy.repository.user.User;
22 import org.outerj.daisy.repository.user.UserManager;
23 import org.apache.commons.collections.map.LRUMap;
24
25 import java.util.Collections JavaDoc;
26 import java.util.Map JavaDoc;
27
28 /**
29  * A wrapper around an AuthenticationScheme that performs credential caching.
30  */

31 public class CachingAuthenticationScheme implements AuthenticationScheme {
32     private AuthenticationScheme delegate;
33     private long maxCacheDuration;
34     private Map cache;
35
36     /**
37      *
38      * @param maxCacheDuration max time an entry can stay in the cache before becoming invalid, in millis
39      * @param maxCacheSize maximum size of the cache (should be large enough to handle max expected concurrent users
40      * for optimal performance)
41      */

42     public CachingAuthenticationScheme(AuthenticationScheme delegate, long maxCacheDuration, int maxCacheSize) {
43         this.delegate = delegate;
44         this.maxCacheDuration = maxCacheDuration;
45         this.cache = Collections.synchronizedMap(new LRUMap(maxCacheSize));
46     }
47
48     public String JavaDoc getName() {
49         return delegate.getName();
50     }
51
52     public String JavaDoc getDescription() {
53         return delegate.getDescription();
54     }
55
56     public void clearCaches() {
57         cache.clear();
58     }
59
60     public boolean check(Credentials credentials) throws AuthenticationException {
61         String JavaDoc password = getFromCache(credentials.getLogin());
62
63         if (password != null && password.equals(credentials.getPassword()))
64             return true;
65
66         boolean valid = delegate.check(credentials);
67         if (valid) {
68             putInCache(credentials);
69             return true;
70         }
71
72         return false;
73     }
74
75     public User createUser(Credentials crendentials, UserManager userManager) throws AuthenticationException {
76         return delegate.createUser(crendentials, userManager);
77     }
78
79     private String JavaDoc getFromCache(String JavaDoc login) {
80         CacheEntry cacheEntry = (CacheEntry)cache.get(login);
81         if (cacheEntry != null) {
82             if ((System.currentTimeMillis() - cacheEntry.created) > maxCacheDuration) {
83                 cache.remove(login);
84                 return null;
85             }
86             return cacheEntry.password;
87         }
88         return null;
89     }
90
91     private void putInCache(Credentials credentials) {
92         cache.put(credentials.getLogin(), new CacheEntry(credentials.getPassword()));
93     }
94
95     private static class CacheEntry {
96         long created = System.currentTimeMillis();
97         String JavaDoc password;
98
99         CacheEntry(String JavaDoc password) {
100             this.password = password;
101         }
102     }
103 }
104
Popular Tags