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  	 * @param injector
    40  	 *            インジェクタ
    41  	 */
    42  	@Inject
    43  	public GuiceContainerProvider(final Injector injector) {
    44  		this.container = new GuiceContainerImpl(injector);
    45  	}
    46
    47  	/**
    48  	 * {@inheritDoc}
    49  	 */
    50  	public Container getContainer() {
    51  		return container;
    52  	}
    53
    54  	/**
    55  	 * {@link Injector} による {@link Container} の実装です。
    56  	 *
    57  	 * @author baba
    58  	 */
    59  	private static class GuiceContainerImpl implements Container {
    60
    61  		/** インスタンスをルックアップするための {@link Injector} */
    62  		private final Injector injector;
    63
    64  		/**
    65  		 * インスタンス化します。
    66  		 *
    67  		 * @param injector
    68  		 *            インスタンスをルックアップするための {@link Injector}
    69  		 */
    70  		public GuiceContainerImpl(final Injector injector) {
    71  			this.injector = injector;
    72  		}
    73
    74  		/**
    75  		 * {@inheritDoc}
    76  		 */
    77  		public <T> T lookup(final Class<T> type) throws LookupException {
    78  			try {
    79  				return injector.getInstance(type);
    80  			} catch (final ConfigurationException e) {
    81  				throw new LookupException(e);
    82  			}
    83  		}
    84
    85  	}
    86
    87  }