org.apache.cxf.common.jaxb.JAXBContextCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cxf-bundle-minimal Show documentation
Show all versions of cxf-bundle-minimal Show documentation
Apache CXF Minimal Bundle Jar
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.cxf.common.jaxb;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.transform.dom.DOMSource;
import org.apache.cxf.common.classloader.ClassLoaderUtils;
import org.apache.cxf.common.util.CacheMap;
import org.apache.cxf.common.util.CachedClass;
import org.apache.cxf.common.util.StringUtils;
/**
*
*/
public final class JAXBContextCache {
/**
* Return holder of the context, classes, etc...
* Do NOT hold onto these strongly as that can lock the JAXBContext and Set objects
* into memory. It preferred to grab the context and classes (if needed) from this object
* immediately after the call to getCachedContextAndSchemas and then discard it. The
* main purpose of this class is to hold onto the context/set strongly until the caller
* has a chance to copy those into a place where they can hold onto it strongly as
* needed.
*/
public static final class CachedContextAndSchemas {
private final JAXBContext context;
private final Set> classes;
private final WeakReference ccas;
private CachedContextAndSchemas(JAXBContext context, Set> classes, CachedContextAndSchemasInternal i) {
this.context = context;
this.classes = classes;
ccas = new WeakReference(i);
}
private CachedContextAndSchemas(CachedContextAndSchemasInternal i) {
this.context = i.getContext();
this.classes = i.getClasses();
ccas = new WeakReference(i);
}
public JAXBContext getContext() {
return context;
}
public Set> getClasses() {
return classes;
}
public Collection getSchemas() {
CachedContextAndSchemasInternal i = ccas.get();
if (i != null) {
return i.getSchemas();
}
return null;
}
public void setSchemas(Collection schemas) {
CachedContextAndSchemasInternal i = ccas.get();
if (i != null) {
i.setSchemas(schemas);
}
}
}
private static final class CachedContextAndSchemasInternal {
private final WeakReference context;
private final WeakReference>> classes;
private Collection schemas;
CachedContextAndSchemasInternal(JAXBContext context, Set> classes) {
this.context = new WeakReference(context);
this.classes = new WeakReference>>(classes);
}
public JAXBContext getContext() {
return context.get();
}
public Set> getClasses() {
return classes.get();
}
public Collection getSchemas() {
return schemas;
}
public void setSchemas(Collection schemas) {
this.schemas = schemas;
}
}
private static final Map>, CachedContextAndSchemasInternal> JAXBCONTEXT_CACHE
= new CacheMap>, CachedContextAndSchemasInternal>();
private static final Map OBJECT_FACTORY_CACHE
= new CacheMap();
private JAXBContextCache() {
//utility class
}
/**
* Clear any caches to make sure new contexts are created
*/
public static void clearCaches() {
synchronized (JAXBCONTEXT_CACHE) {
JAXBCONTEXT_CACHE.clear();
}
synchronized (OBJECT_FACTORY_CACHE) {
OBJECT_FACTORY_CACHE.clear();
}
}
public static void scanPackages(Set> classes) {
JAXBUtils.scanPackages(classes, OBJECT_FACTORY_CACHE);
}
public static CachedContextAndSchemas getCachedContextAndSchemas(Class> cls) throws JAXBException {
Set> classes = new HashSet>();
classes.add(cls);
scanPackages(classes);
return JAXBContextCache.getCachedContextAndSchemas(classes, null, null, null, false);
}
public static CachedContextAndSchemas getCachedContextAndSchemas(String pkg,
Map props,
ClassLoader loader)
throws JAXBException {
Set> classes = new HashSet>();
addPackage(classes, pkg, loader);
return getCachedContextAndSchemas(classes, null, props, null, true);
}
public static CachedContextAndSchemas getCachedContextAndSchemas(final Set> classes,
String defaultNs,
Map props,
Collection