1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.directwebremoting.guice;
17
18 import com.google.inject.Inject;
19 import com.google.inject.Provider;
20
21 import org.directwebremoting.convert.BaseV20Converter;
22 import org.directwebremoting.extend.Converter;
23 import org.directwebremoting.extend.ConverterManager;
24 import org.directwebremoting.extend.InboundContext;
25 import org.directwebremoting.extend.InboundVariable;
26 import org.directwebremoting.extend.MarshallException;
27 import org.directwebremoting.extend.OutboundContext;
28 import org.directwebremoting.extend.OutboundVariable;
29
30 /**
31 * Specialized converter implementation that uses a Provider to
32 * look up instances to delegate to. This class is used by
33 * {@link InternalConverterManager}.
34 * @author Tim Peierls [tim at peierls dot net]
35 */
36 class InternalConverter extends BaseV20Converter implements Converter
37 {
38 /**
39 * Only used to satisfy bindings for the two-arg {@code bindConversion}
40 * method of {@link AbstractDwrModule}.
41 */
42 @Inject InternalConverter()
43 {
44 this.type = null;
45 this.provider = null;
46 }
47
48 /**
49 * Adapts a Provider into a Converter.
50 */
51 InternalConverter(Class<?> type, Provider<Converter> provider)
52 {
53 this.type = type;
54 this.provider = provider;
55 }
56
57 public Object convertInbound(Class paramType, InboundVariable data, InboundContext inctx)
58 throws MarshallException
59 {
60 try
61 {
62 return provider.get().convertInbound(type.asSubclass(paramType), data, inctx);
63 }
64 catch (ClassCastException e)
65 {
66 throw new MarshallException(type, e);
67 }
68 }
69
70 public OutboundVariable convertOutbound(Object data, OutboundContext outctx)
71 throws MarshallException
72 {
73 try
74 {
75 return provider.get().convertOutbound(type.cast(data), outctx);
76 }
77 catch (ClassCastException e)
78 {
79 throw new MarshallException(type, e);
80 }
81 }
82
83 public void setConverterManager(ConverterManager mgr)
84 {
85 provider.get().setConverterManager(mgr);
86 }
87
88 private final Class<?> type;
89 private final Provider<Converter> provider;
90 }