View Javadoc

1   /*
2    * Copyright 2004-2009 the Seasar Foundation and the Others.
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,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.plugins.guice.spi;
17  
18  import org.seasar.cubby.spi.ContainerProvider;
19  import org.seasar.cubby.spi.container.Container;
20  import org.seasar.cubby.spi.container.LookupException;
21  
22  import com.google.inject.ConfigurationException;
23  import com.google.inject.Inject;
24  import com.google.inject.Injector;
25  
26  /**
27   * Guice の {@link Injector} による {@link Container} の実装を提供します。
28   * 
29   * @author baba
30   */
31  public class GuiceContainerProvider implements ContainerProvider {
32  
33  	/** {@link Container} */
34  	private final Container container;
35  
36  	/**
37  	 * インスタンス化します。
38  	 */
39  	@Inject
40  	public GuiceContainerProvider(final Injector injector) {
41  		this.container = new GuiceContainerImpl(injector);
42  	}
43  
44  	/**
45  	 * {@inheritDoc}
46  	 */
47  	public Container getContainer() {
48  		return container;
49  	}
50  
51  	/**
52  	 * {@link Injector} による {@link Container} の実装です。
53  	 * 
54  	 * @author baba
55  	 * @since 2.0.0
56  	 */
57  	private static class GuiceContainerImpl implements Container {
58  
59  		/** インスタンスをルックアップするための {@link Injector} */
60  		private final Injector injector;
61  
62  		/**
63  		 * インスタンス化します。
64  		 * 
65  		 * @param injector
66  		 *            インスタンスをルックアップするための {@link Injector}
67  		 */
68  		public GuiceContainerImpl(final Injector injector) {
69  			this.injector = injector;
70  		}
71  
72  		/**
73  		 * {@inheritDoc}
74  		 */
75  		public <T> T lookup(final Class<T> type) throws LookupException {
76  			try {
77  				return injector.getInstance(type);
78  			} catch (final ConfigurationException e) {
79  				throw new LookupException(e);
80  			}
81  		}
82  
83  	}
84  
85  }