Coverage Report - org.apache.tapestry.services.impl.ComponentConstructorFactoryImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ComponentConstructorFactoryImpl
0% 
0% 
1.444
 
 1  
 // Copyright 2004, 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 edu.emory.mathcs.backport.java.util.concurrent.locks.ReentrantLock;
 18  
 
 19  
 import java.util.Collections;
 20  
 import java.util.HashMap;
 21  
 import java.util.Map;
 22  
 
 23  
 import org.apache.commons.logging.Log;
 24  
 import org.apache.hivemind.ApplicationRuntimeException;
 25  
 import org.apache.hivemind.ClassResolver;
 26  
 import org.apache.hivemind.service.ClassFactory;
 27  
 import org.apache.hivemind.util.Defense;
 28  
 import org.apache.tapestry.enhance.EnhancedClassValidator;
 29  
 import org.apache.tapestry.enhance.EnhancementOperationImpl;
 30  
 import org.apache.tapestry.enhance.EnhancementWorker;
 31  
 import org.apache.tapestry.event.ReportStatusEvent;
 32  
 import org.apache.tapestry.event.ReportStatusListener;
 33  
 import org.apache.tapestry.event.ResetEventListener;
 34  
 import org.apache.tapestry.services.ComponentConstructor;
 35  
 import org.apache.tapestry.services.ComponentConstructorFactory;
 36  
 import org.apache.tapestry.spec.IComponentSpecification;
 37  
 
 38  
 /**
 39  
  * Implementation of the {@link org.apache.tapestry.services.ComponentConstructorFactory} service
 40  
  * interface.
 41  
  * 
 42  
  * @author Howard M. Lewis Ship
 43  
  * @since 4.0
 44  
  */
 45  0
 public class ComponentConstructorFactoryImpl implements ComponentConstructorFactory,
 46  
         ResetEventListener, ReportStatusListener
 47  
 {
 48  0
     private final ReentrantLock _lock = new ReentrantLock();
 49  
     
 50  
     private String _serviceId;
 51  
 
 52  
     private Log _log;
 53  
 
 54  
     private ClassFactory _classFactory;
 55  
 
 56  
     private ClassResolver _classResolver;
 57  
 
 58  
     private EnhancedClassValidator _validator;
 59  
 
 60  
     private EnhancementWorker _chain;
 61  
 
 62  
     /**
 63  
      * Map of {@link org.apache.tapestry.services.ComponentConstructor} keyed on
 64  
      * {@link org.apache.tapestry.spec.IComponentSpecification}.
 65  
      */
 66  
 
 67  0
     private Map _cachedConstructors = Collections.synchronizedMap(new HashMap());
 68  
 
 69  
     public void resetEventDidOccur()
 70  
     {
 71  0
         _cachedConstructors.clear();
 72  0
     }
 73  
 
 74  
     public synchronized void reportStatus(ReportStatusEvent event)
 75  
     {
 76  0
         event.title(_serviceId);
 77  
         
 78  0
         event.property("enhanced class count", _cachedConstructors.size());
 79  0
         event.collection("enhanced classes", _cachedConstructors.keySet());
 80  0
     }
 81  
 
 82  
     public ComponentConstructor getComponentConstructor(IComponentSpecification specification,
 83  
             String className)
 84  
     {
 85  0
         Defense.notNull(specification, "specification");
 86  
         
 87  
         try {
 88  
             
 89  0
             _lock.lockInterruptibly();
 90  
             
 91  0
             ComponentConstructor result = (ComponentConstructor) _cachedConstructors.get(specification);
 92  
             
 93  0
             if (result == null) {
 94  
                 
 95  0
                 Class baseClass = _classResolver.findClass(className);
 96  
                 
 97  0
                 EnhancementOperationImpl eo = new EnhancementOperationImpl(_classResolver,
 98  
                         specification, baseClass, _classFactory, _log);
 99  
                 
 100  
                 // Invoking on the chain is the same as invoking on every
 101  
                 // object in the chain (because method performEnhancement() is type void).
 102  
                 
 103  0
                 _chain.performEnhancement(eo, specification);
 104  
                 
 105  0
                 result = eo.getConstructor();
 106  
                 
 107  
                 // TODO: This should be optional to work around that IBM JVM bug.
 108  
                 
 109  0
                 _validator.validate(baseClass, result.getComponentClass(), specification);
 110  
                 
 111  0
                 _cachedConstructors.put(specification, result);
 112  
             }
 113  
             
 114  0
             return result;
 115  
             
 116  0
         } catch (InterruptedException e) {
 117  
             
 118  0
            throw new ApplicationRuntimeException(e);
 119  
         } finally {
 120  
             
 121  0
             _lock.unlock();
 122  
         }
 123  
     }
 124  
     
 125  
     public void setClassFactory(ClassFactory classFactory)
 126  
     {
 127  0
         _classFactory = classFactory;
 128  0
     }
 129  
 
 130  
     public void setClassResolver(ClassResolver classResolver)
 131  
     {
 132  0
         _classResolver = classResolver;
 133  0
     }
 134  
 
 135  
     public void setValidator(EnhancedClassValidator validator)
 136  
     {
 137  0
         _validator = validator;
 138  0
     }
 139  
 
 140  
     public void setChain(EnhancementWorker chain)
 141  
     {
 142  0
         _chain = chain;
 143  0
     }
 144  
 
 145  
     public void setLog(Log log)
 146  
     {
 147  0
         _log = log;
 148  0
     }
 149  
 
 150  
     public void setServiceId(String serviceId)
 151  
     {
 152  0
         _serviceId = serviceId;
 153  0
     }
 154  
 }