Coverage Report - org.seasar.cubby.plugins.spring.spi.SpringContainerProvider
 
Classes in this File Line Coverage Branch Coverage Complexity
SpringContainerProvider
100%
5/5
N/A
1.75
SpringContainerProvider$SpringContainerImpl
85%
6/7
50%
2/4
1.75
 
 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.spring.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.springframework.beans.factory.BeanFactoryUtils;
 22  
 import org.springframework.context.ApplicationContext;
 23  
 import org.springframework.context.ApplicationContextAware;
 24  
 
 25  
 /**
 26  
  * {@link ApplicationContext} による {@link Container} の実装を提供します。
 27  
  * 
 28  
  * @author suzuki-kei
 29  
  * @author someda
 30  
  */
 31  6
 public class SpringContainerProvider implements ContainerProvider,
 32  
                 ApplicationContextAware {
 33  
 
 34  
         /** コンテナ。 */
 35  
         private Container container;
 36  
 
 37  
         /**
 38  
          * {@inheritDoc}
 39  
          */
 40  
         public Container getContainer() {
 41  1
                 return container;
 42  
         }
 43  
 
 44  
         /**
 45  
          * アプリケーションコンテキストを設定します。
 46  
          * 
 47  
          * @param applicationContext
 48  
          *            アプリケーションコンテキスト
 49  
          */
 50  
         public void setApplicationContext(
 51  
                         final ApplicationContext applicationContext) {
 52  6
                 this.container = new SpringContainerImpl(applicationContext);
 53  6
         }
 54  
 
 55  
         /**
 56  
          * {@link ApplicationContext} による {@link Container} の実装です。
 57  
          */
 58  6
         private static class SpringContainerImpl implements Container {
 59  
 
 60  
                 private final ApplicationContext applicationContext;
 61  
 
 62  6
                 public SpringContainerImpl(final ApplicationContext applicationContext) {
 63  6
                         this.applicationContext = applicationContext;
 64  6
                 }
 65  
 
 66  
                 public <T> T lookup(final Class<T> type) throws LookupException {
 67  1
                         String[] names = BeanFactoryUtils
 68  
                                         .beanNamesForTypeIncludingAncestors(applicationContext,
 69  
                                                         type);
 70  1
                         if (names == null || names.length < 1) {
 71  0
                                 throw new LookupException(type + " component not found.");
 72  
                         }
 73  1
                         return type.cast(applicationContext.getBean(names[0], type));
 74  
                 }
 75  
 
 76  
         }
 77  
 
 78  
 }