forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SPR-8986 Add the ability to Scan Packages for JAXB Marshalling
Jaxb2Marshaller now has the capability to scan for classes annotated with JAXB2 annotations.
- Loading branch information
Showing
4 changed files
with
195 additions
and
8 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
...ngframework.oxm/src/main/java/org/springframework/oxm/jaxb/ClassPathJaxb2TypeScanner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* Copyright 2002-2012 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.oxm.jaxb; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import javax.xml.bind.annotation.XmlEnum; | ||
import javax.xml.bind.annotation.XmlRootElement; | ||
import javax.xml.bind.annotation.XmlSeeAlso; | ||
import javax.xml.bind.annotation.XmlType; | ||
|
||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.ResourceLoader; | ||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; | ||
import org.springframework.core.io.support.ResourcePatternResolver; | ||
import org.springframework.core.io.support.ResourcePatternUtils; | ||
import org.springframework.core.type.classreading.CachingMetadataReaderFactory; | ||
import org.springframework.core.type.classreading.MetadataReader; | ||
import org.springframework.core.type.classreading.MetadataReaderFactory; | ||
import org.springframework.core.type.filter.AnnotationTypeFilter; | ||
import org.springframework.core.type.filter.TypeFilter; | ||
import org.springframework.oxm.UncategorizedMappingException; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.ClassUtils; | ||
|
||
/** | ||
* Helper class for {@link Jaxb2Marshaller} that scans given packages for classes marked with JAXB2 annotations. | ||
* | ||
* @author Arjen Poutsma | ||
* @author David Harrigan | ||
* @see #scanPackages() | ||
*/ | ||
class ClassPathJaxb2TypeScanner { | ||
|
||
private static final String RESOURCE_PATTERN = "/**/*.class"; | ||
|
||
private final TypeFilter[] jaxb2TypeFilters = | ||
new TypeFilter[]{new AnnotationTypeFilter(XmlRootElement.class, false), | ||
new AnnotationTypeFilter(XmlType.class, false), new AnnotationTypeFilter(XmlSeeAlso.class, false), | ||
new AnnotationTypeFilter(XmlEnum.class, false)}; | ||
|
||
private final String[] packagesToScan; | ||
|
||
private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); | ||
|
||
private List<Class<?>> jaxb2Classes = new ArrayList<Class<?>>(); | ||
|
||
/** Constructs a new {@code ClassPathJaxb2TypeScanner} for the given packages. */ | ||
ClassPathJaxb2TypeScanner(String[] packagesToScan) { | ||
Assert.notEmpty(packagesToScan, "'packagesToScan' must not be empty"); | ||
this.packagesToScan = packagesToScan; | ||
} | ||
|
||
void setResourceLoader(ResourceLoader resourceLoader) { | ||
if (resourceLoader != null) { | ||
this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader); | ||
} | ||
} | ||
|
||
/** Returns the JAXB2 classes found in the specified packages. */ | ||
Class<?>[] getJaxb2Classes() { | ||
return jaxb2Classes.toArray(new Class<?>[jaxb2Classes.size()]); | ||
} | ||
|
||
/** | ||
* Scans the packages for classes marked with JAXB2 annotations. | ||
* | ||
* @throws UncategorizedMappingException in case of errors | ||
*/ | ||
void scanPackages() throws UncategorizedMappingException { | ||
try { | ||
for (String packageToScan : packagesToScan) { | ||
String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + | ||
ClassUtils.convertClassNameToResourcePath(packageToScan) + RESOURCE_PATTERN; | ||
Resource[] resources = resourcePatternResolver.getResources(pattern); | ||
MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver); | ||
for (Resource resource : resources) { | ||
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource); | ||
if (isJaxb2Class(metadataReader, metadataReaderFactory)) { | ||
String className = metadataReader.getClassMetadata().getClassName(); | ||
Class<?> jaxb2AnnotatedClass = resourcePatternResolver.getClassLoader().loadClass(className); | ||
jaxb2Classes.add(jaxb2AnnotatedClass); | ||
} | ||
} | ||
} | ||
} | ||
catch (IOException ex) { | ||
throw new UncategorizedMappingException("Failed to scan classpath for unlisted classes", ex); | ||
} | ||
catch (ClassNotFoundException ex) { | ||
throw new UncategorizedMappingException("Failed to load annotated classes from classpath", ex); | ||
} | ||
} | ||
|
||
private boolean isJaxb2Class(MetadataReader reader, MetadataReaderFactory factory) throws IOException { | ||
for (TypeFilter filter : jaxb2TypeFilters) { | ||
if (filter.match(reader, factory)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters