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