ohair@286: /* alanb@368: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.ws.spi.db; ohair@286: ohair@286: import java.lang.reflect.Array; ohair@286: import java.lang.reflect.Field; ohair@286: import java.lang.reflect.GenericArrayType; ohair@286: import java.lang.reflect.Method; ohair@286: import java.lang.reflect.ParameterizedType; ohair@286: import java.lang.reflect.Type; ohair@286: import java.security.AccessController; ohair@286: import java.security.PrivilegedActionException; ohair@286: import java.security.PrivilegedExceptionAction; ohair@286: import java.util.ArrayList; alanb@368: import java.util.Arrays; ohair@286: import java.util.HashMap; ohair@286: import java.util.HashSet; ohair@286: import java.util.List; ohair@286: ohair@286: import javax.xml.bind.JAXBElement; ohair@286: import javax.xml.bind.annotation.XmlElement; ohair@286: import javax.xml.bind.annotation.XmlElementRef; ohair@286: import javax.xml.namespace.QName; ohair@286: ohair@286: /** ohair@286: * JAXBWrapperAccessor ohair@286: * ohair@286: * @author shih-chang.chen@oracle.com ohair@286: */ ohair@286: @SuppressWarnings({ "unchecked", "rawtypes" }) ohair@286: public class JAXBWrapperAccessor extends WrapperAccessor { ohair@286: ohair@286: protected Class contentClass; ohair@286: protected HashMap elementDeclaredTypes; ohair@286: ohair@286: public JAXBWrapperAccessor(Class wrapperBean) { ohair@286: contentClass = (Class) wrapperBean; ohair@286: ohair@286: HashMap setByQName = new HashMap(); ohair@286: HashMap setByLocalpart = new HashMap(); ohair@286: HashMap publicSetters = new HashMap(); ohair@286: ohair@286: HashMap getByQName = new HashMap(); ohair@286: HashMap getByLocalpart = new HashMap(); ohair@286: HashMap publicGetters = new HashMap(); ohair@286: ohair@286: HashMap elementDeclaredTypesByQName = new HashMap(); ohair@286: HashMap elementDeclaredTypesByLocalpart = new HashMap(); ohair@286: ohair@286: for (Method method : contentClass.getMethods()) { ohair@286: if (PropertySetterBase.setterPattern(method)) { ohair@286: String key = method.getName() ohair@286: .substring(3, method.getName().length()).toLowerCase(); ohair@286: publicSetters.put(key, method); ohair@286: } ohair@286: if (PropertyGetterBase.getterPattern(method)) { ohair@286: String methodName = method.getName(); ohair@286: String key = methodName.startsWith("is") ? methodName ohair@286: .substring(2, method.getName().length()).toLowerCase() ohair@286: : methodName.substring(3, method.getName().length()) ohair@286: .toLowerCase(); ohair@286: publicGetters.put(key, method); ohair@286: } ohair@286: } ohair@286: HashSet elementLocalNames = new HashSet(); ohair@286: for (Field field : getAllFields(contentClass)) { ohair@286: XmlElement xmlElem = field.getAnnotation(XmlElement.class); ohair@286: XmlElementRef xmlElemRef = field.getAnnotation(XmlElementRef.class); ohair@286: String fieldName = field.getName().toLowerCase(); ohair@286: String namespace = ""; ohair@286: String localName = field.getName(); ohair@286: if (xmlElem != null) { ohair@286: namespace = xmlElem.namespace(); ohair@286: if (xmlElem.name() != null && !xmlElem.name().equals("") ohair@286: && !xmlElem.name().equals("##default")) { ohair@286: localName = xmlElem.name(); ohair@286: } ohair@286: } else if (xmlElemRef != null) { ohair@286: namespace = xmlElemRef.namespace(); ohair@286: if (xmlElemRef.name() != null && !xmlElemRef.name().equals("") ohair@286: && !xmlElemRef.name().equals("##default")) { ohair@286: localName = xmlElemRef.name(); ohair@286: } ohair@286: } ohair@286: if (elementLocalNames.contains(localName)) { ohair@286: this.elementLocalNameCollision = true; ohair@286: } else { ohair@286: elementLocalNames.add(localName); ohair@286: } ohair@286: ohair@286: QName qname = new QName(namespace, localName); ohair@286: if (field.getType().equals(JAXBElement.class)) { ohair@286: if (field.getGenericType() instanceof ParameterizedType) { ohair@286: Type arg = ((ParameterizedType) field.getGenericType()) ohair@286: .getActualTypeArguments()[0]; ohair@286: if (arg instanceof Class) { ohair@286: elementDeclaredTypesByQName.put(qname, (Class) arg); ohair@286: elementDeclaredTypesByLocalpart.put(localName, ohair@286: (Class) arg); ohair@286: } else if (arg instanceof GenericArrayType) { ohair@286: Type componentType = ((GenericArrayType) arg) ohair@286: .getGenericComponentType(); ohair@286: if (componentType instanceof Class) { ohair@286: Class arrayClass = Array.newInstance( ohair@286: (Class) componentType, 0).getClass(); ohair@286: elementDeclaredTypesByQName.put(qname, arrayClass); ohair@286: elementDeclaredTypesByLocalpart.put(localName, ohair@286: arrayClass); ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: } ohair@286: // _return ohair@286: if (fieldName.startsWith("_") && !localName.startsWith("_")) { ohair@286: fieldName = fieldName.substring(1); ohair@286: } ohair@286: Method setMethod = publicSetters.get(fieldName); ohair@286: Method getMethod = publicGetters.get(fieldName); ohair@286: PropertySetter setter = createPropertySetter(field, setMethod); ohair@286: PropertyGetter getter = createPropertyGetter(field, getMethod); ohair@286: setByQName.put(qname, setter); ohair@286: setByLocalpart.put(localName, setter); ohair@286: getByQName.put(qname, getter); ohair@286: getByLocalpart.put(localName, getter); ohair@286: } ohair@286: if (this.elementLocalNameCollision) { ohair@286: this.propertySetters = setByQName; ohair@286: this.propertyGetters = getByQName; ohair@286: elementDeclaredTypes = elementDeclaredTypesByQName; ohair@286: } else { ohair@286: this.propertySetters = setByLocalpart; ohair@286: this.propertyGetters = getByLocalpart; ohair@286: elementDeclaredTypes = elementDeclaredTypesByLocalpart; ohair@286: } ohair@286: } ohair@286: ohair@286: static protected List getAllFields(Class clz) { ohair@286: List list = new ArrayList(); ohair@286: while (!Object.class.equals(clz)) { alanb@368: list.addAll(Arrays.asList(getDeclaredFields(clz))); ohair@286: clz = clz.getSuperclass(); ohair@286: } ohair@286: return list; ohair@286: } ohair@286: ohair@286: static protected Field[] getDeclaredFields(final Class clz) { ohair@286: try { ohair@286: return (System.getSecurityManager() == null) ? clz .getDeclaredFields() : ohair@286: AccessController.doPrivileged(new PrivilegedExceptionAction() { alanb@368: @Override ohair@286: public Field[] run() throws IllegalAccessException { ohair@286: return clz.getDeclaredFields(); ohair@286: } ohair@286: }); ohair@286: } catch (PrivilegedActionException e) { ohair@286: // TODO Auto-generated catch block ohair@286: e.printStackTrace(); ohair@286: return null; ohair@286: } ohair@286: } ohair@286: ohair@286: static protected PropertyGetter createPropertyGetter(Field field, Method getMethod) { ohair@286: if (!field.isAccessible()) { ohair@286: if (getMethod != null) { ohair@286: MethodGetter methodGetter = new MethodGetter(getMethod); alanb@368: if (methodGetter.getType().toString().equals(field.getType().toString())) { ohair@286: return methodGetter; ohair@286: } ohair@286: } ohair@286: } ohair@286: return new FieldGetter(field); ohair@286: } ohair@286: ohair@286: static protected PropertySetter createPropertySetter(Field field, ohair@286: Method setter) { ohair@286: if (!field.isAccessible()) { ohair@286: if (setter != null) { ohair@286: MethodSetter injection = new MethodSetter(setter); alanb@368: if (injection.getType().toString().equals(field.getType().toString())) { ohair@286: return injection; ohair@286: } ohair@286: } ohair@286: } ohair@286: return new FieldSetter(field); ohair@286: } ohair@286: ohair@286: private Class getElementDeclaredType(QName name) { ohair@286: Object key = (this.elementLocalNameCollision) ? name : name ohair@286: .getLocalPart(); ohair@286: return elementDeclaredTypes.get(key); ohair@286: } ohair@286: alanb@368: @Override ohair@286: public PropertyAccessor getPropertyAccessor(String ns, String name) { ohair@286: final QName n = new QName(ns, name); ohair@286: final PropertySetter setter = getPropertySetter(n); ohair@286: final PropertyGetter getter = getPropertyGetter(n); ohair@286: final boolean isJAXBElement = setter.getType() ohair@286: .equals(JAXBElement.class); ohair@286: final boolean isListType = java.util.List.class.isAssignableFrom(setter ohair@286: .getType()); ohair@286: final Class elementDeclaredType = isJAXBElement ? getElementDeclaredType(n) ohair@286: : null; ohair@286: return new PropertyAccessor() { alanb@368: @Override ohair@286: public Object get(Object bean) throws DatabindingException { alanb@368: Object val; ohair@286: if (isJAXBElement) { ohair@286: JAXBElement jaxbElement = (JAXBElement) getter.get(bean); ohair@286: val = (jaxbElement == null) ? null : jaxbElement.getValue(); ohair@286: } else { ohair@286: val = getter.get(bean); ohair@286: } ohair@286: if (val == null && isListType) { ohair@286: val = new java.util.ArrayList(); ohair@286: set(bean, val); ohair@286: } ohair@286: return val; ohair@286: } ohair@286: alanb@368: @Override ohair@286: public void set(Object bean, Object value) throws DatabindingException { ohair@286: if (isJAXBElement) { ohair@286: JAXBElement jaxbElement = new JAXBElement( ohair@286: n, elementDeclaredType, contentClass, value); ohair@286: setter.set(bean, jaxbElement); ohair@286: } else { ohair@286: setter.set(bean, value); ohair@286: } ohair@286: } ohair@286: }; ohair@286: } ohair@286: }