1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/reflect/AdaptedAccessor.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,103 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.bind.v2.runtime.reflect; 1.30 + 1.31 +import javax.xml.bind.annotation.adapters.XmlAdapter; 1.32 + 1.33 +import com.sun.xml.internal.bind.api.AccessorException; 1.34 +import com.sun.xml.internal.bind.v2.ClassFactory; 1.35 +import com.sun.xml.internal.bind.v2.model.core.Adapter; 1.36 +import com.sun.xml.internal.bind.v2.runtime.Coordinator; 1.37 + 1.38 +/** 1.39 + * {@link Accessor} that adapts the value by using {@link Adapter}. 1.40 + * 1.41 + * @see Accessor#adapt 1.42 + * @author Kohsuke Kawaguchi 1.43 + */ 1.44 +final class AdaptedAccessor<BeanT,InMemValueT,OnWireValueT> extends Accessor<BeanT,OnWireValueT> { 1.45 + private final Accessor<BeanT,InMemValueT> core; 1.46 + private final Class<? extends XmlAdapter<OnWireValueT,InMemValueT>> adapter; 1.47 + 1.48 + /*pacakge*/ AdaptedAccessor(Class<OnWireValueT> targetType, Accessor<BeanT, InMemValueT> extThis, Class<? extends XmlAdapter<OnWireValueT, InMemValueT>> adapter) { 1.49 + super(targetType); 1.50 + this.core = extThis; 1.51 + this.adapter = adapter; 1.52 + } 1.53 + 1.54 + @Override 1.55 + public boolean isAdapted() { 1.56 + return true; 1.57 + } 1.58 + 1.59 + public OnWireValueT get(BeanT bean) throws AccessorException { 1.60 + InMemValueT v = core.get(bean); 1.61 + 1.62 + XmlAdapter<OnWireValueT,InMemValueT> a = getAdapter(); 1.63 + try { 1.64 + return a.marshal(v); 1.65 + } catch (Exception e) { 1.66 + throw new AccessorException(e); 1.67 + } 1.68 + } 1.69 + 1.70 + public void set(BeanT bean, OnWireValueT o) throws AccessorException { 1.71 + XmlAdapter<OnWireValueT, InMemValueT> a = getAdapter(); 1.72 + try { 1.73 + core.set(bean, (o == null ? null : a.unmarshal(o))); 1.74 + } catch (Exception e) { 1.75 + throw new AccessorException(e); 1.76 + } 1.77 + } 1.78 + 1.79 + public Object getUnadapted(BeanT bean) throws AccessorException { 1.80 + return core.getUnadapted(bean); 1.81 + } 1.82 + 1.83 + public void setUnadapted(BeanT bean, Object value) throws AccessorException { 1.84 + core.setUnadapted(bean,value); 1.85 + } 1.86 + 1.87 + /** 1.88 + * Sometimes Adapters are used directly by JAX-WS outside any 1.89 + * {@link Coordinator}. Use this lazily-created cached 1.90 + * {@link XmlAdapter} in such cases. 1.91 + */ 1.92 + private XmlAdapter<OnWireValueT, InMemValueT> staticAdapter; 1.93 + 1.94 + private XmlAdapter<OnWireValueT, InMemValueT> getAdapter() { 1.95 + Coordinator coordinator = Coordinator._getInstance(); 1.96 + if(coordinator!=null) 1.97 + return coordinator.getAdapter(adapter); 1.98 + else { 1.99 + synchronized(this) { 1.100 + if(staticAdapter==null) 1.101 + staticAdapter = ClassFactory.create(adapter); 1.102 + } 1.103 + return staticAdapter; 1.104 + } 1.105 + } 1.106 +}