001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: XmlSelectorResolver.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.selector;
009:
010: import com.uwyn.rife.resources.ResourceFinder;
011: import java.net.URL;
012:
013: /**
014: * Looks up XML configuration file locations based on participant parameters.
015: *
016: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
017: * @author Steven Grimm (koreth[remove] at midwinter dot com)
018: * @version $Revision: 3634 $
019: * @since 1.0
020: */
021: public abstract class XmlSelectorResolver {
022: /**
023: * Returns an XML file location.
024: *
025: * @param parameter
026: * Parameter of the participant for this configuration file. This is a
027: * comma-separated list of path names and XML selector class names;
028: * they are tried in order until one of them resolves to a resource
029: * that exists.
030: * @param resourceFinder
031: * Object that looks up resources on the classpath.
032: * @param prefix
033: * Configuration file prefix; or
034: * <p><code>null</code> if none is needed
035: * @since 1.0
036: */
037: public static String resolve(String parameter,
038: ResourceFinder resourceFinder, String prefix) {
039: if (null == parameter)
040: throw new IllegalArgumentException("xmlPath can't be null.");
041: if (0 == parameter.length())
042: throw new IllegalArgumentException(
043: "xmlPath can't be empty.");
044: if (null == resourceFinder)
045: throw new IllegalArgumentException(
046: "resourceFinder can't be null.");
047:
048: String result = null;
049:
050: URL resource = null;
051: for (String resourceName : parameter.split(",")) {
052: resource = resourceFinder.getResource(resourceName);
053:
054: // check if the xml file could be found
055: if (resource != null) {
056: result = resourceName;
057: break;
058: } else {
059: // it could not be found, try to see if it was a class name of a XmlSelector
060: // that is present in the classpath
061: Class klass = null;
062: try {
063: // try a complete classname
064: klass = Class.forName(resourceName);
065: } catch (ClassNotFoundException e) {
066: klass = null;
067: }
068: // try this package's prefix if the class couldn't be found
069: if (null == klass) {
070: try {
071: klass = Class.forName(XmlSelectorResolver.class
072: .getPackage().getName()
073: + "." + resourceName);
074: } catch (ClassNotFoundException e) {
075: klass = null;
076: }
077: }
078: // if the class was found, create an instance
079: if (klass != null) {
080: try {
081: Object instance = klass.newInstance();
082: if (instance instanceof XmlSelector) {
083: String path = ((XmlSelector) instance)
084: .getXmlPath(prefix);
085: resource = resourceFinder.getResource(path);
086: if (resource != null) {
087: result = path;
088: break;
089: }
090: }
091: } catch (InstantiationException e) {
092: continue;
093: } catch (IllegalAccessException e) {
094: continue;
095: }
096: }
097: }
098: }
099:
100: return result;
101: }
102: }
|