Coverage Report - org.seasar.teeda.core.application.impl.DefaultComponentLookupStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultComponentLookupStrategy
87%
20/23
67%
8/12
3
 
 1  
 /*
 2  
  * Copyright 2004-2011 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.teeda.core.application.impl;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Iterator;
 20  
 import java.util.List;
 21  
 
 22  
 import org.seasar.framework.util.StringUtil;
 23  
 import org.seasar.teeda.core.JsfConstants;
 24  
 import org.seasar.teeda.core.application.ComponentLookupStrategy;
 25  
 import org.seasar.teeda.core.util.DIContainerUtil;
 26  
 
 27  
 /**
 28  
  * @author shot
 29  
  */
 30  
 public class DefaultComponentLookupStrategy implements ComponentLookupStrategy {
 31  
 
 32  54
     private List namespaces = new ArrayList();
 33  
 
 34  54
     public DefaultComponentLookupStrategy() {
 35  54
         namespaces.add(JsfConstants.TEEDA_NAMESPACE);
 36  54
     }
 37  
 
 38  
     public Object getComponentByName(String componentName) {
 39  21
         if (StringUtil.isEmpty(componentName)) {
 40  0
             throw new IllegalArgumentException();
 41  
         }
 42  21
         Object component = DIContainerUtil
 43  
                 .getComponentNoException(componentName);
 44  21
         if (component != null) {
 45  9
             return component;
 46  
         }
 47  12
         for (Iterator itr = namespaces.iterator(); itr.hasNext();) {
 48  12
             String namespace = (String) itr.next();
 49  12
             component = getComponentByDefaultNamespace(namespace, componentName);
 50  12
             if (component != null) {
 51  0
                 return component;
 52  
             }
 53  
         }
 54  12
         return null;
 55  
     }
 56  
 
 57  
     public Object getComponentByClass(Class componentClazz) {
 58  2
         if (componentClazz == null) {
 59  0
             throw new IllegalArgumentException();
 60  
         }
 61  2
         return DIContainerUtil.getComponentNoException(componentClazz);
 62  
     }
 63  
 
 64  
     public void addNamespace(String namespace) {
 65  1
         if (!StringUtil.isEmpty(namespace)) {
 66  1
             this.namespaces.add(namespace);
 67  
         }
 68  1
     }
 69  
 
 70  
     private Object getComponentByDefaultNamespace(String namespace,
 71  
             String componentName) {
 72  12
         final String key = new String(new StringBuffer(namespace.length()
 73  
                 + componentName.length() + 1).append(namespace).append(
 74  
                 JsfConstants.NS_SEP).append(componentName));
 75  12
         return DIContainerUtil.getComponentNoException(key);
 76  
     }
 77  
 }