001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.bind.v2.runtime.reflect;
027:
028: import javax.xml.bind.annotation.adapters.XmlAdapter;
029:
030: import com.sun.xml.internal.bind.api.AccessorException;
031: import com.sun.xml.internal.bind.v2.ClassFactory;
032: import com.sun.xml.internal.bind.v2.model.core.Adapter;
033: import com.sun.xml.internal.bind.v2.runtime.Coordinator;
034:
035: /**
036: * {@link Accessor} that adapts the value by using {@link Adapter}.
037: *
038: * @see Accessor#adapt
039: * @author Kohsuke Kawaguchi
040: */
041: final class AdaptedAccessor<BeanT, InMemValueT, OnWireValueT> extends
042: Accessor<BeanT, OnWireValueT> {
043: private final Accessor<BeanT, InMemValueT> core;
044: private final Class<? extends XmlAdapter<OnWireValueT, InMemValueT>> adapter;
045:
046: /*pacakge*/AdaptedAccessor(
047: Class<OnWireValueT> targetType,
048: Accessor<BeanT, InMemValueT> extThis,
049: Class<? extends XmlAdapter<OnWireValueT, InMemValueT>> adapter) {
050: super (targetType);
051: this .core = extThis;
052: this .adapter = adapter;
053: }
054:
055: public OnWireValueT get(BeanT bean) throws AccessorException {
056: InMemValueT v = core.get(bean);
057:
058: XmlAdapter<OnWireValueT, InMemValueT> a = getAdapter();
059: try {
060: return a.marshal(v);
061: } catch (Exception e) {
062: throw new AccessorException(e);
063: }
064: }
065:
066: public void set(BeanT bean, OnWireValueT o)
067: throws AccessorException {
068: XmlAdapter<OnWireValueT, InMemValueT> a = getAdapter();
069: try {
070: core.set(bean, a.unmarshal(o));
071: } catch (Exception e) {
072: throw new AccessorException(e);
073: }
074: }
075:
076: public Object getUnadapted(BeanT bean) throws AccessorException {
077: return core.getUnadapted(bean);
078: }
079:
080: public void setUnadapted(BeanT bean, Object value)
081: throws AccessorException {
082: core.setUnadapted(bean, value);
083: }
084:
085: /**
086: * Sometimes Adapters are used directly by JAX-WS outside any
087: * {@link Coordinator}. Use this lazily-created cached
088: * {@link XmlAdapter} in such cases.
089: */
090: private XmlAdapter<OnWireValueT, InMemValueT> staticAdapter;
091:
092: private XmlAdapter<OnWireValueT, InMemValueT> getAdapter() {
093: Coordinator coordinator = Coordinator._getInstance();
094: if (coordinator != null)
095: return coordinator.getAdapter(adapter);
096: else {
097: synchronized (this) {
098: if (staticAdapter == null)
099: staticAdapter = ClassFactory.create(adapter);
100: }
101: return staticAdapter;
102: }
103: }
104: }
|