KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jacc > test > portal > InstancePermission


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.jacc.test.portal;
23
24 import org.jboss.logging.Logger;
25
26 import java.io.Serializable JavaDoc;
27 import java.security.Permission JavaDoc;
28 import java.security.PermissionCollection JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30
31 /**
32  * Portal permission class for authorisation checks.
33  * <p>This class is an extension to the JACC mechanism to allow configurable and dynamically modifiable permissions.</p>
34  * <p/>
35  * Note: for now this is a final class. We might want to have subclasses of it later though.
36  *
37  * @author <a HREF="mailto:mholzner@novell.com">Martin Holzner</a>
38  * @version $Revision: 40338 $
39  */

40 public final class InstancePermission extends PortalPermission implements Serializable JavaDoc
41 {
42
43    private static Logger log = Logger.getLogger(InstancePermission.class);
44
45    /**
46     * The view action.
47     */

48    public static final String JavaDoc VIEW = "view";
49
50    private static final int VIEW_MASK = 0x00000001;
51
52    private final String JavaDoc uri;
53
54    private final int mask;
55
56    private final String JavaDoc actions;
57
58
59    private boolean trace;
60
61    /**
62     * Create a permission for the specified resource.
63     *
64     * @param uri handle of the resource that is being protected.
65     * @param actions the allowed actions (or the actions to check for access) as a comma separated list
66     * @throws IllegalArgumentException if the provided arguments are null or the actions string
67     * doesn't contain any valid actions
68     */

69    public InstancePermission(String JavaDoc uri, String JavaDoc actions)
70    {
71       super(uri);
72
73       //
74
if (uri == null || actions == null)
75       {
76          throw new IllegalArgumentException JavaDoc("Arguments must not be null [" + uri + "][" + actions + "]");
77       }
78
79       //
80
int mask = 0;
81       StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(actions, ",");
82       while (tokens.hasMoreTokens())
83       {
84          String JavaDoc action = tokens.nextToken().trim();
85
86          if (VIEW.equals(action))
87          {
88             mask |= VIEW_MASK;
89          }
90          else
91          {
92             log.warn("Unknown action in string [" + action + "] will be ignored");
93          }
94       }
95
96       //
97
this.mask = mask;
98       this.uri = uri;
99       this.actions = actions;
100       this.trace = log.isTraceEnabled();
101    }
102
103    public boolean equals(Object JavaDoc o)
104    {
105       if (this == o)
106       {
107          return true;
108       }
109       if (o == null || getClass() != o.getClass())
110       {
111          return false;
112       }
113
114       final InstancePermission that = (InstancePermission)o;
115
116       if (!uri.equals(that.uri))
117       {
118          return false;
119       }
120
121       if (that.mask != this.mask)
122       {
123          return false;
124       }
125
126       return true;
127    }
128
129    public int hashCode()
130    {
131       int result;
132       result = mask;
133       result = 29 * result + uri.hashCode();
134       return result;
135    }
136
137    public String JavaDoc toString()
138    {
139       return "InstancePermission[" + uri + "] [" + actions + "]";
140    }
141
142    public String JavaDoc getActions()
143    {
144       return actions;
145    }
146
147    public String JavaDoc getURI()
148    {
149       return uri;
150    }
151
152    public boolean implies(Permission JavaDoc permission)
153    {
154       if (trace)
155       {
156          log.trace("implies ? " + getURI() + ": [" + permission + "]");
157       }
158       if (permission instanceof InstancePermission)
159       {
160          InstancePermission other = (InstancePermission)permission;
161          return uri.equals(other.uri) && (this.mask & other.mask) == other.mask;
162       }
163       else
164       {
165          return false;
166       }
167    }
168
169    public PermissionCollection JavaDoc newPermissionCollection()
170    {
171       return new InstancePermissionCollection();
172    }
173
174    public String JavaDoc getType()
175    {
176       return "instance";
177    }
178 }
179
Popular Tags