01: /*
02: * Copyright 2007 Tim Peierls
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
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: * Only used to satisfy bindings for the two-arg {@code bindConversion}
39: * method of {@link AbstractDwrModule}.
40: */
41: @Inject
42: InternalConverter() {
43: this .type = null;
44: this .provider = null;
45: }
46:
47: /**
48: * Adapts a Provider into a Converter.
49: */
50: InternalConverter(Class<?> type, Provider<Converter> provider) {
51: this .type = type;
52: this .provider = provider;
53: }
54:
55: /* (non-Javadoc)
56: * @see org.directwebremoting.extend.Converter#convertInbound(java.lang.Class, org.directwebremoting.extend.InboundVariable, org.directwebremoting.extend.InboundContext)
57: */
58: public Object convertInbound(Class<?> paramType,
59: InboundVariable data, InboundContext inctx)
60: throws MarshallException {
61: try {
62: return provider.get().convertInbound(
63: type.asSubclass(paramType), data, inctx);
64: } catch (ClassCastException e) {
65: throw new MarshallException(type, e);
66: }
67: }
68:
69: /* (non-Javadoc)
70: * @see org.directwebremoting.extend.Converter#convertOutbound(java.lang.Object, org.directwebremoting.extend.OutboundContext)
71: */
72: public OutboundVariable convertOutbound(Object data,
73: OutboundContext outctx) throws MarshallException {
74: try {
75: return provider.get().convertOutbound(type.cast(data),
76: outctx);
77: } catch (ClassCastException e) {
78: throw new MarshallException(type, e);
79: }
80: }
81:
82: /* (non-Javadoc)
83: * @see org.directwebremoting.convert.BaseV20Converter#setConverterManager(org.directwebremoting.extend.ConverterManager)
84: */
85: @Override
86: public void setConverterManager(ConverterManager mgr) {
87: provider.get().setConverterManager(mgr);
88: }
89:
90: private final Class<?> type;
91:
92: private final Provider<Converter> provider;
93: }
|