001 /**
002 *
003 * Copyright 2004 Hiram Chirino
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 **/
018 package org.activemq.security.jassjacc;
019
020 import java.io.Serializable;
021 import java.security.Permission;
022 import java.util.Arrays;
023 import java.util.HashSet;
024 import java.util.Set;
025
026 /**
027 * Abstract class to make it easier to JMS Permissions.
028 *
029 * @version $Revision: 1.1.1.1 $
030 */
031 abstract public class AbstractJMSPermission extends Permission implements Serializable {
032
033 private String action;
034 private int cachedHashCode;
035 private HashSet actions;
036
037 /**
038 * @param name
039 */
040 public AbstractJMSPermission(String name, String action) {
041 super(name);
042 String listOfActions[]=normalize(action);
043 this.actions = new HashSet( Arrays.asList(listOfActions) );
044 if( !getValidSetOfActions().containsAll(actions) ) {
045 throw new IllegalArgumentException("The action set ("+actions+") is invalid. Valid set of actions are: "+getValidSetOfActions());
046 }
047 this.action=join(listOfActions);
048 }
049
050 abstract public Set getValidSetOfActions();
051
052 static private String[] normalize(String action) {
053 String[] strings = action.split(",", -1);
054 Arrays.sort(strings);
055 return strings;
056 }
057
058 static private String join(String action[]) {
059 StringBuffer buff = new StringBuffer();
060 for (int i = 0; i < action.length; i++) {
061 if( i!=0 )
062 buff.append(",");
063 buff.append(action[i]);
064 }
065 return buff.toString();
066 }
067
068 /**
069 * @see java.security.Permission#hashCode()
070 */
071 public int hashCode() {
072 if (cachedHashCode == 0) {
073 cachedHashCode = getName().hashCode() ^ action.hashCode();
074 }
075 return cachedHashCode;
076 }
077
078 /**
079 * @see java.security.Permission#equals(java.lang.Object)
080 */
081 public boolean equals(Object o) {
082 if (o == null || !(o instanceof AbstractJMSPermission)) return false;
083
084 AbstractJMSPermission other = (AbstractJMSPermission) o;
085 return getName().equals(other.getName()) && action.equals(other.action);
086 }
087
088 /**
089 * @see java.security.Permission#getActions()
090 */
091 public String getActions() {
092 return action;
093 }
094
095 /**
096 * @see java.security.Permission#implies(java.security.Permission)
097 */
098 public boolean implies(Permission permission) {
099 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