in modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/JAXBUtils.java [450:648]
private static JAXBContextValue createJAXBContextValue(TreeSet<String> contextPackages,
ClassLoader cl,
boolean forceArrays,
Map<String, ?> properties,
List<String> classRefs) throws JAXBException {
JAXBContextValue contextValue = null;
if (log.isDebugEnabled()) {
log.debug("Following packages are in this batch of getJAXBContext() :");
for (String pkg : contextPackages) {
log.debug(pkg);
}
log.debug("This classloader will be used to construct the JAXBContext" + cl);
}
// The contextPackages is a set of package names that are constructed using PackageSetBuilder.
// PackageSetBuilder gets the packages names from various sources.
// a) It walks the various annotations on the WebService collecting package names.
// b) It walks the wsdl/schemas and builds package names for each target namespace.
//
// The combination of these two sources should produce all of the package names.
// -------------
// Note that (b) is necessary for the following case:
// An operation has a parameter named BASE.
// Object DERIVED is an extension of BASE and is defined in a different package/schema.
// In this case, there will not be any annotations on the WebService that reference DERIVED.
// The only way to find the package for DERIVED is to walk the schemas.
// -------------
Iterator<String> it = contextPackages.iterator();
while (it.hasNext()) {
String p = it.next();
// Don't consider java and javax packages
// REVIEW: We might have to refine this
if (p.startsWith("javax.xml.ws.wsaddressing")) {
continue;
}
if (p.startsWith("java.") ||
p.startsWith("javax.")) {
it.remove();
}
}
// There are two ways to construct the context.
// 1) USE A CONTEXTPATH, which is a string containing
// all of the packages separated by colons.
// 2) USE A CLASS[], which is an array of all of the classes
// involved in the marshal/unmarshal.
//
// There are pros/cons with both approaches.
// USE A CONTEXTPATH:
// Pros: preferred way of doing this.
// performant
// most dynamic
// Cons: Each package in context path must have an ObjectFactory
//
//
// USE CLASS[]:
// Pros: Doesn't require ObjectFactory in each package
// Cons: Hard to set up, must account for JAX-WS classes, etc.
// Does not work if arrays of classes are needed
// slower
//
// The following code attempts to build a context path. It then
// choose one of the two constructions above (prefer USE A CONTEXT_PATH)
//
// The packages are examined to see if they have ObjectFactory/package-info classes.
// Invalid packages are removed from the list
it = contextPackages.iterator();
boolean contextConstruction = (!forceArrays);
boolean isJAXBFound = false;
while (it.hasNext()) {
String p = it.next();
// See if this package has an ObjectFactory or package-info
if (checkPackage(p, cl)) {
// Flow to here indicates package can be used for CONTEXT construction
isJAXBFound = true;
if (log.isDebugEnabled()) {
log.debug("Package " + p + " contains an ObjectFactory or package-info class.");
}
} else {
// Flow to here indicates that the package is not valid for context construction.
// Perhaps the package is invalid.
if (log.isDebugEnabled()) {
log.debug("Package " + p +
" does not contain an ObjectFactory or package-info class. Searching for JAXB classes");
}
List<Class> classes = null;
classes = getAllClassesFromPackage(p, cl);
if (classes == null || classes.size() == 0) {
if (log.isDebugEnabled()) {
log.debug("Package " + p +
" does not have any JAXB classes. It is removed from the JAXB context path.");
}
it.remove();
} else {
// Classes are found in the package. We cannot use the CONTEXT construction
contextConstruction = false;
if (log.isDebugEnabled()) {
log.debug("Package " + p +
" does not contain ObjectFactory, but it does contain other JAXB classes.");
}
}
}
}
if (!isJAXBFound) {
if (log.isDebugEnabled()) {
log.debug("ObjectFactory & package-info are not found in package hierachy");
}
}
// The code above may have removed some packages from the list.
// Retry our lookup with the updated list
if (contextConstruction) {
if (log.isDebugEnabled()) {
log.debug("Recheck Cache Start: Some packages have been removed from the list. Rechecking cache.");
}
String key = contextPackages.toString();
ConcurrentHashMap<ClassLoader, JAXBContextValue> innerMap = null;
SoftReference<ConcurrentHashMap<ClassLoader, JAXBContextValue>> softRef = jaxbMap.get(key);
if (softRef != null) {
innerMap = softRef.get();
}
if (innerMap != null) {
contextValue = innerMap.get(cl);
if (forceArrays &&
contextValue != null &&
contextValue.constructionType != JAXBUtils.CONSTRUCTION_TYPE.BY_CLASS_ARRAY_PLUS_ARRAYS) {
if(log.isDebugEnabled()) {
log.debug("Found a JAXBContextValue with constructionType=" +
contextValue.constructionType + " but the caller requested a JAXBContext " +
" that includes arrays. A new JAXBContext will be built");
}
contextValue = null;
}
if (contextValue != null) {
if (log.isDebugEnabled()) {
log.debug("Successfully found JAXBContext with updated context list:" +
contextValue.jaxbContext.toString());
}
return contextValue;
}
}
if (log.isDebugEnabled()) {
log.debug("Recheck Cache End: Did not find a JAXBContext. Will build a new JAXBContext.");
}
}
// CONTEXT construction
if (contextConstruction) {
if (log.isDebugEnabled()) {
log.debug("Try building a JAXBContext using the packages only.");
}
JAXBContext context = createJAXBContextUsingContextPath(contextPackages, cl, classRefs);
if (context != null) {
contextValue = new JAXBContextValue(context, CONSTRUCTION_TYPE.BY_CONTEXT_PATH);
}
if (log.isDebugEnabled()) {
log.debug("Building a JAXBContext with packages only success=" + (contextValue != null));
}
}
// CLASS construction
if (contextValue == null) {
if (log.isDebugEnabled()) {
log.debug("Try building a JAXBContext using a list of classes.");
log.debug("Start finding classes");
}
it = contextPackages.iterator();
List<Class> fullList = new ArrayList<Class>();
while (it.hasNext()) {
String pkg = it.next();
fullList.addAll(getAllClassesFromPackage(pkg, cl));
}
//Lets add all common array classes
addCommonArrayClasses(fullList);
Class[] classArray = fullList.toArray(new Class[0]);
if (log.isDebugEnabled()) {
log.debug("End finding classes");
}
JAXBContext context = JAXBContext_newInstance(classArray, cl, properties, classRefs);
if (context != null) {
if (forceArrays) {
contextValue = new JAXBContextValue(context, CONSTRUCTION_TYPE.BY_CLASS_ARRAY_PLUS_ARRAYS);
} else {
contextValue = new JAXBContextValue(context, CONSTRUCTION_TYPE.BY_CLASS_ARRAY);
}
}
}
if (log.isDebugEnabled()) {
log.debug("Successfully created JAXBContext " + contextValue.jaxbContext.toString());
}
return contextValue;
}