1 /* 2 * $Id: JAXBContextResolver.java 20321 2010-11-24 15:21:24Z dfeist $ 3 * -------------------------------------------------------------------------------------- 4 * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com 5 * 6 * The software in this package is published under the terms of the CPAL v1.0 7 * license, a copy of which has been included with this distribution in the 8 * LICENSE.txt file. 9 */ 10 package org.mule.module.xml.transformer.jaxb; 11 12 import org.mule.api.MuleContext; 13 import org.mule.config.transformer.AbstractAnnotatedTransformerArgumentResolver; 14 15 import javax.xml.bind.JAXBContext; 16 import javax.xml.bind.JAXBException; 17 18 /** 19 * This resolver is used by the transform engine to inject a JAXBContext into a method that requires it. 20 * A shared JAXB context can be created for the application and stored in the registry, this will get injected 21 * into any transform methods that add {@link javax.xml.bind.JAXBContext} to the method signature. 22 * <p/> 23 * IF there is no shared JAXB context one will be created. First this resolver will attempt to create the context from 24 * the package of the the annotated class, for this to work either a jaxb.index file must be present or an {@link javax.naming.spi.ObjectFactory} 25 * must be in the package. This allows for JAXB generated classes to be used easily. If this method fails a context will 26 * be created using just the annotated class to initialise the context. 27 * 28 * @since 3.0 29 */ 30 public class JAXBContextResolver extends AbstractAnnotatedTransformerArgumentResolver 31 { 32 protected Class getArgumentClass() 33 { 34 return JAXBContext.class; 35 } 36 37 protected Object createArgument(Class annotatedType, MuleContext muleContext) throws Exception 38 { 39 try 40 { 41 return JAXBContext.newInstance(annotatedType); 42 } 43 catch (JAXBException e) 44 { 45 //Fallback to just adding the annotated class to the context 46 logger.warn(e.getMessage() + ". Initializing context using JAXB annotated class: " + annotatedType); 47 return JAXBContext.newInstance(annotatedType); 48 } 49 } 50 51 protected String getAnnotationsPackageName() 52 { 53 return "javax.xml.bind.annotation"; 54 } 55 56 }