KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > core > authorization > permissions > DurationDecorator


1 /*
2 jGuard is a security framework based on top of jaas (java authentication and authorization security).
3 it is written for web applications, to resolve simply, access control problems.
4 version $Name: $
5 http://sourceforge.net/projects/jguard/
6
7 Copyright (C) 2004 Charles GAY
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23
24 jGuard project home page:
25 http://sourceforge.net/projects/jguard/
26
27 */

28 package net.sf.jguard.core.authorization.permissions;
29
30 import java.security.Permission JavaDoc;
31 import java.util.Date JavaDoc;
32
33 /**
34  * decorate java.security.Permission subclasses by defining a duration
35  * for their validity.
36  * @author <a HREF="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
37  */

38 public class DurationDecorator extends Permission JavaDoc {
39
40     private static final long serialVersionUID = 3085444057980849140L;
41     private Permission JavaDoc permission;
42     private Date JavaDoc begin;
43     private Date JavaDoc end;
44     
45     /**
46      *
47      * @param p permission to decorate
48      * @param begin begin date of the duration: can be <i>null</i> if there is no begin
49      * @param end end date of the duration : can be <i>null</i> if there is no end
50      */

51     public DurationDecorator(Permission JavaDoc p,Date JavaDoc start,Date JavaDoc end){
52         super(p.getName());
53         this.permission = p;
54         this.begin = start;
55         this.end = end;
56     }
57     
58     public boolean equals(Object JavaDoc obj) {
59         if (obj instanceof DurationDecorator) {
60             DurationDecorator duration = (DurationDecorator) obj;
61             if(this.permission.getName().equals(duration.getName())
62                 && this.permission.getActions().equals(duration.getActions())){
63                 return true;
64             }
65         }
66         return false;
67     }
68
69     public String JavaDoc getActions() {
70         return permission.getActions();
71     }
72
73     public int hashCode() {
74         int hashCode = permission.hashCode();
75         if(begin!= null){
76             hashCode += begin.hashCode();
77         }
78         if(end!= null){
79             hashCode += end.hashCode();
80         }
81         return hashCode;
82         
83     }
84
85     public boolean implies(Permission JavaDoc permission) {
86         Date JavaDoc now = new Date JavaDoc();
87         if(begin != null && now.before(begin)){
88             return false;
89         }
90         if(end != null && now.after(end)){
91             return false;
92         }
93         //duration check succeed,
94
//so we check in a classic way the permission
95
return this.permission.implies(permission);
96     }
97
98 }
99
Popular Tags