JAXB Deployer
anil.saldhana Mar 18, 2009 11:14 AMI have an independent JAXB model for configuration from a separate project called as JBossXACML (which does not have any dependencies on JBossXB).
As part of https://jira.jboss.org/jira/browse/JBAS-6607, I tried using the SchemaResolvingDeployer but it was unable to locate the schema. Additionally, registering with JBossXB is a noop because my metadata does not have the @JBossxml annotation.
Given this, I looked at the JAXBDeployer http://anonsvn.jboss.org/repos/jbossas/projects/jboss-deployers/tags/2.0.5.GA/deployers-vfs-spi/src/main/java/org/jboss/deployers/vfs/spi/deployer/JAXBDeployer.java in deployers which is an abstract class. The issue with this is that if the JAXB metadata has a root of JAXBElement from the following schema:
http://anonsvn.jboss.org/repos/jbossas/projects/security/security-xacml/trunk/jboss-xacml/src/main/resources/schema/jbossxacml-2.0.xsd
then the JAXBDeployer create method falls apart because it tries to instantiate JAXBElement.
Issue with JAXBDeployer:
"context" and "properties" need to made protected.
My JAXBElementParsingDeployer looks as follows:
package org.jboss.security.deployers;
import java.io.InputStream;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import org.jboss.deployers.vfs.spi.deployer.JAXBDeployer;
import org.jboss.deployers.vfs.spi.structure.VFSDeploymentUnit;
import org.jboss.virtual.VirtualFile;
import org.xml.sax.InputSource;
/**
* A parsing deployer that is capable of parsing
* a JAXB model with the root element being
* JAXBElement<T>
*/
@SuppressWarnings("unchecked")
public class JAXBElementParsingDeployer<T,V> extends JAXBDeployer
{
/** The JAXBContext */
protected JAXBContext context;
/** The properties */
protected Map<String, Object> properties;
protected Class<V> enclosed;
/**
* CTR
* @param output JAXBElement.class
* @param enclosed Type enclosed by JAXBElement
*/
public JAXBElementParsingDeployer(Class<T> output, Class<V> enclosed)
{
super(output);
this.enclosed = enclosed;
}
/**
* Create lifecycle
*
* @throws Exception for any problem
*/
@Override
public void create() throws Exception
{
if (properties != null)
context = JAXBContext.newInstance(new Class[] {enclosed}, properties);
else
context = JAXBContext.newInstance(enclosed);
}
/**
* Destroy lifecycle
*/
public void destroy()
{
context = null;
}
@Override
protected Object parse(VFSDeploymentUnit unit, VirtualFile file, Object root) throws Exception
{
Unmarshaller unmarshaller = context.createUnmarshaller();
InputStream is = openStreamAndValidate(file);
try
{
InputSource source = new InputSource(is);
source.setSystemId(file.toURI().toString());
Object o = unmarshaller.unmarshal(source);
return getOutput().cast(o);
}
finally
{
try
{
is.close();
}
catch (Exception ignored)
{
}
}
}
}
So if "context" and "properties" are made protected members of the JAXBDeployer then I can just override the create method to avoid the duplicated parse.