Skip to content

Commit

Permalink
Target environment implementation for realm configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
Cedric Staub committed Jun 23, 2012
1 parent 60abea9 commit 7afea64
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/core/java/org/wyona/yanel/core/map/RealmDefaultImpl.java
Expand Up @@ -123,6 +123,8 @@ protected void configure(Configuration config) throws Exception {
name = nameConfigElement.getValue();
}

// Filter by target environment
config = ConfigurationUtil.filterEnvironment(config, yanel.getTargetEnvironment());

initIdentityManager(config, yanel);
initPolicyManager(config, yanel);
Expand Down Expand Up @@ -586,6 +588,7 @@ protected void initPolicyManager(Configuration config, Yanel yanel) throws Excep
*/
protected void initIdentityManager(Configuration config, Yanel yanel) throws Exception {
Configuration repoConfigElement = config.getChild("ac-identities", false);

if (repoConfigElement != null) {

IdentityManagerFactory imFactory = null;
Expand All @@ -596,7 +599,6 @@ protected void initIdentityManager(Configuration config, Yanel yanel) throws Exc
imFactory = (IdentityManagerFactory) Class.forName(customIdentityManagerFactoryImplClassName).newInstance();

// INFO: ConfigurationUtil generates a DOM Document with the root node called "identity-manager-config" which wraps/contains the custom indentities configuration
log.warn("TODO: Pass target environment in identity manager: " + yanel.getTargetEnvironment());
identityManager = imFactory.newIdentityManager(ConfigurationUtil.getCustomConfiguration(repoConfigElement, "identity-manager-config", "http://www.wyona.org/security/1.0"), new RealmConfigPathResolver(this));

log.debug("Custom identity manager " + identityManager.getClass().getName() + " has been set for realm: " + getName());
Expand Down
87 changes: 84 additions & 3 deletions src/core/java/org/wyona/yanel/core/util/ConfigurationUtil.java
Expand Up @@ -18,19 +18,100 @@
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.avalon.framework.configuration.Configuration;
import org.apache.log4j.Category;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.configuration.DefaultConfiguration;
import org.apache.avalon.framework.configuration.MutableConfiguration;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.util.Map;
import java.util.HashMap;
import java.util.Queue;
import java.util.LinkedList;

/**
* Configuration utility to copy an avalon configuration into a DOM document
* Configuration utility to deal with Avalon configuration elements.
*/
public class ConfigurationUtil {

/**
* The log category instance
*/
private static final Category log = Category.getInstance(ConfigurationUtil.class);
private static final Logger log = Logger.getLogger(ConfigurationUtil.class);

/**
* Filter elements by target environment
* @param repoConfigElement The config element.
* @param targetEnvironment The target environment.
*/
public static Configuration filterEnvironment(Configuration repoConfigElement, String targetEnvironment) throws ConfigurationException {
DefaultConfiguration rootElement = new DefaultConfiguration(repoConfigElement);

Queue<MutableConfiguration> roots = new LinkedList<MutableConfiguration>();
Queue<MutableConfiguration> children = new LinkedList<MutableConfiguration>();

// Push the first root element
roots.add(rootElement);

while(roots.size() > 0) {
// Examine every currently available root element.
MutableConfiguration current = roots.remove();

// Push children of the current element
for(MutableConfiguration mc : current.getMutableChildren()) {
children.add(mc);
}

// Map of previous elements
Map<String, Queue<MutableConfiguration>> prevMap =
new HashMap<String, Queue<MutableConfiguration>>();

while(children.size() > 0) {
// Examine every child of the current root element.
MutableConfiguration child = children.remove();

String name = child.getName();
Queue<MutableConfiguration> prev;
if(prevMap.containsKey(name)) {
prev = prevMap.get(name);
} else {
prev = new LinkedList<MutableConfiguration>();
prevMap.put(name, prev);
}

try {
String env = child.getAttribute("target-environment");
if(targetEnvironment.equals(env)) {
// Target env matches - remove previous objects
// with the same name (this overrides previous elements).
for(MutableConfiguration mc : prev) {
current.removeChild(mc);
}
prev.clear();
} else {
// Target env does not match - remove object.
current.removeChild(child);
}
} catch(ConfigurationException e) {
// We receive a configuration exception is there could
// be no target-env attribute found. In this case, we keep
// the element in all target environments.
}

// Push to prev element list
prev.add(child);
}

// All remaining children are now root elements
for(MutableConfiguration mc : current.getMutableChildren()) {
roots.add(mc);
}
}

return rootElement;
}

/**
* Create a DOM Document from a custom config element modelled with avalon
Expand Down
2 changes: 1 addition & 1 deletion yanel.sh
Expand Up @@ -40,7 +40,7 @@ fi

# ----- Set Environment Variables
unset ANT_HOME
ANT_HOME=$SCRIPT_DIR/tools/apache-ant
export ANT_HOME=$SCRIPT_DIR/tools/apache-ant
#echo $ANT_HOME
OUR_ANT="ant -lib $SCRIPT_DIR/tools/apache-ant_extras -f src/build/build.xml"

Expand Down

0 comments on commit 7afea64

Please sign in to comment.