View Javadoc

1   /*
2    * Copyright 2004-2010 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  
17  package org.seasar.cubby.plugins.s2.spi;
18  
19  import org.seasar.cubby.spi.ContainerProvider;
20  import org.seasar.cubby.spi.container.Container;
21  import org.seasar.cubby.spi.container.LookupException;
22  import org.seasar.framework.container.ComponentNotFoundRuntimeException;
23  import org.seasar.framework.container.CyclicReferenceRuntimeException;
24  import org.seasar.framework.container.S2Container;
25  import org.seasar.framework.container.TooManyRegistrationRuntimeException;
26  
27  /**
28   * {@link S2Container} による {@link Container} の実装を提供します。
29   * 
30   * @author baba
31   */
32  public class S2ContainerProvider implements ContainerProvider {
33  
34  	public static final String s2Container_BINDING = "bindingType=must";
35  
36  	/** {@link Container} */
37  	private final Container container = new S2ContainerImpl();
38  
39  	/** S2 コンテナ。 */
40  	private S2Container s2Container;
41  
42  	/**
43  	 * S2 コンテナを設定します。
44  	 * 
45  	 * @param s2Container
46  	 *            S2 コンテナ
47  	 */
48  	public void setS2Container(final S2Container s2Container) {
49  		this.s2Container = s2Container;
50  	}
51  
52  	/**
53  	 * {@inheritDoc}
54  	 */
55  	public Container getContainer() {
56  		return container;
57  	}
58  
59  	/**
60  	 * {@link S2Container} による {@link Container} の実装です。
61  	 * 
62  	 * @author baba
63  	 */
64  	private class S2ContainerImpl implements Container {
65  
66  		/**
67  		 * {@inheritDoc}
68  		 * 
69  		 * @throws LookupException
70  		 */
71  		public <T> T lookup(final Class<T> type) throws LookupException {
72  			try {
73  				final T component = type.cast(s2Container.getRoot()
74  						.getComponent(type));
75  				if (component == null) {
76  					throw new LookupException();
77  				}
78  				return component;
79  			} catch (final ComponentNotFoundRuntimeException e) {
80  				throw new LookupException(e);
81  			} catch (final TooManyRegistrationRuntimeException e) {
82  				throw new LookupException(e);
83  			} catch (final CyclicReferenceRuntimeException e) {
84  				throw new LookupException(e);
85  			}
86  		}
87  
88  	}
89  
90  }