Coverage Report - org.apache.tapestry5.internal.services.AliasImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
AliasImpl
100%
19/19
100%
4/4
0
 
 1  
 // Copyright 2006, 2007 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.tapestry5.internal.services;
 16  
 
 17  
 import org.apache.tapestry5.ioc.AnnotationProvider;
 18  
 import org.apache.tapestry5.ioc.ObjectLocator;
 19  
 import org.apache.tapestry5.ioc.ObjectProvider;
 20  
 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
 21  
 import org.apache.tapestry5.services.Alias;
 22  
 import org.apache.tapestry5.services.AliasManager;
 23  
 
 24  
 import java.util.Map;
 25  
 
 26  
 public class AliasImpl implements Alias, ObjectProvider
 27  
 {
 28  
     // Derived from the managers when first needed
 29  
 
 30  66
     private final Map<Class, Object> properties = CollectionFactory.newMap();
 31  
 
 32  
     private final String mode;
 33  
 
 34  66
     private boolean initialized = false;
 35  
 
 36  
     private AliasManager masterManager;
 37  
 
 38  
     private AliasManager overridesManager;
 39  
 
 40  
     public AliasImpl(AliasManager masterManager, String mode, AliasManager overridesManager)
 41  66
     {
 42  66
         this.masterManager = masterManager;
 43  66
         this.mode = mode;
 44  66
         this.overridesManager = overridesManager;
 45  66
     }
 46  
 
 47  
     public ObjectProvider getObjectProvider()
 48  
     {
 49  21084
         return this;
 50  
     }
 51  
 
 52  
     private synchronized void initialize()
 53  
     {
 54  21086
         if (initialized) return;
 55  
 
 56  66
         properties.putAll(masterManager.getAliasesForMode(mode));
 57  66
         properties.putAll(overridesManager.getAliasesForMode(mode));
 58  
 
 59  66
         masterManager = null;
 60  66
         overridesManager = null;
 61  
 
 62  66
         initialized = true;
 63  66
     }
 64  
 
 65  
     public <T> T provide(Class<T> objectType, AnnotationProvider annotationProvider,
 66  
                          ObjectLocator locator)
 67  
     {
 68  21086
         initialize();
 69  
 
 70  21086
         Object object = properties.get(objectType);
 71  
 
 72  
         // Let another provider handle this (probably the default object provider)
 73  21086
         if (object == null) return null;
 74  
 
 75  
         // A ClassCastException should never occur.
 76  
 
 77  728
         return objectType.cast(object);
 78  
     }
 79  
 }