
java.org.jboss.security.acl.config.ACLConfiguration Maven / Gradle / Ivy
/*
* JBoss, Home of Professional Open Source.
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.security.acl.config;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jboss.security.acl.ACL;
import org.jboss.security.acl.ACLEntry;
import org.jboss.security.acl.ACLImpl;
/**
*
* The {@code ACLConfiguration} class contains all ACL definitions that have been specified in an XML file according to
* the {@code jboss-acl-configuration} schema. Each definition is used to construct an {@code ACL} instance that will
* protect the specified resource according to the permissions that are assigned to each identity.
*
*
* @author Stefan Guilhen
*/
public class ACLConfiguration
{
/** the set of ACL definitions keyed by their resource identifier. */
private final Map definitions = new HashMap();
/**
*
* Adds a parsed {@code ACLDefinition} object to the map of definitions.
*
*
* @param definition the {@code ACLDefinition} instance to be added.
*/
public void addDefinition(Object definition)
{
if (definition instanceof ACLBaseDefinition)
{
ACLBaseDefinition aclDefinition = (ACLBaseDefinition) definition;
this.definitions.put(aclDefinition.getResource(), aclDefinition);
}
}
/**
*
* Creates and returns the {@code ACL} objects that correspond to the {@code acl-definition}s specified in the XML
* configuration file.
*
*
* @return a {@code Set} containing the generated {@code ACL}s.
*/
public Set getConfiguredACLs()
{
Set configuredACLs = new HashSet();
for (ACLBaseDefinition definition : this.definitions.values())
{
Set entries = this.getEntries(definition, new ArrayList());
ACLImpl acl = new ACLImpl(definition.getResource(), entries);
configuredACLs.add(acl);
}
return configuredACLs;
}
/**
*
* This method retrieves the set of {@code ACLEntry} objects that belong to an ACL, recursively getting the entries
* from the parent definitions when the extension configuration is used. An extending {@code ACLDefinition}
* "inherits" the entries from its parent and is free to add or override entries as needed.
*
*
* @param definition the {@code ACLBaseDefinition} that contains the data used to retrieve the entries.
* @param visitedACLs a {@code List} of the visited ACLs to detect circular dependencies.
* @return a {@code Set} containing the entries that will be used to create an {@code ACL} according to the
* specified definition.
* @throws RuntimeException if a circular dependency is detected among the {@code ACLDefinition} objects.
*/
private Set getEntries(ACLBaseDefinition definition, List visitedACLs)
{
if (visitedACLs.contains(definition.getResource()))
throw new RuntimeException("Circular dependency between ACLs has been detected");
visitedACLs.add(definition.getResource());
if (definition.getBaseResource() != null)
{
ACLBaseDefinition superDefinition = this.definitions.get(definition.getBaseResource());
if (superDefinition != null)
{
Set superEntries = this.getEntries(superDefinition, visitedACLs);
Set entries = definition.getEntries();
entries.addAll(superEntries);
return entries;
}
else
{
throw new RuntimeException("Parent ACL not found: " + definition.getBaseResource());
}
}
else
{
return definition.getEntries();
}
}
}