Coverage Report - org.apache.tapestry.services.impl.ComponentPropertySourceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ComponentPropertySourceImpl
0% 
0% 
1.308
 
 1  
 // Copyright 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.services.impl;
 16  
 
 17  
 import org.apache.hivemind.Resource;
 18  
 import org.apache.hivemind.lib.chain.ChainBuilder;
 19  
 import org.apache.tapestry.IComponent;
 20  
 import org.apache.tapestry.INamespace;
 21  
 import org.apache.tapestry.engine.IPropertySource;
 22  
 import org.apache.tapestry.event.ResetEventListener;
 23  
 import org.apache.tapestry.services.ComponentPropertySource;
 24  
 import org.apache.tapestry.spec.IComponentSpecification;
 25  
 import org.apache.tapestry.util.PropertyHolderPropertySource;
 26  
 
 27  
 import java.util.*;
 28  
 
 29  
 /**
 30  
  * Implementation of tapestry.props.ComponentPropertySource.
 31  
  * <p>
 32  
  * TODO: Figure out a testing strategy for this beast!
 33  
  * 
 34  
  * @author Howard M. Lewis Ship
 35  
  * @since 4.0
 36  
  */
 37  0
 public class ComponentPropertySourceImpl implements ComponentPropertySource, ResetEventListener
 38  
 {
 39  
     private IPropertySource _globalProperties;
 40  
 
 41  
     private ChainBuilder _chainBuilder;
 42  
 
 43  0
     private Map _componentSources = new HashMap();
 44  
 
 45  0
     private Map _localizedComponentSources = new HashMap();
 46  
 
 47  0
     private Map _namespaceSources = new HashMap();
 48  
 
 49  0
     private Map _localizedNamespaceSources = new HashMap();
 50  
 
 51  
     public synchronized void resetEventDidOccur()
 52  
     {
 53  0
         _componentSources.clear();
 54  0
         _localizedComponentSources.clear();
 55  0
         _namespaceSources.clear();
 56  0
         _localizedNamespaceSources.clear();
 57  0
     }
 58  
 
 59  
     private synchronized IPropertySource getSourceForNamespace(INamespace namespace)
 60  
     {
 61  0
         Resource key = namespace.getSpecificationLocation();
 62  
 
 63  0
         IPropertySource result = (IPropertySource) _namespaceSources.get(key);
 64  
 
 65  0
         if (result == null)
 66  
         {
 67  0
             result = createSourceForNamespace(namespace);
 68  0
             _namespaceSources.put(key, result);
 69  
         }
 70  
 
 71  0
         return result;
 72  
     }
 73  
 
 74  
     private synchronized IPropertySource getSourceForComponent(IComponent component)
 75  
     {
 76  0
         Resource key = component.getSpecification().getSpecificationLocation();
 77  
 
 78  0
         IPropertySource result = (IPropertySource) _componentSources.get(key);
 79  
 
 80  0
         if (result == null)
 81  
         {
 82  0
             result = createSourceForComponent(component);
 83  0
             _componentSources.put(key, result);
 84  
         }
 85  
 
 86  0
         return result;
 87  
     }
 88  
 
 89  
     private synchronized LocalizedPropertySource getLocalizedSourceForComponent(IComponent component)
 90  
     {
 91  0
         Resource key = component.getSpecification().getSpecificationLocation();
 92  
 
 93  0
         LocalizedPropertySource result = (LocalizedPropertySource) _localizedComponentSources.get(key);
 94  
 
 95  0
         if (result == null)
 96  
         {
 97  0
             result = new LocalizedPropertySource(getSourceForComponent(component));
 98  
 
 99  0
             _localizedComponentSources.put(key, result);
 100  
         }
 101  
 
 102  0
         return result;
 103  
     }
 104  
 
 105  
     private synchronized LocalizedPropertySource getLocalizedSourceForNamespace(INamespace namespace)
 106  
     {
 107  0
         Resource key = namespace.getSpecificationLocation();
 108  
 
 109  0
         LocalizedPropertySource result = (LocalizedPropertySource) _localizedNamespaceSources.get(key);
 110  
 
 111  0
         if (result == null)
 112  
         {
 113  0
             result = new LocalizedPropertySource(getSourceForNamespace(namespace));
 114  
 
 115  0
             _localizedNamespaceSources.put(key, result);
 116  
         }
 117  
 
 118  0
         return result;
 119  
     }
 120  
 
 121  
     private IPropertySource createSourceForComponent(IComponent component)
 122  
     {
 123  0
         IComponentSpecification specification = component.getSpecification();
 124  
 
 125  0
         List sources = new ArrayList();
 126  
 
 127  0
         sources.add(new PropertyHolderPropertySource(specification));
 128  0
         sources.add(getSourceForNamespace(component.getNamespace()));
 129  
 
 130  0
         return (IPropertySource) _chainBuilder.buildImplementation(
 131  0
                 IPropertySource.class,
 132  
                 sources,
 133  
                 ImplMessages.componentPropertySourceDescription(specification));
 134  
     }
 135  
 
 136  
     private IPropertySource createSourceForNamespace(INamespace namespace)
 137  
     {
 138  0
         List sources = new ArrayList();
 139  
 
 140  0
         sources.add(new PropertyHolderPropertySource(namespace.getSpecification()));
 141  0
         sources.add(_globalProperties);
 142  
 
 143  0
         return (IPropertySource) _chainBuilder.buildImplementation(
 144  
                 IPropertySource.class,
 145  
                 sources,
 146  
                 ImplMessages.namespacePropertySourceDescription(namespace));
 147  
     }
 148  
 
 149  
     public String getComponentProperty(IComponent component, String propertyName)
 150  
     {
 151  0
         return getSourceForComponent(component).getPropertyValue(propertyName);
 152  
     }
 153  
 
 154  
     public String getLocalizedComponentProperty(IComponent component, Locale locale,
 155  
             String propertyName)
 156  
     {
 157  0
         return getLocalizedSourceForComponent(component).getPropertyValue(propertyName, locale);
 158  
     }
 159  
 
 160  
     public String getNamespaceProperty(INamespace namespace, String propertyName)
 161  
     {
 162  0
         return getSourceForNamespace(namespace).getPropertyValue(propertyName);
 163  
     }
 164  
 
 165  
     public String getLocalizedNamespaceProperty(INamespace namespace, Locale locale,
 166  
             String propertyName)
 167  
     {
 168  0
         return getLocalizedSourceForNamespace(namespace).getPropertyValue(propertyName, locale);
 169  
     }
 170  
 
 171  
     public void setChainBuilder(ChainBuilder chainBuilder)
 172  
     {
 173  0
         _chainBuilder = chainBuilder;
 174  0
     }
 175  
 
 176  
     public void setGlobalProperties(IPropertySource globalProperties)
 177  
     {
 178  0
         _globalProperties = globalProperties;
 179  0
     }
 180  
 }