polishing (at the occasion of a backport)

This commit is contained in:
Juergen Hoeller 2012-08-28 19:42:17 +02:00 committed by unknown
parent 30de87a51e
commit 049b944434
1 changed files with 33 additions and 34 deletions

View File

@ -106,6 +106,7 @@ import org.springframework.util.xml.StaxUtils;
* listeners, and to refer to it. * listeners, and to refer to it.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 3.0
* @see #setContextPath(String) * @see #setContextPath(String)
* @see #setClassesToBeBound(Class[]) * @see #setClassesToBeBound(Class[])
* @see #setJaxbContextProperties(Map) * @see #setJaxbContextProperties(Map)
@ -116,7 +117,6 @@ import org.springframework.util.xml.StaxUtils;
* @see #setMarshallerListener(javax.xml.bind.Marshaller.Listener) * @see #setMarshallerListener(javax.xml.bind.Marshaller.Listener)
* @see #setUnmarshallerListener(javax.xml.bind.Unmarshaller.Listener) * @see #setUnmarshallerListener(javax.xml.bind.Unmarshaller.Listener)
* @see #setAdapters(XmlAdapter[]) * @see #setAdapters(XmlAdapter[])
* @since 3.0
*/ */
public class Jaxb2Marshaller public class Jaxb2Marshaller
implements MimeMarshaller, MimeUnmarshaller, GenericMarshaller, GenericUnmarshaller, BeanClassLoaderAware, implements MimeMarshaller, MimeUnmarshaller, GenericMarshaller, GenericUnmarshaller, BeanClassLoaderAware,
@ -225,10 +225,10 @@ public class Jaxb2Marshaller
} }
/** /**
* Returns the packages to search for JAXB2 annotations. * Return the packages to search for JAXB2 annotations.
*/ */
public String[] getPackagesToScan() { public String[] getPackagesToScan() {
return packagesToScan; return this.packagesToScan;
} }
/** /**
@ -317,8 +317,7 @@ public class Jaxb2Marshaller
} }
/** /**
* Sets the resource resolver, as used to load the schema resources. * Set the resource resolver, as used to load the schema resources.
*
* @see SchemaFactory#setResourceResolver(org.w3c.dom.ls.LSResourceResolver) * @see SchemaFactory#setResourceResolver(org.w3c.dom.ls.LSResourceResolver)
* @see #setSchema(Resource) * @see #setSchema(Resource)
* @see #setSchemas(Resource[]) * @see #setSchemas(Resource[])
@ -352,7 +351,6 @@ public class Jaxb2Marshaller
* <p>This property is typically enabled in combination with usage of classes like * <p>This property is typically enabled in combination with usage of classes like
* {@link org.springframework.web.servlet.view.xml.MarshallingView MarshallingView}, since the {@code ModelAndView} * {@link org.springframework.web.servlet.view.xml.MarshallingView MarshallingView}, since the {@code ModelAndView}
* does not offer type parameter information at runtime. * does not offer type parameter information at runtime.
*
* @see #supports(Class) * @see #supports(Class)
* @see #supports(Type) * @see #supports(Type)
*/ */
@ -369,9 +367,9 @@ public class Jaxb2Marshaller
} }
public final void afterPropertiesSet() throws Exception { public final void afterPropertiesSet() throws Exception {
boolean hasContextPath = StringUtils.hasLength(getContextPath()); boolean hasContextPath = StringUtils.hasLength(this.contextPath);
boolean hasClassesToBeBound = !ObjectUtils.isEmpty(getClassesToBeBound()); boolean hasClassesToBeBound = !ObjectUtils.isEmpty(this.classesToBeBound);
boolean hasPackagesToScan = !ObjectUtils.isEmpty(getPackagesToScan()); boolean hasPackagesToScan = !ObjectUtils.isEmpty(this.packagesToScan);
if (hasContextPath && (hasClassesToBeBound || hasPackagesToScan) || if (hasContextPath && (hasClassesToBeBound || hasPackagesToScan) ||
(hasClassesToBeBound && hasPackagesToScan)) { (hasClassesToBeBound && hasPackagesToScan)) {
@ -393,13 +391,13 @@ public class Jaxb2Marshaller
protected synchronized JAXBContext getJaxbContext() { protected synchronized JAXBContext getJaxbContext() {
if (this.jaxbContext == null) { if (this.jaxbContext == null) {
try { try {
if (StringUtils.hasLength(getContextPath())) { if (StringUtils.hasLength(this.contextPath)) {
this.jaxbContext = createJaxbContextFromContextPath(); this.jaxbContext = createJaxbContextFromContextPath();
} }
else if (!ObjectUtils.isEmpty(getClassesToBeBound())) { else if (!ObjectUtils.isEmpty(this.classesToBeBound)) {
this.jaxbContext = createJaxbContextFromClasses(); this.jaxbContext = createJaxbContextFromClasses();
} }
else if (!ObjectUtils.isEmpty(getPackagesToScan())) { else if (!ObjectUtils.isEmpty(this.packagesToScan)) {
this.jaxbContext = createJaxbContextFromPackages(); this.jaxbContext = createJaxbContextFromPackages();
} }
} }
@ -407,27 +405,27 @@ public class Jaxb2Marshaller
throw convertJaxbException(ex); throw convertJaxbException(ex);
} }
} }
return jaxbContext; return this.jaxbContext;
} }
private JAXBContext createJaxbContextFromContextPath() throws JAXBException { private JAXBContext createJaxbContextFromContextPath() throws JAXBException {
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
logger.info("Creating JAXBContext with context path [" + getContextPath() + "]"); logger.info("Creating JAXBContext with context path [" + this.contextPath + "]");
} }
if (this.jaxbContextProperties != null) { if (this.jaxbContextProperties != null) {
if (this.beanClassLoader != null) { if (this.beanClassLoader != null) {
return JAXBContext.newInstance(getContextPath(), this.beanClassLoader, this.jaxbContextProperties); return JAXBContext.newInstance(this.contextPath, this.beanClassLoader, this.jaxbContextProperties);
} }
else { else {
return JAXBContext.newInstance(getContextPath(), ClassUtils.getDefaultClassLoader(), this.jaxbContextProperties); return JAXBContext.newInstance(this.contextPath, ClassUtils.getDefaultClassLoader(), this.jaxbContextProperties);
} }
} }
else { else {
if (this.beanClassLoader != null) { if (this.beanClassLoader != null) {
return JAXBContext.newInstance(getContextPath(), this.beanClassLoader); return JAXBContext.newInstance(this.contextPath, this.beanClassLoader);
} }
else { else {
return JAXBContext.newInstance(getContextPath()); return JAXBContext.newInstance(this.contextPath);
} }
} }
} }
@ -435,22 +433,22 @@ public class Jaxb2Marshaller
private JAXBContext createJaxbContextFromClasses() throws JAXBException { private JAXBContext createJaxbContextFromClasses() throws JAXBException {
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
logger.info("Creating JAXBContext with classes to be bound [" + logger.info("Creating JAXBContext with classes to be bound [" +
StringUtils.arrayToCommaDelimitedString(getClassesToBeBound()) + "]"); StringUtils.arrayToCommaDelimitedString(this.classesToBeBound) + "]");
} }
if (this.jaxbContextProperties != null) { if (this.jaxbContextProperties != null) {
return JAXBContext.newInstance(getClassesToBeBound(), this.jaxbContextProperties); return JAXBContext.newInstance(this.classesToBeBound, this.jaxbContextProperties);
} }
else { else {
return JAXBContext.newInstance(getClassesToBeBound()); return JAXBContext.newInstance(this.classesToBeBound);
} }
} }
private JAXBContext createJaxbContextFromPackages() throws JAXBException { private JAXBContext createJaxbContextFromPackages() throws JAXBException {
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
logger.info("Creating JAXBContext by scanning packages [" + logger.info("Creating JAXBContext by scanning packages [" +
StringUtils.arrayToCommaDelimitedString(getPackagesToScan()) + "]"); StringUtils.arrayToCommaDelimitedString(this.packagesToScan) + "]");
} }
ClassPathJaxb2TypeScanner scanner = new ClassPathJaxb2TypeScanner(getPackagesToScan()); ClassPathJaxb2TypeScanner scanner = new ClassPathJaxb2TypeScanner(this.packagesToScan);
scanner.setResourceLoader(this.resourceLoader); scanner.setResourceLoader(this.resourceLoader);
scanner.scanPackages(); scanner.scanPackages();
Class<?>[] jaxb2Classes = scanner.getJaxb2Classes(); Class<?>[] jaxb2Classes = scanner.getJaxb2Classes();
@ -468,7 +466,8 @@ public class Jaxb2Marshaller
private Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException { private Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Setting validation schema to " + StringUtils.arrayToCommaDelimitedString(this.schemaResources)); logger.debug("Setting validation schema to " +
StringUtils.arrayToCommaDelimitedString(this.schemaResources));
} }
Assert.notEmpty(resources, "No resources given"); Assert.notEmpty(resources, "No resources given");
Assert.hasLength(schemaLanguage, "No schema language provided"); Assert.hasLength(schemaLanguage, "No schema language provided");
@ -482,15 +481,15 @@ public class Jaxb2Marshaller
schemaSources[i] = new SAXSource(xmlReader, inputSource); schemaSources[i] = new SAXSource(xmlReader, inputSource);
} }
SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage); SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
if (schemaResourceResolver != null) { if (this.schemaResourceResolver != null) {
schemaFactory.setResourceResolver(schemaResourceResolver); schemaFactory.setResourceResolver(this.schemaResourceResolver);
} }
return schemaFactory.newSchema(schemaSources); return schemaFactory.newSchema(schemaSources);
} }
public boolean supports(Class<?> clazz) { public boolean supports(Class<?> clazz) {
if (supportJaxbElementClass && JAXBElement.class.isAssignableFrom(clazz)) { if (this.supportJaxbElementClass && JAXBElement.class.isAssignableFrom(clazz)) {
return true; return true;
} }
return supportsInternal(clazz, true); return supportsInternal(clazz, true);
@ -531,9 +530,9 @@ public class Jaxb2Marshaller
if (checkForXmlRootElement && AnnotationUtils.findAnnotation(clazz, XmlRootElement.class) == null) { if (checkForXmlRootElement && AnnotationUtils.findAnnotation(clazz, XmlRootElement.class) == null) {
return false; return false;
} }
if (StringUtils.hasLength(getContextPath())) { if (StringUtils.hasLength(this.contextPath)) {
String packageName = ClassUtils.getPackageName(clazz); String packageName = ClassUtils.getPackageName(clazz);
String[] contextPaths = StringUtils.tokenizeToStringArray(getContextPath(), ":"); String[] contextPaths = StringUtils.tokenizeToStringArray(this.contextPath, ":");
for (String contextPath : contextPaths) { for (String contextPath : contextPaths) {
if (contextPath.equals(packageName)) { if (contextPath.equals(packageName)) {
return true; return true;
@ -541,8 +540,8 @@ public class Jaxb2Marshaller
} }
return false; return false;
} }
else if (!ObjectUtils.isEmpty(getClassesToBeBound())) { else if (!ObjectUtils.isEmpty(this.classesToBeBound)) {
return Arrays.asList(getClassesToBeBound()).contains(clazz); return Arrays.asList(this.classesToBeBound).contains(clazz);
} }
return false; return false;
} }
@ -709,7 +708,8 @@ public class Jaxb2Marshaller
} }
/** /**
* Return a newly created JAXB unmarshaller. JAXB unmarshallers are not necessarily thread safe. * Return a newly created JAXB unmarshaller.
* Note: JAXB unmarshallers are not necessarily thread-safe.
*/ */
protected Unmarshaller createUnmarshaller() { protected Unmarshaller createUnmarshaller() {
try { try {
@ -752,7 +752,6 @@ public class Jaxb2Marshaller
} }
} }
/** /**
* Convert the given <code>JAXBException</code> to an appropriate exception from the * Convert the given <code>JAXBException</code> to an appropriate exception from the
* <code>org.springframework.oxm</code> hierarchy. * <code>org.springframework.oxm</code> hierarchy.
@ -819,7 +818,7 @@ public class Jaxb2Marshaller
@Override @Override
public String addSwaRefAttachment(DataHandler dataHandler) { public String addSwaRefAttachment(DataHandler dataHandler) {
String contentId = UUID.randomUUID() + "@" + dataHandler.getName(); String contentId = UUID.randomUUID() + "@" + dataHandler.getName();
mimeContainer.addAttachment(contentId, dataHandler); this.mimeContainer.addAttachment(contentId, dataHandler);
return contentId; return contentId;
} }