Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/activemq/security/jassjacc/AbstractJMSPermission.java


1   /** 
2    * 
3    * Copyright 2004 Hiram Chirino
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.activemq.security.jassjacc;
19  
20  import java.io.Serializable;
21  import java.security.Permission;
22  import java.util.Arrays;
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  /**
27   * Abstract class to make it easier to JMS Permissions.
28   * 
29   * @version $Revision: 1.1.1.1 $
30   */
31  abstract public class AbstractJMSPermission extends Permission implements Serializable {
32    
33    private String action;
34    private int cachedHashCode;
35    private HashSet actions;  
36    
37    /**
38     * @param name
39     */
40    public AbstractJMSPermission(String name, String action) {
41      super(name);
42      String listOfActions[]=normalize(action);
43      this.actions = new HashSet( Arrays.asList(listOfActions) );
44      if( !getValidSetOfActions().containsAll(actions) ) {
45        throw new IllegalArgumentException("The action set ("+actions+") is invalid. Valid set of actions are: "+getValidSetOfActions());
46      }    
47      this.action=join(listOfActions);
48    }
49  
50    abstract public Set getValidSetOfActions();
51  
52    static private String[] normalize(String action) {
53      String[] strings = action.split(",", -1);
54      Arrays.sort(strings);
55      return strings;
56    }
57    
58    static private String join(String action[]) {
59      StringBuffer buff = new StringBuffer();
60      for (int i = 0; i < action.length; i++) {
61        if( i!=0 )
62          buff.append(",");
63        buff.append(action[i]);
64      }
65      return buff.toString();
66    }
67  
68    /**
69     * @see java.security.Permission#hashCode()
70     */
71    public int hashCode() {
72      if (cachedHashCode == 0) {
73              cachedHashCode = getName().hashCode() ^ action.hashCode();
74          }
75          return cachedHashCode;  
76      }
77  
78    /**
79     * @see java.security.Permission#equals(java.lang.Object)
80     */
81    public boolean equals(Object o) {
82          if (o == null || !(o instanceof AbstractJMSPermission)) return false;
83  
84          AbstractJMSPermission other = (AbstractJMSPermission) o;
85          return getName().equals(other.getName()) && action.equals(other.action);  
86      }
87  
88    /**
89     * @see java.security.Permission#getActions()
90     */
91    public String getActions() {
92      return action;
93    }
94  
95    /**
96     * @see java.security.Permission#implies(java.security.Permission)
97     */
98    public boolean implies(Permission permission) {
99         if (permission == null || !(permission instanceof AbstractJMSPermission)) return false;
100 
101        AbstractJMSPermission other = (AbstractJMSPermission) permission;
102         return getName().equals(other.getName()) && other.actions.containsAll(this.actions);
103   }
104 }
105