📄 setofsubsetscredentials.java
字号:
/*
* Copyright (c) 2000-2005, University of Salford
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the University of Salford nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package issrg.pba.rbac;
import java.util.Vector;
import issrg.pba.Credentials;
/**
* This class implements the Credentials interface, as needed for representing
* the set of subsets.
*
* <p>The object can contain a set of any credentials and tell if it contains
* other credentials and build unions and intersections with other credentials.
*
* @author E Ball
* @author A Otenko
* @author D Chadwick
* @version 0.2
*/
public class SetOfSubsetsCredentials implements Credentials{
private Vector Creds=new Vector();
/**
* This constructor creates an object with no credentials: an empty set of
* credentials.
*/
public SetOfSubsetsCredentials(){}
/**
* This constructor builds a Credential object from an array of Credentials.
*
* @param Creds is the array of credentials to be contained in this set
*/
public SetOfSubsetsCredentials(Credentials [] Creds){
for(int i=0;i<Creds.length;i++){
this.Creds.add(Creds[i]);
}
}
/**
* This constructor also builds the set from the given array of credentials,
* but the array of credentials is supplied as a vector.
*
* @param creds is the Vector of Credential objects to be included in the set
*/
public SetOfSubsetsCredentials(Vector creds){
Creds = new Vector(creds);
}
/**
* This method returns the set of credentials
*
* @return the Vector of credentials in this set; cannot be null, but can be
* empty; all elements are of type Credential
*/
public Vector getValue (){
return new Vector(Creds);
}
/**
* This method checks to see if the given set of credentials is contained
* in this object. That is, for each Credential contained in the set there is
* at least one Credential that can grant more or the same privilege.
*
* @param subSet the credentials object (even a set of subsets) that must be a
* sub-set
* of this object
*
* @return true, if <code>subSet</code> is fully contained within this object;
* false, otherwise
*/
public boolean contains(Credentials subSet){
return _contains_(subSet, false);
}
/**
* This method tells if this set of subsets is a part of the given
* credentials object.
* It is used by the SubsetCredentials class.
*
* @param superSet is the credentials object that must be a superset of
* this set
*
* @return true, if the given set of credentials contains this set
*/
public boolean partOf(Credentials superSet){
return _contains_(superSet, true);
}
/**
* This is a private method that decides if a given subset is contained within
* this object or vice versa, depending on reverse. If reverse is false, then
* it calculates "contains"; otherwise - "partOf".
*
* @param subSet is the credential against which to compare
* @param reverse tells which of the credentials must be a superset; if true,
* then the subSet must be a superset of this credential; if false, this
* credential must be a superset of the subSet
*
* @return boolean result; true if the needed set contains the other
*/
private boolean _contains_(Credentials subSet, boolean reverse){
Vector v;
//System.out.println("\t_contains_("+subSet+","+reverse+")"); //***
if (subSet instanceof SetOfSubsetsCredentials){
v = ((SetOfSubsetsCredentials)subSet).getValue();
}else{
v = new java.util.Vector();
if (subSet!=null) v.add(subSet);
}
Vector lhs = reverse?v:Creds;
Vector rhs = reverse?Creds:v;
//System.out.println("\t\tlhs: "+lhs+"\n\t\trhs: "+rhs); //****
cont_loop:
for (int i=rhs.size(); i-->0; ){
Credentials c = (Credentials)rhs.get(i);
for (int j=lhs.size(); j-->0; ){
if (((Credentials)lhs.get(j)).contains(c)){
// found a credential that is greater than c
continue cont_loop;
}
}
// one credential with no greater credential has been found
return false;
}
// we are here only if for each of the credentials of the rhs there
// is a greater credential in the lhs set.
return true;
}
/**
* This method checks to see if the supplied credentials (roles) are equal
* to the credentials contained in this object. a.equals(b) and b.equals(a)
* are both equivalent to (a.contains(b) && b.contains(a)).
*
* @param creds is the credentials object to compare to
*
* @return true if this credentials object is equal to the given set
*/
public boolean equals(Credentials creds){
return contains(creds) && creds.contains(this);
}
/**
* This method compares this credentials object to another object. First it checks if
* that object is a credentials object, then calls <code>equals(Credentials)</code>.
*
* @param o is the object to compare to
*
* @return true, if o is a credentials object and it is equal to this set
*
* @see equals(Credentials)
*/
public boolean equals(Object o){
if (o instanceof Credentials){
return equals((Credentials)o);
}
return false;
}
/**
* This method creates a copy of this credentials set.
*
* @return the SetOfSubsetsCredentials
*/
public Object clone(){
return intersection(this);
}
/**
* This method builds an intersection of this credentials set with another set.
*
* <p>The method just intersects each component of the stored set with the given set and
* collects the result in a credentials object.
*
* @param set is the set of credentials to intersect with
*
* @return the Credentials object that is the intersection of the two sets; it is
* contained by both of the sets
*/
public Credentials intersection(Credentials set){
Vector alien;
if (set instanceof SetOfSubsetsCredentials){
alien = ((SetOfSubsetsCredentials)set).getValue();
}else{
alien = new Vector();
if (set!=null) alien.add(set);
}
Vector r = new Vector();
int credsize = Creds.size();
int aliensize = alien.size();
for (int i = 0; i < credsize; i++) {
Credentials c = (Credentials) Creds.get(i);
Credentials m = null;
for (int j = 0; j < aliensize; j++) {
Credentials c1 = ((Credentials) alien.get(j)).intersection(c);
if (c1 != null) {
if (m == null) {
m = c1;
} else {
if (c1.contains(m)) {
m = c1;
} else {
if ((!c1.contains(m)) && (!m.contains(c1))) {
m = m.union(c1);
}
}
}
}
}
if (m != null) {
r.add(m);
}
}
credsize = r.size();
Vector l = new Vector();
for (int i = 0; i < credsize; i++) {
Credentials c = (Credentials) r.get(i);
Credentials temp = null;
for (int j = 0; j < l.size(); j++) {
temp = (Credentials) l.get(j);
if ((c.contains(temp)) && (!c.equals(temp))) {
l.remove(j);
}
}
if (temp == null) {
l.add(c.clone());
} else {
if (!temp.contains(c)) l.add(c.clone());
}
}
SetOfSubsetsCredentials a = new SetOfSubsetsCredentials(l);
if (l.isEmpty()) return a;
while (a.getValue().get(0) instanceof issrg.pba.rbac.SetOfSubsetsCredentials) a = (SetOfSubsetsCredentials) a.getValue().get(0);
return a;
//
// for (int i=Creds.size(); i-->0;){
// Credentials c = (Credentials)Creds.get(i);
// Credentials m = null;
//
// for(int j=alien.size(); j-->0;){
// Credentials c1 = ((Credentials)alien.get(j)).intersection(c);
// if (c1!=null){
// c=c1;
// m=c1;
//
// }
// }
//
// if (m!=null){
// r.add(m);
// }
// }
// return new SetOfSubsetsCredentials(r);
}
/**
* This method returns a union of this credential set with another one.
*
* <p>It just collects all the credentials from the given set that are not
* contained in any of the elements of the other set.
*
* @param set is the set to join with
*
* @return the Credential set representing the union of the two sets; it
* contains both of the sets
*/
public Credentials union(Credentials set){
Vector alien;
//System.err.println("\t\t*** JOINING SETS OF CREDS ***"); //*****
if (set instanceof SetOfSubsetsCredentials){
alien = ((SetOfSubsetsCredentials)set).getValue();
}else{
alien = new Vector();
if (set!=null) alien.add(set);
}
Vector r = new Vector();
Vector p = Creds;
//System.err.println("\t"+p+"\n\t"+alien); //*****
while (p!=null){
//System.err.println("\t\t"+p); //*****
for(int i=p.size(); i-->0;){
Credentials c = (Credentials)p.get(i);
for (int j=r.size(); j-->0;){
//System.err.println(c+" contains "+((Credentials)r.get(j))+": "+c.contains((Credentials)r.get(j))); //***************
if (c.contains((Credentials)r.get(j))){
r.remove(j);
}
}
r.add(c.clone()); //Tuan Anh: This command seems not enough. Look at intersection function.
}
//System.err.println("\t\t resulted in: "+r); //*****
p=alien;
alien=null;
}
return new SetOfSubsetsCredentials(r);
}
public String toString(){
return "set of subsets "+(Creds==null?"null":Creds.toString());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -