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