📄 virtualrepository.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.
*/
/*
* Copyright (c) 2006, University of Kent
* 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.
*
* 1. Neither the name of the University of Kent nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 2. 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.
*
* 3. 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.
*
* 4. YOU AGREE THAT THE EXCLUSIONS IN PARAGRAPHS 2 AND 3 ABOVE ARE REASONABLE
* IN THE CIRCUMSTANCES. IN PARTICULAR, YOU ACKNOWLEDGE (1) THAT THIS
* SOFTWARE HAS BEEN MADE AVAILABLE TO YOU FREE OF CHARGE, (2) THAT THIS
* SOFTWARE IS NOT "PRODUCT" QUALITY, BUT HAS BEEN PRODUCED BY A RESEARCH
* GROUP WHO DESIRE TO MAKE THIS SOFTWARE FREELY AVAILABLE TO PEOPLE WHO WISH
* TO USE IT, AND (3) THAT BECAUSE THIS SOFTWARE IS NOT OF "PRODUCT" QUALITY
* IT IS INEVITABLE THAT THERE WILL BE BUGS AND ERRORS, AND POSSIBLY MORE
* SERIOUS FAULTS, IN THIS SOFTWARE.
*
* 5. This license is governed, except to the extent that local laws
* necessarily apply, by the laws of England and Wales.
*/
package issrg.utils.repository;
import java.util.Map;
import java.util.Hashtable;
import java.security.Principal;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
/**
* This is the class for representing the virtual repository of ACs and ACRLs
* for the push model. It can be used to store other attributes, too.
*
* @author A Otenko
* @version 1.0
*/
public class VirtualRepository implements AttributeRepository {
/**
* This is the actual repository. It contains the
* javax.naming.directory.Attributes
* objects indexed by the String entry DN.
*/
protected Map repository = new Hashtable();
/**
* This is the diagnostic message reference; it is null, if everything was OK
*/
protected final RepositoryException diagnosis=null;
public VirtualRepository() {
}
/**
* This method allows the caller to place the given value of the named
* attribute in the
* specified entry. If the entry does not exist, it is created. If the
* attribute does not exist, it is created. No checks for duplicate values are
* done.
*
* @param DN is the canonical LDAP DN of the entry
* @param attributeName is the name of the attribute
* @param value is the value to add to the attribute
*/
public void populate(String DN, String attributeName, Object value){
Attributes as;
DN = DN.toUpperCase();
if ((as=(Attributes)repository.get(DN))==null){
repository.put(DN, as=new BasicAttributes());
}
Attribute a = as.get(attributeName);
if (a==null){
as.put(a=new BasicAttribute(attributeName));
}
a.add(value);
}
/**
* This method allows the caller to access the Attributes of the given entry,
* which can be useful for updating the entry's contents (remove or replace
* attributes or
* their values). It is equal to the getAllAttributes with a few unapparent
* differences: it does not affect the repository status; it gives access to
* the repository, whilst getAllAttributes returns a <i>copy</i> of the data,
* so tampering with it is safe after getAllAttributes, and is not after
* get().
*
* @param DN is the canonical LDAP DN of the entry to retrieve
*
* @return the Attributes of that entry; is null if no such entry exists
*/
public Attributes get(String DN){
return (Attributes)repository.get(DN.toUpperCase());
}
/**
* This method destroys the whole entry: all the data is lost; the former
* contents of the entry is returned.
*
* @param DN is the canonical LDAP DN of the entry to remove
*
* @return the Attributes of that entry, the repository no longer contains
* any of the attributes in that entry; can be null, if no such entry
* existed
*/
public Attributes remove(String DN){
return (Attributes)repository.remove(DN);
}
/**
* This method returns the values of the requested attribute from a given
* entry.
*
* @param DN is the name of the entry
* @param attributeID is the name of the attribute to return
*
* @return the Attribute with values or null, if no such attribute was there
*/
public Attribute getAttribute(java.security.Principal DN, String attributeID) {
Attributes as = getAttributes(DN, new String[]{attributeID});
if (as==null){
return null; // we are working as if all the DNs are present in the Repository, only they can be empty
//throw diagnosis=new PbaException("No such entry exists");
}
//diagnosis=null;
return as.get(attributeID);
}
/**
* This method returns the attributes from the given entry.
*
* @param DN - the entry name; its getName() should return the canonical name
* @param attributeIDs - the array of attribute names; if null, all available
* attributes are returned
*
* @return the requested Attributes, or null, if no such entry exists
*/
public Attributes getAttributes(java.security.Principal DN, String [] attributeIDs) {
Attributes as = getAllAttributes(DN);
if (as==null){
return null;
//throw diagnosis=new PbaException("No such entry exists");
}
if (attributeIDs==null) return as; // it is already a clone
Attributes result = new BasicAttributes();
for (int i=0; i<attributeIDs.length; i++){
Attribute a = as.get(attributeIDs[i]);
if (a!=null){
// note that the way the repository is populated ensures that the names
// of the attributes are unique in any entry; therefore this put()
// operation always returns null
result.put(a);
}
}
//diagnosis=null;
return result;
}
/**
* This method returns all available attributes from the entry. It does the
* same as getAttributes(DN, null)
*
* @param DN - the entry name; its getName method should return the canonical
* name
*
* @return all available Attributes, or null, if no such entry exists
*/
public Attributes getAllAttributes(java.security.Principal DN) {
Attributes as = (Attributes)repository.get(DN.getName().toUpperCase());
if (as==null){
return null;
//throw diagnosis=new PbaException("No such entry exists: "+DN.getName());
}
//diagnosis=null;
return (Attributes)as.clone();
}
public int getStatus(){
return diagnosis==null? SUCCESS_STATUS: FAILURE_STATUS;
}
public Throwable getDiagnosis(){
return diagnosis;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -