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.Key;
19  import com.google.inject.Injector;
20  
21  import java.util.Map;
22  
23  import org.directwebremoting.extend.Creator;
24  
25  /**
26   * Specialized creator implementation that uses an injector to
27   * look up its instances. This class is used by {@link InternalCreatorManager}.
28   * @author Tim Peierls [tim at peierls dot net]
29   */
30  class InternalCreator implements Creator
31  {
32      InternalCreator(Injector injector, Key key, String scriptName)
33      {
34          this.injector = injector;
35          this.key = key;
36          this.scriptName = scriptName;
37      }
38  
39      public void setProperties(Map params) throws IllegalArgumentException
40      {
41          // Do nothing, we ignore properties (since we inject everything we create).
42      }
43  
44      public Class getType()
45      {
46          return (Class) key.getTypeLiteral().getType();
47      }
48      
49      public Object getInstance() throws InstantiationException
50      {
51          return injector.getInstance(key);
52      }
53  
54      public String getScope()
55      {
56          return Creator.PAGE; // i.e., tell DWR always to create
57      }
58  
59      public boolean isCacheable()
60      {
61          return true;
62      }
63  
64      public String getJavascript()
65      {
66          return scriptName;
67      }
68      
69      private final Injector injector;
70      private final Key key;
71      private final String scriptName;
72  }