|
1 /* |
|
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.xml.internal.bind.v2.runtime.reflect; |
|
27 |
|
28 import javax.xml.bind.annotation.adapters.XmlAdapter; |
|
29 |
|
30 import com.sun.xml.internal.bind.api.AccessorException; |
|
31 import com.sun.xml.internal.bind.v2.ClassFactory; |
|
32 import com.sun.xml.internal.bind.v2.model.core.Adapter; |
|
33 import com.sun.xml.internal.bind.v2.runtime.Coordinator; |
|
34 |
|
35 /** |
|
36 * {@link Accessor} that adapts the value by using {@link Adapter}. |
|
37 * |
|
38 * @see Accessor#adapt |
|
39 * @author Kohsuke Kawaguchi |
|
40 */ |
|
41 final class AdaptedAccessor<BeanT,InMemValueT,OnWireValueT> extends Accessor<BeanT,OnWireValueT> { |
|
42 private final Accessor<BeanT,InMemValueT> core; |
|
43 private final Class<? extends XmlAdapter<OnWireValueT,InMemValueT>> adapter; |
|
44 |
|
45 /*pacakge*/ AdaptedAccessor(Class<OnWireValueT> targetType, Accessor<BeanT, InMemValueT> extThis, Class<? extends XmlAdapter<OnWireValueT, InMemValueT>> adapter) { |
|
46 super(targetType); |
|
47 this.core = extThis; |
|
48 this.adapter = adapter; |
|
49 } |
|
50 |
|
51 @Override |
|
52 public boolean isAdapted() { |
|
53 return true; |
|
54 } |
|
55 |
|
56 public OnWireValueT get(BeanT bean) throws AccessorException { |
|
57 InMemValueT v = core.get(bean); |
|
58 |
|
59 XmlAdapter<OnWireValueT,InMemValueT> a = getAdapter(); |
|
60 try { |
|
61 return a.marshal(v); |
|
62 } catch (Exception e) { |
|
63 throw new AccessorException(e); |
|
64 } |
|
65 } |
|
66 |
|
67 public void set(BeanT bean, OnWireValueT o) throws AccessorException { |
|
68 XmlAdapter<OnWireValueT, InMemValueT> a = getAdapter(); |
|
69 try { |
|
70 core.set(bean, (o == null ? null : a.unmarshal(o))); |
|
71 } catch (Exception e) { |
|
72 throw new AccessorException(e); |
|
73 } |
|
74 } |
|
75 |
|
76 public Object getUnadapted(BeanT bean) throws AccessorException { |
|
77 return core.getUnadapted(bean); |
|
78 } |
|
79 |
|
80 public void setUnadapted(BeanT bean, Object value) throws AccessorException { |
|
81 core.setUnadapted(bean,value); |
|
82 } |
|
83 |
|
84 /** |
|
85 * Sometimes Adapters are used directly by JAX-WS outside any |
|
86 * {@link Coordinator}. Use this lazily-created cached |
|
87 * {@link XmlAdapter} in such cases. |
|
88 */ |
|
89 private XmlAdapter<OnWireValueT, InMemValueT> staticAdapter; |
|
90 |
|
91 private XmlAdapter<OnWireValueT, InMemValueT> getAdapter() { |
|
92 Coordinator coordinator = Coordinator._getInstance(); |
|
93 if(coordinator!=null) |
|
94 return coordinator.getAdapter(adapter); |
|
95 else { |
|
96 synchronized(this) { |
|
97 if(staticAdapter==null) |
|
98 staticAdapter = ClassFactory.create(adapter); |
|
99 } |
|
100 return staticAdapter; |
|
101 } |
|
102 } |
|
103 } |