jaxbutils.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 893 行 · 第 1/3 页
JAVA
893 行
// CLASS construction
if (contextValue == null) {
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]);
JAXBContext context = JAXBContext_newInstance(classArray);
if (context != null) {
contextValue = new JAXBContextValue(context, CONSTRUCTION_TYPE.BY_CLASS_ARRAY);
}
}
if (log.isDebugEnabled()) {
log.debug("Successfully created JAXBContext " + contextValue.jaxbContext.toString());
}
return contextValue;
}
/**
* Get the unmarshaller. You must call releaseUnmarshaller to put it back into the pool
*
* @param context JAXBContext
* @return Unmarshaller
* @throws JAXBException
*/
public static Unmarshaller getJAXBUnmarshaller(JAXBContext context) throws JAXBException {
if (!ENABLE_ADV_POOLING) {
if (log.isDebugEnabled()) {
log.debug("Unmarshaller created [no pooling]");
}
return context.createUnmarshaller();
}
Unmarshaller u = umap.remove(context);
if (u == null) {
if (log.isDebugEnabled()) {
log.debug("Unmarshaller created [not in pool]");
}
u = context.createUnmarshaller();
} else {
if (log.isDebugEnabled()) {
log.debug("Unmarshaller obtained [from pool]");
}
}
return u;
}
/**
* Release Unmarshaller Do not call this method if an exception occurred while using the
* Unmarshaller. We object my be in an invalid state.
*
* @param context JAXBContext
* @param unmarshaller Unmarshaller
*/
public static void releaseJAXBUnmarshaller(JAXBContext context, Unmarshaller unmarshaller) {
if (log.isDebugEnabled()) {
log.debug("Unmarshaller placed back into pool");
}
if (ENABLE_ADV_POOLING) {
adjustPoolSize(umap);
umap.put(context, unmarshaller);
}
}
/**
* Get JAXBMarshaller
*
* @param context JAXBContext
* @return Marshaller
* @throws JAXBException
*/
public static Marshaller getJAXBMarshaller(JAXBContext context) throws JAXBException {
Marshaller m = null;
if (!ENABLE_ADV_POOLING) {
if (log.isDebugEnabled()) {
log.debug("Marshaller created [no pooling]");
}
m = context.createMarshaller();
} else {
m = mmap.remove(context);
if (m == null) {
if (log.isDebugEnabled()) {
log.debug("Marshaller created [not in pool]");
}
m = context.createMarshaller();
} else {
if (log.isDebugEnabled()) {
log.debug("Marshaller obtained [from pool]");
}
}
}
m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); // No PIs
return m;
}
/**
* releaseJAXBMarshalller Do not call this method if an exception occurred while using the
* Marshaller. We object my be in an invalid state.
*
* @param context JAXBContext
* @param marshaller Marshaller
*/
public static void releaseJAXBMarshaller(JAXBContext context, Marshaller marshaller) {
if (log.isDebugEnabled()) {
log.debug("Marshaller placed back into pool");
}
if (ENABLE_ADV_POOLING) {
adjustPoolSize(mmap);
mmap.put(context, marshaller);
}
}
/**
* get JAXB Introspector
*
* @param context JAXBContext
* @return JAXBIntrospector
* @throws JAXBException
*/
public static JAXBIntrospector getJAXBIntrospector(JAXBContext context) throws JAXBException {
JAXBIntrospector i = null;
if (!ENABLE_ADV_POOLING) {
if (log.isDebugEnabled()) {
log.debug("JAXBIntrospector created [no pooling]");
}
i = context.createJAXBIntrospector();
} else {
i = imap.remove(context);
if (i == null) {
if (log.isDebugEnabled()) {
log.debug("JAXBIntrospector created [not in pool]");
}
i = context.createJAXBIntrospector();
} else {
if (log.isDebugEnabled()) {
log.debug("JAXBIntrospector obtained [from pool]");
}
}
}
return i;
}
/**
* Release JAXBIntrospector Do not call this method if an exception occurred while using the
* JAXBIntrospector. We object my be in an invalid state.
*
* @param context JAXBContext
* @param introspector JAXBIntrospector
*/
public static void releaseJAXBIntrospector(JAXBContext context, JAXBIntrospector introspector) {
if (log.isDebugEnabled()) {
log.debug("JAXBIntrospector placed back into pool");
}
if (ENABLE_ADV_POOLING) {
adjustPoolSize(imap);
imap.put(context, introspector);
}
}
/**
* @param p Package
* @param cl
* @return true if each package has a ObjectFactory class or package-info
*/
private static boolean checkPackage(String p, ClassLoader cl) {
// Each package must have an ObjectFactory
if (log.isDebugEnabled()) {
log.debug("checking package :" + p);
}
try {
Class cls = forName(p + ".ObjectFactory", false, cl);
if (cls != null) {
return true;
}
//Catch Throwable as ClassLoader can throw an NoClassDefFoundError that
//does not extend Exception. So we will absorb any Throwable exception here.
} catch (Throwable e) {
if (log.isDebugEnabled()) {
log.debug("ObjectFactory Class Not Found " + e);
log.debug("...caused by " + e.getCause() + " " + JavaUtils.stackToString(e));
}
}
try {
Class cls = forName(p + ".package-info", false, cl);
if (cls != null) {
return true;
}
//Catch Throwable as ClassLoader can throw an NoClassDefFoundError that
//does not extend Exception. So we will absorb any Throwable exception here.
} catch (Throwable e) {
if (log.isDebugEnabled()) {
log.debug("package-info Class Not Found " + e);
log.debug("...caused by " + e.getCause() + " " + JavaUtils.stackToString(e));
}
}
return false;
}
/**
* Create a JAXBContext using the contextpath approach
*
* @param packages
* @param cl ClassLoader
* @return JAXBContext or null if unsuccessful
*/
private static JAXBContext createJAXBContextUsingContextPath(TreeSet<String> packages,
ClassLoader cl) {
JAXBContext context = null;
String contextpath = "";
// Iterate through the classes and build the contextpath
Iterator<String> it = packages.iterator();
while (it.hasNext()) {
String p = it.next();
if (contextpath.length() != 0) {
contextpath += ":";
}
contextpath += p;
}
try {
if (log.isDebugEnabled()) {
log.debug("Attempting to create JAXBContext with contextPath=" + contextpath);
}
context = JAXBContext_newInstance(contextpath, cl);
if (log.isDebugEnabled()) {
log.debug(" Successfully created JAXBContext:" + context);
}
} catch (Throwable e) {
if (log.isDebugEnabled()) {
log.debug(
" Unsuccessful: We will now use an alterative JAXBConstruct construction");
log.debug(" Reason " + e.toString());
}
}
return context;
}
/**
* This method will return all the Class names needed to construct a JAXBContext
*
* @param pkg Package
* @param ClassLoader cl
* @return
* @throws ClassNotFoundException if error occurs getting package
*/
private static List<Class> getAllClassesFromPackage(String pkg, ClassLoader cl) {
if (pkg == null) {
return new ArrayList<Class>();
}
/*
* This method is a best effort method. We should always return an object.
*/
ArrayList<Class> classes = new ArrayList<Class>();
try {
// This will load classes from directory
classes.addAll(getClassesFromDirectory(pkg, cl));
} catch (ClassNotFoundException e) {
if (log.isDebugEnabled()) {
log.debug("getClassesFromDirectory failed to get Classes");
}
}
try {
//If Clases not found in directory then look for jar that has these classes
if (classes.size() <= 0) {
//This will load classes from jar file.
ClassFinderFactory cff =
(ClassFinderFactory)FactoryRegistry.getFactory(ClassFinderFactory.class);
ClassFinder cf = cff.getClassFinder();
classes.addAll(cf.getClassesFromJarFile(pkg, cl));
}
} catch (ClassNotFoundException e) {
if (log.isDebugEnabled()) {
log.debug("getClassesFromJarFile failed to get Classes");
}
}
return classes;
}
private static ArrayList<Class> getClassesFromDirectory(String pkg, ClassLoader cl)
throws ClassNotFoundException {
// This will hold a list of directories matching the pckgname. There may be more than one if a package is split over multiple jars/paths
String pckgname = pkg;
ArrayList<File> directories = new ArrayList<File>();
try {
String path = pckgname.replace('.', '/');
// Ask for all resources for the path
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?