Coverage Report - org.apache.tapestry.services.impl.EngineFactoryImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
EngineFactoryImpl
0%
0/22
0%
0/6
1.556
EngineFactoryImpl$EngineConstructor
N/A
N/A
1.556
EngineFactoryImpl$ReflectiveEngineConstructor
0%
0/6
N/A
1.556
 
 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 org.apache.hivemind.ApplicationRuntimeException;
 18  
 import org.apache.hivemind.ClassResolver;
 19  
 import org.apache.hivemind.ErrorLog;
 20  
 import org.apache.tapestry.IEngine;
 21  
 import org.apache.tapestry.engine.BaseEngine;
 22  
 import org.apache.tapestry.services.EngineFactory;
 23  
 import org.apache.tapestry.spec.IApplicationSpecification;
 24  
 
 25  
 import java.util.Locale;
 26  
 
 27  
 /**
 28  
  * Standard implementation of {@link org.apache.tapestry.services.EngineFactory} service. This
 29  
  * should do for most purposes, since a major focus of Tapestry 4.0 is to no longer require
 30  
  * subclassing of {@link org.apache.tapestry.engine.BaseEngine}.
 31  
  * 
 32  
  * @author Howard Lewis Ship
 33  
  * @since 4.0
 34  
  */
 35  0
 public class EngineFactoryImpl implements EngineFactory
 36  
 {
 37  
     private IApplicationSpecification _applicationSpecification;
 38  
 
 39  
     private String _defaultEngineClassName;
 40  
 
 41  
     private EngineConstructor _constructor;
 42  
 
 43  
     private ClassResolver _classResolver;
 44  
 
 45  
     private ErrorLog _errorLog;
 46  
 
 47  
     /**
 48  
      * Creates a new engine?
 49  
      * @author Howard Lewis Ship
 50  
      */
 51  
     interface EngineConstructor
 52  
     {
 53  
         IEngine construct();
 54  
     }
 55  
 
 56  
     // TODO: Create a BaseEngineConstructor that is hardcoded to
 57  
     // instantiate a BaseEngine instance, without using reflection
 58  
     // (for efficiency).
 59  
 
 60  
     /**
 61  
      * Creates a new reflective engine constructor.
 62  
      */
 63  0
     static class ReflectiveEngineConstructor implements EngineConstructor
 64  
     {
 65  
         private Class _engineClass;
 66  
 
 67  
         ReflectiveEngineConstructor(Class engineClass)
 68  0
         {
 69  0
             _engineClass = engineClass;
 70  0
         }
 71  
 
 72  
         public IEngine construct()
 73  
         {
 74  
             try
 75  
             {
 76  0
                 return (IEngine) _engineClass.newInstance();
 77  
             }
 78  0
             catch (Exception ex)
 79  
             {
 80  0
                 throw new ApplicationRuntimeException(ImplMessages.errorInstantiatingEngine(_engineClass, ex), ex);
 81  
             }
 82  
         }
 83  
     }
 84  
 
 85  
     public void initializeService()
 86  
     {
 87  0
         String engineClassName = _applicationSpecification.getEngineClassName();
 88  
 
 89  
         // TODO: Check in web.xml first.
 90  
 
 91  0
         if (engineClassName == null)
 92  0
             engineClassName = _defaultEngineClassName;
 93  
 
 94  0
         Class engineClass = _classResolver.checkForClass(engineClassName);
 95  
 
 96  0
         if (engineClass == null)
 97  
         {
 98  0
             _errorLog.error(ImplMessages.engineClassNotFound(engineClassName), null, null);
 99  0
             engineClass = BaseEngine.class;
 100  
         }
 101  
 
 102  0
         _constructor = new ReflectiveEngineConstructor(engineClass);
 103  0
     }
 104  
 
 105  
     public IEngine constructNewEngineInstance(Locale locale)
 106  
     {
 107  0
         IEngine result = _constructor.construct();
 108  
 
 109  0
         result.setLocale(locale);
 110  
 
 111  0
         return result;
 112  
     }
 113  
 
 114  
     public void setApplicationSpecification(IApplicationSpecification specification)
 115  
     {
 116  0
         _applicationSpecification = specification;
 117  0
     }
 118  
 
 119  
     public void setClassResolver(ClassResolver resolver)
 120  
     {
 121  0
         _classResolver = resolver;
 122  0
     }
 123  
 
 124  
     public void setDefaultEngineClassName(String string)
 125  
     {
 126  0
         _defaultEngineClassName = string;
 127  0
     }
 128  
 
 129  
     public void setErrorLog(ErrorLog errorLog)
 130  
     {
 131  0
         _errorLog = errorLog;
 132  0
     }
 133  
 
 134  
 }