📄 configurationstore.java
字号:
Document doc = null;
try {
doc = db.parse(new FileInputStream(configFile));
} catch (IOException ioe) {
throw new ParsingException("failed to load the file ", ioe);
} catch (SAXException saxe) {
throw new ParsingException("error parsing the XML tree", saxe);
} catch (IllegalArgumentException iae) {
throw new ParsingException("no data to parse", iae);
}
Element root = doc.getDocumentElement();
if (! root.getTagName().equals("config"))
throw new ParsingException("unknown document type: " +
root.getTagName());
return root;
}
/**
* Private helper that handles the pdp elements.
*/
private PDPConfig parsePDPConfig(Node root) throws ParsingException {
ArrayList attrModules = new ArrayList();
HashSet policyModules = new HashSet();
ArrayList rsrcModules = new ArrayList();
// go through all elements of the pdp, loading the specified modules
NodeList children = root.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
String name = child.getNodeName();
if (name.equals("policyFinderModule")) {
policyModules.add(loadClass("module", child));
} else if (name.equals("attributeFinderModule")) {
attrModules.add(loadClass("module", child));
} else if (name.equals("resourceFinderModule")) {
rsrcModules.add(loadClass("module", child));
}
}
// after loading the modules, use the collections to setup a
// PDPConfig based on this pdp element
AttributeFinder attrFinder = new AttributeFinder();
attrFinder.setModules(attrModules);
PolicyFinder policyFinder = new PolicyFinder();
policyFinder.setModules(policyModules);
ResourceFinder rsrcFinder = new ResourceFinder();
rsrcFinder.setModules(rsrcModules);
return new PDPConfig(attrFinder, policyFinder, rsrcFinder);
}
/**
* Private helper that handles the attributeFactory elements.
*/
private AttributeFactory parseAttributeFactory(Node root)
throws ParsingException
{
AttributeFactory factory = null;
// check if we're starting with the standard factory setup
if (useStandard(root, "useStandardDatatypes")) {
logger.config("Starting with standard Datatypes");
factory = StandardAttributeFactory.getNewFactory();
} else {
factory = new BaseAttributeFactory();
}
// now look for all datatypes specified for this factory, adding
// them as we go
NodeList children = root.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (child.getNodeName().equals("datatype")) {
// a datatype is a class with an identifier
String identifier = child.getAttributes().
getNamedItem("identifier").getNodeValue();
AttributeProxy proxy =
(AttributeProxy)(loadClass("datatype", child));
try {
factory.addDatatype(identifier, proxy);
} catch (IllegalArgumentException iae) {
throw new ParsingException("duplicate datatype: " +
identifier, iae);
}
}
}
return factory;
}
/**
* Private helper that handles the combiningAlgFactory elements.
*/
private CombiningAlgFactory parseCombiningAlgFactory(Node root)
throws ParsingException
{
CombiningAlgFactory factory = null;
// check if we're starting with the standard factory setup
if (useStandard(root, "useStandardAlgorithms")) {
logger.config("Starting with standard Combining Algorithms");
factory = StandardCombiningAlgFactory.getNewFactory();
} else {
factory = new BaseCombiningAlgFactory();
}
// now look for all algorithms specified for this factory, adding
// them as we go
NodeList children = root.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (child.getNodeName().equals("algorithm")) {
// an algorithm is a simple class element
CombiningAlgorithm alg =
(CombiningAlgorithm)(loadClass("algorithm", child));
try {
factory.addAlgorithm(alg);
} catch (IllegalArgumentException iae) {
throw new ParsingException("duplicate combining " +
"algorithm: " +
alg.getIdentifier().toString(),
iae);
}
}
}
return factory;
}
/**
* Private helper that handles the functionFactory elements. This one
* is a little more complex than the other two factory helper methods,
* since it consists of three factories (target, condition, and general).
*/
private FunctionFactoryProxy parseFunctionFactory(Node root)
throws ParsingException
{
FunctionFactoryProxy proxy = null;
FunctionFactory generalFactory = null;
FunctionFactory conditionFactory = null;
FunctionFactory targetFactory = null;
// check if we're starting with the standard factory setup, and
// make sure that the proxy is pre-configured
if (useStandard(root, "useStandardFunctions")) {
logger.config("Starting with standard Functions");
proxy = StandardFunctionFactory.getNewFactoryProxy();
targetFactory = proxy.getTargetFactory();
conditionFactory = proxy.getConditionFactory();
generalFactory = proxy.getGeneralFactory();
} else {
generalFactory = new BaseFunctionFactory();
conditionFactory = new BaseFunctionFactory(generalFactory);
targetFactory = new BaseFunctionFactory(conditionFactory);
proxy = new BasicFunctionFactoryProxy(targetFactory,
conditionFactory,
generalFactory);
}
// go through and load the three sections, putting the loaded
// functions into the appropriate factory
NodeList children = root.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
String name = child.getNodeName();
if (name.equals("target")) {
logger.config("Loading [TARGET] functions");
functionParserHelper(child, targetFactory);
} else if (name.equals("condition")) {
logger.config("Loading [CONDITION] functions");
functionParserHelper(child, conditionFactory);
} else if (name.equals("general")) {
logger.config("Loading [GENERAL] functions");
functionParserHelper(child, generalFactory);
}
}
return proxy;
}
/**
* Private helper used by the function factory code to load a specific
* target, condition, or general section.
*/
private void functionParserHelper(Node root, FunctionFactory factory)
throws ParsingException
{
// go through all elements in the section
NodeList children = root.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
String name = child.getNodeName();
if (name.equals("function")) {
// a function section is a simple class element
Function function =
(Function)(loadClass("function", child));
try {
factory.addFunction(function);
} catch (IllegalArgumentException iae) {
throw new ParsingException("duplicate function", iae);
}
} else if (name.equals("abstractFunction")) {
// an abstract function is a class with an identifier
URI identifier = null;
try {
identifier = new URI(child.getAttributes().
getNamedItem("identifier").
getNodeValue());
} catch (URISyntaxException urise) {
throw new ParsingException("invalid function identifier",
urise);
}
FunctionProxy proxy =
(FunctionProxy)(loadClass("abstract function", child));
try {
factory.addAbstractFunction(proxy, identifier);
} catch (IllegalArgumentException iae) {
throw new ParsingException("duplicate abstract function",
iae);
}
} else if (name.equals("functionCluster")) {
// a cluster is a class that will give us a collection of
// functions that need to be added one by one into the factory
FunctionCluster cluster =
(FunctionCluster)(loadClass("function cluster", child));
Iterator it = cluster.getSupportedFunctions().iterator();
while (it.hasNext()) {
try {
factory.addFunction((Function)(it.next()));
} catch (IllegalArgumentException iae) {
throw new ParsingException("duplicate function", iae);
}
}
}
}
}
/**
* Private helper that is used by all the code to load an instance of
* the given class...this assumes that the class is in the classpath,
* both for simplicity and for stronger security
*/
private Object loadClass(String prefix, Node root)
throws ParsingException
{
// get the name of the class
String className =
root.getAttributes().getNamedItem("class").getNodeValue();
if (logger.isLoggable(Level.CONFIG))
logger.config("Loading [ " + prefix + ": " + className + " ]");
// load the given class using the local classloader
Class c = null;
try {
c = loader.loadClass(className);
} catch (ClassNotFoundException cnfe) {
throw new ParsingException("couldn't load class " + className,
cnfe);
}
Object instance = null;
// figure out if there are any parameters to the constructor
if (! root.hasChildNodes()) {
// we're using a null constructor, so this is easy
try {
instance = c.newInstance();
} catch (InstantiationException ie) {
throw new ParsingException("couldn't instantiate " + className
+ " with empty constructor", ie);
} catch (IllegalAccessException iae) {
throw new ParsingException("couldn't get access to instance " +
"of " + className, iae);
}
} else {
// parse the arguments to the constructor
List args = null;
try {
args = getArgs(root);
} catch (IllegalArgumentException iae) {
throw new ParsingException("illegal class arguments", iae);
}
int argLength = args.size();
// next we need to see if there's a constructor that matches the
// arguments provided...this has to be done by hand since
// Class.getConstructor(Class []) doesn't handle sub-classes and
// generic types (for instance, a constructor taking List won't
// match a parameter list containing ArrayList)
// get the list of all available constructors
Constructor [] cons = c.getConstructors();
Constructor constructor = null;
for (int i = 0; i < cons.length; i++) {
// get the parameters for this constructor
Class [] params = cons[i].getParameterTypes();
if (params.length == argLength) {
Iterator it = args.iterator();
int j = 0;
// loop through the parameters and see if each one is
// assignable from the coresponding input argument
while (it.hasNext()) {
if (! params[j].isAssignableFrom(it.next().getClass()))
break;
j++;
}
// if we looked at all the parameters, then this
// constructor matches the input
if (j == argLength)
constructor = cons[i];
}
// if we've found a matching constructor then stop looping
if (constructor != null)
break;
}
// make sure we found a matching constructor
if (constructor == null)
throw new ParsingException("couldn't find a matching " +
"constructor");
// finally, instantiate the class
try {
instance = constructor.newInstance(args.toArray());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -