You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2013/08/03 03:30:55 UTC
svn commit: r1509931 - in /cxf/branches/2.7.x-fixes:
api/src/main/java/org/apache/cxf/common/jaxb/
rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/
Author: dkulp
Date: Sat Aug 3 01:30:54 2013
New Revision: 1509931
URL: http://svn.apache.org/r1509931
Log:
Merged revisions 1509891 via git cherry-pick from
https://svn.apache.org/repos/asf/cxf/trunk
........
r1509891 | dkulp | 2013-08-02 18:18:47 -0400 (Fri, 02 Aug 2013) | 2 lines
[CXF-5166] If not eclipse or sun, use the SchemaCollection to try and determine the schema types
........
Added:
cxf/branches/2.7.x-fixes/api/src/main/java/org/apache/cxf/common/jaxb/SchemaCollectionContextProxy.java
Modified:
cxf/branches/2.7.x-fixes/api/src/main/java/org/apache/cxf/common/jaxb/JAXBUtils.java
cxf/branches/2.7.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java
cxf/branches/2.7.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java
Modified: cxf/branches/2.7.x-fixes/api/src/main/java/org/apache/cxf/common/jaxb/JAXBUtils.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/api/src/main/java/org/apache/cxf/common/jaxb/JAXBUtils.java?rev=1509931&r1=1509930&r2=1509931&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/api/src/main/java/org/apache/cxf/common/jaxb/JAXBUtils.java (original)
+++ cxf/branches/2.7.x-fixes/api/src/main/java/org/apache/cxf/common/jaxb/JAXBUtils.java Sat Aug 3 01:30:54 2013
@@ -82,6 +82,7 @@ import org.apache.cxf.common.util.Reflec
import org.apache.cxf.common.util.ReflectionUtil;
import org.apache.cxf.common.util.StringUtils;
import org.apache.cxf.common.util.SystemPropertyAction;
+import org.apache.cxf.common.xmlschema.SchemaCollection;
import org.apache.cxf.helpers.JavaUtils;
@@ -1151,6 +1152,19 @@ public final class JAXBUtils {
cls, bts);
}
+ public static JAXBContextProxy createJAXBContextProxy(final JAXBContext ctx) {
+ return createJAXBContextProxy(ctx, null, null);
+ }
+ public static JAXBContextProxy createJAXBContextProxy(final JAXBContext ctx,
+ final SchemaCollection collection,
+ final String defaultNs) {
+ if (ctx.getClass().getName().contains("com.sun.")
+ || collection == null) {
+ return ReflectionInvokationHandler.createProxyWrapper(ctx, JAXBContextProxy.class);
+ }
+ return new SchemaCollectionContextProxy(ctx, collection, defaultNs);
+ }
+
public static JAXBBeanInfo getBeanInfo(JAXBContextProxy context, Class<?> cls) {
Object o = context.getBeanInfo(cls);
if (o == null) {
Added: cxf/branches/2.7.x-fixes/api/src/main/java/org/apache/cxf/common/jaxb/SchemaCollectionContextProxy.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/api/src/main/java/org/apache/cxf/common/jaxb/SchemaCollectionContextProxy.java?rev=1509931&view=auto
==============================================================================
--- cxf/branches/2.7.x-fixes/api/src/main/java/org/apache/cxf/common/jaxb/SchemaCollectionContextProxy.java (added)
+++ cxf/branches/2.7.x-fixes/api/src/main/java/org/apache/cxf/common/jaxb/SchemaCollectionContextProxy.java Sat Aug 3 01:30:54 2013
@@ -0,0 +1,240 @@
+/**
+ * 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.math.BigDecimal;
+import java.math.BigInteger;
+import java.net.URI;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.activation.DataHandler;
+import javax.activation.DataSource;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlSchema;
+import javax.xml.bind.annotation.XmlType;
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.transform.Source;
+
+import org.w3c.dom.Document;
+
+import org.apache.cxf.common.util.StringUtils;
+import org.apache.cxf.common.xmlschema.SchemaCollection;
+import org.apache.ws.commons.schema.XmlSchemaElement;
+import org.apache.ws.commons.schema.XmlSchemaType;
+import org.apache.ws.commons.schema.constants.Constants;
+
+/**
+ *
+ */
+public class SchemaCollectionContextProxy implements JAXBContextProxy {
+ private static final Map<Class<?>, QName> TYPE_MAP = new HashMap<Class<?>, QName>();
+
+ final JAXBContext context;
+ final SchemaCollection schemas;
+ final String defaultNamespace;
+
+
+ static {
+ defaultRegister(BigDecimal.class, Constants.XSD_DECIMAL);
+ defaultRegister(BigInteger.class, Constants.XSD_INTEGER);
+ defaultRegister(Boolean.class, Constants.XSD_BOOLEAN);
+ defaultRegister(Calendar.class, Constants.XSD_DATETIME);
+ defaultRegister(Date.class, Constants.XSD_DATETIME);
+ defaultRegister(Float.class, Constants.XSD_FLOAT);
+ defaultRegister(Double.class, Constants.XSD_DOUBLE);
+ defaultRegister(Integer.class, Constants.XSD_INT);
+ defaultRegister(Long.class, Constants.XSD_LONG);
+ defaultRegister(Object.class, Constants.XSD_ANYTYPE);
+ defaultRegister(Byte.class, Constants.XSD_BYTE);
+ defaultRegister(Short.class, Constants.XSD_SHORT);
+ defaultRegister(Source.class, Constants.XSD_ANYTYPE);
+ defaultRegister(String.class, Constants.XSD_STRING);
+ defaultRegister(Time.class, Constants.XSD_TIME);
+ defaultRegister(Timestamp.class, Constants.XSD_DATETIME);
+ defaultRegister(URI.class, Constants.XSD_ANYURI);
+ defaultRegister(XMLStreamReader.class, Constants.XSD_ANYTYPE);
+
+ defaultRegister(boolean.class, Constants.XSD_BOOLEAN);
+ defaultRegister(Date.class, Constants.XSD_DATETIME);
+ defaultRegister(Float.class, Constants.XSD_FLOAT);
+ defaultRegister(Double.class, Constants.XSD_DOUBLE);
+ defaultRegister(Integer.class, Constants.XSD_INT);
+ defaultRegister(Long.class, Constants.XSD_LONG);
+ defaultRegister(Object.class, Constants.XSD_ANYTYPE);
+ defaultRegister(Byte.class, Constants.XSD_BYTE);
+ defaultRegister(Short.class, Constants.XSD_SHORT);
+ defaultRegister(Source.class, Constants.XSD_ANYTYPE);
+ defaultRegister(String.class, Constants.XSD_STRING);
+ defaultRegister(Time.class, Constants.XSD_TIME);
+ defaultRegister(Timestamp.class, Constants.XSD_DATETIME);
+ defaultRegister(URI.class, Constants.XSD_ANYURI);
+ defaultRegister(XMLStreamReader.class, Constants.XSD_ANYTYPE);
+
+ defaultRegister(boolean.class, Constants.XSD_BOOLEAN);
+ defaultRegister(byte[].class, Constants.XSD_BASE64);
+ defaultRegister(double.class, Constants.XSD_DOUBLE);
+ defaultRegister(float.class, Constants.XSD_FLOAT);
+ defaultRegister(int.class, Constants.XSD_INT);
+ defaultRegister(short.class, Constants.XSD_SHORT);
+ defaultRegister(byte.class, Constants.XSD_BYTE);
+ defaultRegister(long.class, Constants.XSD_LONG);
+
+ defaultRegister(java.sql.Date.class, Constants.XSD_DATETIME);
+ defaultRegister(java.sql.Date.class, Constants.XSD_DATE);
+ defaultRegister(Number.class, Constants.XSD_DECIMAL);
+
+ defaultRegister(DataSource.class, Constants.XSD_BASE64);
+ defaultRegister(DataHandler.class, Constants.XSD_BASE64);
+ defaultRegister(Document.class, Constants.XSD_ANYTYPE);
+ }
+
+ public SchemaCollectionContextProxy(JAXBContext ctx, SchemaCollection c, String defaultNs) {
+ schemas = c;
+ context = ctx;
+ defaultNamespace = defaultNs;
+ }
+
+ private static void defaultRegister(Class<?> cls, QName name) {
+ TYPE_MAP.put(cls, name);
+ }
+
+ public Object getBeanInfo(Class<?> cls) {
+ XmlRootElement xre = cls.getAnnotation(XmlRootElement.class);
+ String name = xre == null ? "##default" : xre.name();
+ String namespace = xre == null ? "##default" : xre.namespace();
+ if ("##default".equals(name)) {
+ name = java.beans.Introspector.decapitalize(cls.getSimpleName());
+ }
+ if ("##default".equals(namespace) && cls.getPackage() != null) {
+ XmlSchema sc = cls.getPackage().getAnnotation(XmlSchema.class);
+ if (sc != null) {
+ namespace = sc.namespace();
+ }
+ }
+ if ("##default".equals(namespace) || StringUtils.isEmpty(namespace)) {
+ namespace = JAXBUtils.getPackageNamespace(cls);
+ if (namespace == null) {
+ namespace = defaultNamespace;
+ }
+ }
+ final QName qname = new QName(namespace, name);
+ final XmlSchemaElement el = schemas.getElementByQName(qname);
+ XmlSchemaType type = null;
+ if (el != null) {
+ type = el.getSchemaType();
+ }
+ if (type == null) {
+ type = schemas.getTypeByQName(getTypeQName(cls, namespace));
+ if (type == null) {
+ type = schemas.getTypeByQName(qname);
+ }
+ }
+ if (type == null) {
+ type = mapToSchemaType(cls, namespace);
+ /*
+ if (type == null) {
+ type = mapToSchemaType(cls, namespace);
+ }
+ */
+ }
+ if (el == null && type == null) {
+ return null;
+ }
+ final QName typeName = type == null ? null : type.getQName();
+
+ JAXBBeanInfo bi = new JAXBBeanInfo() {
+ public boolean isElement() {
+ return el == null ? false : true;
+ }
+ public Collection<QName> getTypeNames() {
+ return Collections.singletonList(typeName);
+ }
+ public String getElementNamespaceURI(Object object) {
+ return qname.getNamespaceURI();
+ }
+ public String getElementLocalName(Object object) {
+ return qname.getLocalPart();
+ }
+ };
+ return bi;
+ }
+
+ private QName getTypeQName(Class<?> cls, String namespace) {
+ QName qn = TYPE_MAP.get(cls);
+ if (qn != null) {
+ return qn;
+ }
+ XmlType xtype = cls.getAnnotation(XmlType.class);
+ String tn = xtype == null ? "##default" : xtype.name();
+ String tns = xtype == null ? "##default" : xtype.namespace();
+ if ("##default".equals(tn)) {
+ tn = java.beans.Introspector.decapitalize(cls.getSimpleName());
+ }
+ if ("##default".equals(tns) || StringUtils.isEmpty(tns)) {
+ tns = JAXBUtils.getPackageNamespace(cls);
+ }
+ if ("##default".equals(tns) || StringUtils.isEmpty(tns)) {
+ tns = namespace;
+ }
+ return new QName(tns, tn);
+ }
+ private XmlSchemaType mapToSchemaType(Class<?> cls, String namespace) {
+ QName qn = getTypeQName(cls, namespace);
+ XmlSchemaType type = null;
+ if (qn != null) {
+ type = schemas.getTypeByQName(qn);
+ }
+ if (type == null && cls.isArray()) {
+ Class<?> compType = cls.getComponentType();
+ int count = 1;
+ while (compType.isArray()) {
+ compType = compType.getComponentType();
+ count++;
+ }
+ QName aqn = getTypeQName(compType, namespace);
+ if (aqn != null) {
+ while (count > 0) {
+ aqn = new QName(aqn.getNamespaceURI(), aqn.getLocalPart() + "Array");
+ count--;
+ }
+ type = schemas.getTypeByQName(aqn);
+ if (type == null) {
+ type = schemas.getTypeByQName(new QName("http://jaxb.dev.java.net/array", aqn.getLocalPart()));
+ }
+ }
+ }
+ /*
+ if (type == null) {
+ System.out.println("HELP: " + cls.getName());
+ }
+ */
+ return type;
+ }
+
+}
Modified: cxf/branches/2.7.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java?rev=1509931&r1=1509930&r2=1509931&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java (original)
+++ cxf/branches/2.7.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java Sat Aug 3 01:30:54 2013
@@ -413,7 +413,7 @@ public class JAXBDataBinding extends Abs
}
JAXBSchemaInitializer schemaInit = new JAXBSchemaInitializer(serviceInfo, col, riContext,
- this.qualifiedSchemas);
+ this.qualifiedSchemas, tns);
schemaInit.walk();
if (cachedContextAndSchemas != null && !schemasFromCache) {
cachedContextAndSchemas.setSchemas(schemas);
@@ -424,13 +424,13 @@ public class JAXBDataBinding extends Abs
private void justCheckForJAXBAnnotations(ServiceInfo serviceInfo) {
for (MessageInfo mi: serviceInfo.getMessages().values()) {
for (MessagePartInfo mpi : mi.getMessageParts()) {
- checkForJAXBAnnotations(mpi);
+ checkForJAXBAnnotations(mpi, serviceInfo.getXmlSchemaCollection(), serviceInfo.getTargetNamespace());
}
}
}
- private void checkForJAXBAnnotations(MessagePartInfo mpi) {
+ private void checkForJAXBAnnotations(MessagePartInfo mpi, SchemaCollection schemaCollection, String ns) {
Annotation[] anns = (Annotation[])mpi.getProperty("parameter.annotations");
- JAXBContextProxy ctx = ReflectionInvokationHandler.createProxyWrapper(context, JAXBContextProxy.class);
+ JAXBContextProxy ctx = JAXBUtils.createJAXBContextProxy(context, schemaCollection, ns);
XmlJavaTypeAdapter jta = JAXBSchemaInitializer.findFromTypeAdapter(ctx, mpi.getTypeClass(), anns);
JAXBBeanInfo jtaBeanInfo = null;
if (jta != null) {
Modified: cxf/branches/2.7.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java?rev=1509931&r1=1509930&r2=1509931&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java (original)
+++ cxf/branches/2.7.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java Sat Aug 3 01:30:54 2013
@@ -84,10 +84,11 @@ class JAXBSchemaInitializer extends Serv
public JAXBSchemaInitializer(ServiceInfo serviceInfo,
SchemaCollection col,
JAXBContext context,
- boolean q) {
+ boolean q,
+ String defaultNs) {
super(serviceInfo);
schemas = col;
- this.context = ReflectionInvokationHandler.createProxyWrapper(context, JAXBContextProxy.class);
+ this.context = JAXBUtils.createJAXBContextProxy(context, serviceInfo.getXmlSchemaCollection(), defaultNs);
this.qualifiedSchemas = q;
}