View Javadoc

1   /*
2    * Copyright 2007 Tim Peierls
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
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      /**
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  }