Configuring a JAXB based configuration file.

Before using registry-j2se JAXB, you will first need to generate the JAXB classes based upon a schema. The JAXBConfiguration is not dependent on the underlying JAXB implementation, so any will do. (I am using applications configured with both JAXME and Sun's JAXB).

Plugging a JAXB repository into registry-j2se is simple. Consider the following registry-config.xml file. The repository-config tag references the XML file to unmarshall. You will also need to add a binding-package parameter that tells the repository which JAXB binding package to use for the unmarshalling.

<registry-config>
   <repositories>
      <repository>
         <repository-name>matchers-config</repository-name>
         <repository-class>org.jvending.provisioning.config.AttributeMatcherRepository</repository-class>
         <repository-config>${basedir}/src/test/resources/matchers.xml</repository-config>
	    <init-param>
		    <param-name>binding-package</param-name>
		    <param-value>org.jvending.provisioning.config.matchers</param-value>
	    </init-param>
      </repository>
    </repositories>
</registry-config>
            

Then you will need to implement a Repository. Implementing the Repository that uses the JAXBConfiguration is also simple.Consider the following class that pulls the name of the binding package from the Hashtable instance and passes it to JaxbConfiguration.parse.

public final class AttributeMatcherRepository implements Repository {

    private static Map matcherMap = new HashMap();

    public AttributeMatcher getMatcherFor(String attributeName) {
        return (AttributeMatcher) matcherMap.get(attributeName);
    }

    public void load(InputStream inputStream, Hashtable properties) throws IOException {
        Matchers matchers =
            (Matchers) JaxbConfiguration.parse(inputStream, (String) properties.get("binding-package"));

        for(Iterator i = matchers.getMatcher().iterator(); i.hasNext(); ) {
            MatcherType matcher = (MatcherType) i.next();
            String attributeName = matcher.getAttributeName();
            List initParam = matcher.getInitParam();
            AttributeMatcher attributeMatcher = null;
            try {
                String matcherClassName = matcher.getMatcherClass();
                Class matcherClass = Class.forName(matcherClassName);
                Constructor matcherConstructor = matcherClass.getConstructor(null);
                attributeMatcher = (AttributeMatcher) matcherConstructor.newInstance(null);
                attributeMatcher.init(attributeName, toMap(initParam));
            } catch (Exception e) {
                throw new IOException("Problem loading the attribute matcher repository");
            }
            matcherMap.put(attributeName, attributeMatcher);
        }
    }

    private Map toMap(List initParam) {
        Map map = new HashMap();
        for(Iterator i = initParam.iterator(); i.hasNext(); ) {
            InitParamType initParamType = (InitParamType) i.next();
            String name = initParamType.getParamName();
            String value = initParamType.getParamValue();
            map.put(name.trim(), value.trim());
        }
        return map;
    }

    public void setRepositoryRegistry(RepositoryRegistry repositoryRegistry) {
        //don't need this
    }

}