KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > kernel > security > authorization > MantaPermissionCache


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Shirley Sasson.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46
47 package org.mr.kernel.security.authorization;
48
49 import org.mr.MantaAgent;
50 import org.mr.kernel.security.SecurityConfigurationPaths;
51 import org.mr.kernel.security.SecurityConstants;
52 import org.apache.commons.logging.Log;
53 import org.apache.commons.logging.LogFactory;
54
55 import java.util.Map JavaDoc;
56 import java.util.HashMap JavaDoc;
57
58 /**
59  * This class represents a cache for permissions that were already read from the ACL storage.
60  *
61  * @version 1.0
62  * @since May 9, 2006
63  * @author Shirley Sasson
64  *
65  */

66 public class MantaPermissionCache extends MantaCache implements SecurityConfigurationPaths, SecurityConstants {
67     private Log _logger;
68
69     // key = PermissionKeyEntry, value=AuthorizationValue
70
private Map JavaDoc _cache;
71
72     /**
73      * Constructs a new instance of MantaPermissionCache.
74      *
75      */

76     public MantaPermissionCache(){
77         _cache = new HashMap JavaDoc();
78         _timeToLive = MantaAgent.getInstance().getSingletonRepository().getConfigManager().getLongProperty(ACL + "." + PERMISSION_CACHE_TTL, DEFAULT_PERMISSION_CACHE_TIME_TO_LIVE);
79         if (getLogger().isDebugEnabled())
80             getLogger().debug("[MantaPermissionCache] TTL is " + _timeToLive);
81     }
82
83     /**
84      * Adds a given entry to the cache.
85      *
86      * @param keyEntry the entry key
87      * @param authorizationValue the entry value
88      */

89     public void add(ACLKeyEntry keyEntry, AuthorizationValue authorizationValue){
90         if (!(keyEntry instanceof PermissionKeyEntry))
91             return;
92         PermissionKeyEntry permissionEntry = (PermissionKeyEntry) keyEntry;
93         if (getLogger().isDebugEnabled())
94             getLogger().debug("[add] Adding: key = [" + permissionEntry + "], value = [" + authorizationValue + "] to the permission cache.");
95         _cache.put(permissionEntry, authorizationValue);
96     }
97
98     /**
99      * Checks is a specific entry exists in the cahce. If it exists, its TTL value is checked to see if
100      * the entry is still valid.
101      *
102      * @param keyEntry the key entry to search
103      * @return true if the entry exists and has valid TTL
104      */

105      public AuthorizationValue isAuthorized(ACLKeyEntry keyEntry){
106         if (!(keyEntry instanceof PermissionKeyEntry))
107             return null;
108         PermissionKeyEntry permissionEntry = (PermissionKeyEntry) keyEntry;
109         if (_cache.containsKey(permissionEntry)){
110             AuthorizationValue value = (AuthorizationValue) _cache.get(permissionEntry);
111             if (value.isValid(_timeToLive)){
112                 if (getLogger().isDebugEnabled())
113                     getLogger().debug("[isAuthorized] AuthorizationValue for key = [" + permissionEntry + "] found and has valid TTL.");
114                 return value;
115             }
116         }
117         return null;
118      }
119
120     /**
121      * Determines whether or not an entry with the given key exists in the cache.
122      *
123      * @param keyEntry the entry key to search
124      * @return true if this entry exists in the cache, false otherwise
125      */

126     public boolean contains(ACLKeyEntry keyEntry){
127         if (!(keyEntry instanceof PermissionKeyEntry))
128             return false;
129         PermissionKeyEntry permissionEntry = (PermissionKeyEntry) keyEntry;
130         return _cache.containsKey(permissionEntry);
131     }
132
133     /**
134      * Returns the instance of the logger for this class
135      *
136      * @return the instance of the logger
137      */

138     public Log getLogger(){
139         if (_logger == null){
140             _logger = LogFactory.getLog(getClass().getName());
141         }
142         return _logger;
143     }
144 }
145
Popular Tags