Author: dkulp
Date: Mon Mar 31 09:40:51 2008
New Revision: 643053
URL: http://svn.apache.org/viewvc?rev=643053&view=rev
Log:
Merged revisions 643021 via svnmerge from
https://svn.apache.org/repos/asf/incubator/cxf/trunk
........
r643021 | dkulp | 2008-03-31 11:12:52 -0400 (Mon, 31 Mar 2008) | 2 lines
Update JAXB data binding to cache the schema DOMs (with major help from
Benson)
........
Modified:
incubator/cxf/branches/2.0.x-fixes/ (props changed)
incubator/cxf/branches/2.0.x-fixes/api/src/main/java/org/apache/cxf/service/model/ServiceInfo.java
incubator/cxf/branches/2.0.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
incubator/cxf/branches/2.0.x-fixes/rt/core/src/main/java/org/apache/cxf/databinding/source/AbstractDataBinding.java
incubator/cxf/branches/2.0.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java
Propchange: incubator/cxf/branches/2.0.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.
Modified:
incubator/cxf/branches/2.0.x-fixes/api/src/main/java/org/apache/cxf/service/model/ServiceInfo.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/2.0.x-fixes/api/src/main/java/org/apache/cxf/service/model/ServiceInfo.java?rev=643053&r1=643052&r2=643053&view=diff
==============================================================================
---
incubator/cxf/branches/2.0.x-fixes/api/src/main/java/org/apache/cxf/service/model/ServiceInfo.java
(original)
+++
incubator/cxf/branches/2.0.x-fixes/api/src/main/java/org/apache/cxf/service/model/ServiceInfo.java
Mon Mar 31 09:40:51 2008
@@ -150,4 +150,10 @@
xmlSchemaCollection = serviceSchemaInfo.getSchemaCollection();
schemas = serviceSchemaInfo.getSchemaInfoList();
}
+
+ public void setSchemas(SchemaCollection cachedXmlSchemaCollection,
+ List<SchemaInfo> cachedSchemas) {
+ xmlSchemaCollection = cachedXmlSchemaCollection;
+ schemas = cachedSchemas;
+ }
}
Modified:
incubator/cxf/branches/2.0.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/2.0.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java?rev=643053&r1=643052&r2=643053&view=diff
==============================================================================
---
incubator/cxf/branches/2.0.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
(original)
+++
incubator/cxf/branches/2.0.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
Mon Mar 31 09:40:51 2008
@@ -30,6 +30,7 @@
import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.StreamFilter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
@@ -255,6 +256,16 @@
return false;
}
+ public static Document copy(Document doc)
+ throws XMLStreamException, ParserConfigurationException {
+
+ XMLStreamReader reader = createXMLStreamReader(doc);
+ W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
+ copy(reader, writer);
+ Document d = writer.getDocument();
+ d.setDocumentURI(doc.getDocumentURI());
+ return d;
+ }
public static void copy(Document doc, XMLStreamWriter writer) throws
XMLStreamException {
XMLStreamReader reader = createXMLStreamReader(doc);
copy(reader, writer);
Modified:
incubator/cxf/branches/2.0.x-fixes/rt/core/src/main/java/org/apache/cxf/databinding/source/AbstractDataBinding.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/2.0.x-fixes/rt/core/src/main/java/org/apache/cxf/databinding/source/AbstractDataBinding.java?rev=643053&r1=643052&r2=643053&view=diff
==============================================================================
---
incubator/cxf/branches/2.0.x-fixes/rt/core/src/main/java/org/apache/cxf/databinding/source/AbstractDataBinding.java
(original)
+++
incubator/cxf/branches/2.0.x-fixes/rt/core/src/main/java/org/apache/cxf/databinding/source/AbstractDataBinding.java
Mon Mar 31 09:40:51 2008
@@ -23,6 +23,8 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.stream.XMLStreamException;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.Document;
@@ -34,6 +36,7 @@
import org.apache.cxf.common.xmlschema.SchemaCollection;
import org.apache.cxf.service.model.SchemaInfo;
import org.apache.cxf.service.model.ServiceInfo;
+import org.apache.cxf.staxutils.StaxUtils;
import org.apache.ws.commons.schema.XmlSchema;
public class AbstractDataBinding {
@@ -52,6 +55,9 @@
String systemId) {
String ns = d.getDocumentElement().getAttribute("targetNamespace");
if (StringUtils.isEmpty(ns)) {
+ //create a copy of the dom so we
+ //can modify it.
+ d = copy(d);
ns = serviceInfo.getInterface().getName().getNamespaceURI();
d.getDocumentElement().setAttribute("targetNamespace", ns);
}
@@ -73,7 +79,17 @@
serviceInfo.addSchema(schema);
return xmlSchema;
}
-
+ private Document copy(Document doc) {
+ try {
+ return StaxUtils.copy(doc);
+ } catch (XMLStreamException e) {
+ //ignore
+ } catch (ParserConfigurationException e) {
+ //ignore
+ }
+ return doc;
+ }
+
/**
* @return Returns the namespaceMap.
*/
Modified:
incubator/cxf/branches/2.0.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/2.0.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java?rev=643053&r1=643052&r2=643053&view=diff
==============================================================================
---
incubator/cxf/branches/2.0.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java
(original)
+++
incubator/cxf/branches/2.0.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java
Mon Mar 31 09:40:51 2008
@@ -75,12 +75,11 @@
public final class JAXBDataBinding extends AbstractDataBinding implements
DataBinding {
public static final String SCHEMA_RESOURCE = "SCHEMRESOURCE";
-
+
public static final String UNWRAP_JAXB_ELEMENT = "unwrap.jaxb.element";
private static final Logger LOG =
LogUtils.getLogger(JAXBDataBinding.class);
-
private static final Class<?> SUPPORTED_READER_FORMATS[] = new Class<?>[]
{Node.class,
XMLEventReader.class,
XMLStreamReader.class};
@@ -89,11 +88,33 @@
XMLEventWriter.class,
XMLStreamWriter.class};
- private static final Map<Set<Class<?>>, JAXBContext> JAXBCONTEXT_CACHE =
- new CacheMap<Set<Class<?>>, JAXBContext>();
+ private static final class CachedContextAndSchemas {
+ private JAXBContext context;
+ private Collection<DOMSource> schemas;
+
+ CachedContextAndSchemas(JAXBContext context) {
+ this.context = context;
+ }
+
+ public JAXBContext getContext() {
+ return context;
+ }
+
+ public Collection<DOMSource> getSchemas() {
+ return schemas;
+ }
+
+ public void setSchemas(Collection<DOMSource> schemas) {
+ this.schemas = schemas;
+ }
+
+ }
+
+ private static final Map<Set<Class<?>>, CachedContextAndSchemas>
JAXBCONTEXT_CACHE
+ = new CacheMap<Set<Class<?>>, CachedContextAndSchemas>();
Class[] extraClass;
-
+
JAXBContext context;
Set<Class<?>> contextClasses;
@@ -104,15 +125,15 @@
private boolean qualifiedSchemas;
-
+
public JAXBDataBinding() {
}
public JAXBDataBinding(boolean q) {
this.qualifiedSchemas = q;
}
-
- public JAXBDataBinding(Class<?>...classes) throws JAXBException {
+
+ public JAXBDataBinding(Class<?>... classes) throws JAXBException {
contextClasses = new HashSet<Class<?>>();
contextClasses.addAll(Arrays.asList(classes));
setContext(createJAXBContext(contextClasses));
@@ -129,16 +150,16 @@
public void setContext(JAXBContext ctx) {
context = ctx;
- }
-
+ }
+
private NamespacePrefixMapper getNamespacePrefixMapper() {
Map<String, String> mappings = getDeclaredNamespaceMappings();
if (mappings == null) {
mappings = Collections.emptyMap();
}
-
+
final Map<String, String> closedMappings = mappings;
-
+
NamespacePrefixMapper mapper = new NamespacePrefixMapper() {
@Override
public String getPreferredPrefix(String namespaceUri, String
suggestion, boolean requirePrefix) {
@@ -147,12 +168,11 @@
return prefix;
}
return suggestion;
- }
+ }
};
return mapper;
}
-
@SuppressWarnings("unchecked")
public <T> DataWriter<T> createWriter(Class<T> c) {
Map<String, Object> currentMarshallerProperties = new HashMap<String,
Object>();
@@ -190,61 +210,64 @@
} else if (c == Node.class) {
dr = (DataReader<T>)new DataReaderImpl<Node>(context);
}
-
+
return dr;
}
public Class<?>[] getSupportedReaderFormats() {
return SUPPORTED_READER_FORMATS;
}
-
+
public void initialize(Service service) {
- //context is already set, don't redo it
+ // context is already set, don't redo it
if (context != null) {
return;
}
-
+
+ CachedContextAndSchemas cachedContextAndSchemas = null;
+
contextClasses = new HashSet<Class<?>>();
for (ServiceInfo serviceInfo : service.getServiceInfos()) {
- JAXBContextInitializer initializer =
- new JAXBContextInitializer(serviceInfo, contextClasses);
+ JAXBContextInitializer initializer = new
JAXBContextInitializer(serviceInfo, contextClasses);
initializer.walk();
-
+
}
-
- String tns = service.getName().getNamespaceURI();
+
+ String tns = service.getName().getNamespaceURI();
JAXBContext ctx = null;
- try {
+ try {
if (service.getServiceInfos().size() > 0) {
tns =
service.getServiceInfos().get(0).getInterface().getName().getNamespaceURI();
}
ctx = createJAXBContext(contextClasses, tns);
+ cachedContextAndSchemas = JAXBCONTEXT_CACHE.get(contextClasses);
} catch (JAXBException e1) {
- //load jaxb needed class and try to create jaxb context for more
times
- boolean added = addJaxbObjectFactory(e1);
+ // load jaxb needed class and try to create jaxb context for more
+ // times
+ boolean added = addJaxbObjectFactory(e1);
while (ctx == null && added) {
try {
- synchronized (JAXBCONTEXT_CACHE) {
- ctx =
JAXBContext.newInstance(contextClasses.toArray(new Class[contextClasses.size()])
- , null);
- JAXBCONTEXT_CACHE.put(contextClasses, ctx);
- }
+ synchronized (JAXBCONTEXT_CACHE) {
+ ctx = JAXBContext.newInstance(contextClasses
+ .toArray(new Class[contextClasses.size()]), null);
+ cachedContextAndSchemas = new
CachedContextAndSchemas(ctx);
+ JAXBCONTEXT_CACHE.put(contextClasses,
cachedContextAndSchemas);
+ }
} catch (JAXBException e) {
e1 = e;
- added = addJaxbObjectFactory(e1);
+ added = addJaxbObjectFactory(e1);
}
}
if (ctx == null) {
throw new ServiceConstructionException(e1);
}
}
-
+
if (LOG.isLoggable(Level.FINE)) {
LOG.log(Level.FINE, "CREATED_JAXB_CONTEXT", new Object[] {ctx,
contextClasses});
}
setContext(ctx);
-
-
+
for (ServiceInfo serviceInfo : service.getServiceInfos()) {
SchemaCollection col = serviceInfo.getXmlSchemaCollection();
@@ -252,50 +275,55 @@
// someone has already filled in the types
continue;
}
-
- Collection<DOMSource> schemas = getSchemas();
- if (schemas != null) {
- for (DOMSource r : schemas) {
- addSchemaDocument(serviceInfo, col,
- (Document)r.getNode(), r.getSystemId());
- }
- } else {
+
+ boolean schemasFromCache = false;
+ Collection<DOMSource> schemas =
cachedContextAndSchemas.getSchemas();
+ if (schemas == null) {
+ schemas = getSchemas();
+ }
+ if (schemas == null) {
+ schemas = new HashSet<DOMSource>();
try {
for (DOMResult r : generateJaxbSchemas()) {
- addSchemaDocument(serviceInfo, col,
- (Document)r.getNode(),
r.getSystemId());
+ schemas.add(new DOMSource(r.getNode()));
}
} catch (IOException e) {
throw new ServiceConstructionException(new
Message("SCHEMA_GEN_EXC", LOG), e);
}
}
-
+ for (DOMSource r : schemas) {
+ addSchemaDocument(serviceInfo,
+ col,
+ (Document)r.getNode(),
+ r.getSystemId());
+ }
+
JAXBContextImpl riContext;
if (context instanceof JAXBContextImpl) {
- riContext = (JAXBContextImpl) context;
+ riContext = (JAXBContextImpl)context;
} else {
// fall back if we're using another jaxb implementation
try {
- riContext = (JAXBContextImpl)
- ContextFactory.createContext(
- contextClasses.toArray(new
Class[contextClasses.size()]), null);
+ riContext =
(JAXBContextImpl)ContextFactory.createContext(contextClasses
+ .toArray(new Class[contextClasses.size()]), null);
} catch (JAXBException e) {
throw new ServiceConstructionException(e);
}
}
-
- JAXBSchemaInitializer schemaInit = new
JAXBSchemaInitializer(serviceInfo,
- col,
-
riContext,
+
+ JAXBSchemaInitializer schemaInit = new
JAXBSchemaInitializer(serviceInfo, col, riContext,
this.qualifiedSchemas);
schemaInit.walk();
+ if (cachedContextAndSchemas != null && !schemasFromCache) {
+ cachedContextAndSchemas.setSchemas(schemas);
+ }
}
}
-
+
public void setExtraClass(Class[] userExtraClass) {
extraClass = userExtraClass;
}
-
+
public Class[] getExtraClass() {
return extraClass;
}
@@ -308,18 +336,15 @@
{
builtIns.put("http://www.w3.org/2005/02/addressing/wsdl",
"classpath:/schemas/wsdl/ws-addr-wsdl.xsd");
- builtIns.put("http://www.w3.org/2005/08/addressing",
- "classpath:/schemas/wsdl/ws-addr.xsd");
- builtIns.put("http://schemas.xmlsoap.org/ws/2005/02/rm",
- "classpath:/schemas/wsdl/wsrm.xsd");
- builtIns.put("http://www.w3.org/2005/05/xmlmime",
- "classpath:/schemas/wsdl/ws-addr.xsd");
+ builtIns.put("http://www.w3.org/2005/08/addressing",
"classpath:/schemas/wsdl/ws-addr.xsd");
+ builtIns.put("http://schemas.xmlsoap.org/ws/2005/02/rm",
"classpath:/schemas/wsdl/wsrm.xsd");
+ builtIns.put("http://www.w3.org/2005/05/xmlmime",
"classpath:/schemas/wsdl/ws-addr.xsd");
}
-
+
@Override
public Result createOutput(String ns, String file) throws
IOException {
DOMResult result = new DOMResult();
-
+
if (builtIns.containsKey(ns)) {
result.setSystemId(builtIns.get(ns));
return result;
@@ -332,14 +357,12 @@
return results;
}
-
public JAXBContext createJAXBContext(Set<Class<?>> classes) throws
JAXBException {
return createJAXBContext(classes, null);
}
-
- public JAXBContext createJAXBContext(Set<Class<?>> classes,
- String defaultNs) throws
JAXBException {
+
+ public JAXBContext createJAXBContext(Set<Class<?>> classes, String
defaultNs) throws JAXBException {
Iterator it = classes.iterator();
String className = "";
Object remoteExceptionObject = null;
@@ -356,24 +379,26 @@
classes.add(clz);
}
}
-
- //try and read any jaxb.index files that are with the other classes.
This should
- //allow loading of extra classes (such as subclasses for inheritance
reasons)
- //that are in the same package. Also check for ObjectFactory classes
+
+ // try and read any jaxb.index files that are with the other classes.
+ // This should
+ // allow loading of extra classes (such as subclasses for inheritance
+ // reasons)
+ // that are in the same package. Also check for ObjectFactory classes
Map<String, InputStream> packages = new HashMap<String, InputStream>();
Map<String, ClassLoader> packageLoaders = new HashMap<String,
ClassLoader>();
Set<Class<?>> objectFactories = new HashSet<Class<?>>();
for (Class<?> jcls : classes) {
- String pkgName = PackageUtils.getPackageName(jcls);
+ String pkgName = PackageUtils.getPackageName(jcls);
if (!packages.containsKey(pkgName)) {
packages.put(pkgName, jcls.getResourceAsStream("jaxb.index"));
packageLoaders.put(pkgName, jcls.getClassLoader());
try {
- Class<?> ofactory = Class.forName(pkgName + "." +
"ObjectFactory",
- false, jcls.getClassLoader());
+ Class<?> ofactory = Class.forName(pkgName + "." +
"ObjectFactory", false, jcls
+ .getClassLoader());
objectFactories.add(ofactory);
} catch (ClassNotFoundException e) {
- //ignore
+ // ignore
}
}
}
@@ -387,7 +412,7 @@
if (!StringUtils.isEmpty(pkg)) {
pkg += ".";
}
-
+
String line = reader.readLine();
while (line != null) {
line = line.trim();
@@ -399,18 +424,18 @@
Class<?> ncls = Class.forName(pkg + line,
false, loader);
classes.add(ncls);
} catch (Exception e) {
- //ignore
+ // ignore
}
}
line = reader.readLine();
}
} catch (Exception e) {
- //ignore
+ // ignore
} finally {
try {
entry.getValue().close();
} catch (Exception e) {
- //ignore
+ // ignore
}
}
}
@@ -420,29 +445,33 @@
for (Class<?> clz : classes) {
if (clz.getName().endsWith("ObjectFactory")) {
- //kind of a hack, but ObjectFactories may be created with
empty namespaces
+ // kind of a hack, but ObjectFactories may be created with
empty
+ // namespaces
defaultNs = null;
}
}
-
+
Map<String, Object> map = new HashMap<String, Object>();
if (defaultNs != null) {
map.put("com.sun.xml.bind.defaultNamespaceRemap", defaultNs);
}
-
+
if (contextProperties != null) {
- //add any specified context properties into the properties map
+ // add any specified context properties into the properties map
map.putAll(contextProperties);
- }
-
+ }
+
+ CachedContextAndSchemas cachedContextAndSchemas = null;
synchronized (JAXBCONTEXT_CACHE) {
if (!JAXBCONTEXT_CACHE.containsKey(classes)) {
JAXBContext ctx = JAXBContext.newInstance(classes.toArray(new
Class[classes.size()]), map);
- JAXBCONTEXT_CACHE.put(classes, ctx);
+ cachedContextAndSchemas = new CachedContextAndSchemas(ctx);
+ JAXBCONTEXT_CACHE.put(classes, cachedContextAndSchemas);
}
}
- return JAXBCONTEXT_CACHE.get(classes);
+ cachedContextAndSchemas = JAXBCONTEXT_CACHE.get(classes);
+ return cachedContextAndSchemas.getContext();
}
private void addWsAddressingTypes(Set<Class<?>> classes) {
@@ -456,36 +485,36 @@
// REVISIT - ignorable if WS-ADDRESSING not available?
// maybe add a way to allow interceptors to add stuff to the
// context?
- }
+ }
}
}
-
-
- //Now we can not add all the classes that Jaxb needed into JaxbContext,
especially when
- //an ObjectFactory is pointed to by an jaxb @XmlElementDecl annotation
- //added this workaround method to load the jaxb needed ObjectFactory class
+
+ // Now we can not add all the classes that Jaxb needed into JaxbContext,
+ // especially when
+ // an ObjectFactory is pointed to by an jaxb @XmlElementDecl annotation
+ // added this workaround method to load the jaxb needed ObjectFactory class
public boolean addJaxbObjectFactory(JAXBException e1) {
boolean added = false;
java.io.ByteArrayOutputStream bout = new
java.io.ByteArrayOutputStream();
java.io.PrintStream pout = new java.io.PrintStream(bout);
e1.printStackTrace(pout);
String str = new String(bout.toByteArray());
- Pattern pattern =
Pattern.compile("(?<=There's\\sno\\sObjectFactory\\swith\\san\\s"
+ Pattern pattern =
Pattern.compile("(?<=There's\\sno\\sObjectFactory\\swith\\san\\s"
+
"@XmlElementDecl\\sfor\\sthe\\selement\\s\\{)\\S*(?=\\})");
- java.util.regex.Matcher matcher = pattern.matcher(str);
- while (matcher.find()) {
+ java.util.regex.Matcher matcher = pattern.matcher(str);
+ while (matcher.find()) {
String pkgName = JAXBUtils.namespaceURIToPackage(matcher.group());
try {
- Class clz = getClass().getClassLoader().loadClass(pkgName +
"." + "ObjectFactory");
-
+ Class clz = getClass().getClassLoader().loadClass(pkgName +
"." + "ObjectFactory");
+
if (!contextClasses.contains(clz)) {
contextClasses.add(clz);
added = true;
}
} catch (ClassNotFoundException e) {
- //do nothing
+ // do nothing
}
-
+
}
return added;
}
@@ -493,6 +522,7 @@
/**
* Return a map of properties. These properties are passed to
* JAXBContext.newInstance when this object creates a context.
+ *
* @return the map of JAXB context properties.
*/
public Map<String, Object> getContextProperties() {
@@ -500,20 +530,22 @@
}
/**
- * Set a map of JAXB context properties. These properties are passed
- * to JAXBContext.newInstance when this object creates a context.
- * Note that if you create a JAXB context elsewhere, you will
- * not respect these properties unless you handle it manually.
+ * Set a map of JAXB context properties. These properties are passed to
+ * JAXBContext.newInstance when this object creates a context. Note that if
+ * you create a JAXB context elsewhere, you will not respect these
+ * properties unless you handle it manually.
*
* @param contextProperties map of properties.
*/
public void setContextProperties(Map<String, Object> contextProperties) {
this.contextProperties = contextProperties;
}
-
+
/**
- * Return a map of properties. These properties are set into the JAXB
Marshaller
- * (via Marshaller.setProperty(...) when the marshaller is created.
+ * Return a map of properties. These properties are set into the JAXB
+ * Marshaller (via Marshaller.setProperty(...) when the marshaller is
+ * created.
+ *
* @return the map of JAXB marshaller properties.
*/
public Map<String, Object> getMarshallerProperties() {
@@ -521,8 +553,9 @@
}
/**
- * Set a map of JAXB marshaller properties. These properties are set into
the JAXB Marshaller
- * (via Marshaller.setProperty(...) when the marshaller is created.
+ * Set a map of JAXB marshaller properties. These properties are set into
+ * the JAXB Marshaller (via Marshaller.setProperty(...) when the marshaller
+ * is created.
*
* @param marshallerProperties map of properties.
*/