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