Clover coverage report - Code Coverage for tapestry release 3.1-alpha-1
Coverage timestamp: Mon Feb 21 2005 09:16:14 EST
file stats: LOC: 200   Methods: 11
NCLOC: 122   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
ExtensionSpecification.java 50% 65.1% 81.8% 66.1%
coverage coverage
 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.spec;
 16   
 
 17   
 import java.util.Collections;
 18   
 import java.util.HashMap;
 19   
 import java.util.Iterator;
 20   
 import java.util.Map;
 21   
 
 22   
 import org.apache.commons.logging.Log;
 23   
 import org.apache.commons.logging.LogFactory;
 24   
 import org.apache.hivemind.ApplicationRuntimeException;
 25   
 import org.apache.hivemind.ClassResolver;
 26   
 import org.apache.hivemind.util.PropertyUtils;
 27   
 import org.apache.tapestry.Tapestry;
 28   
 import org.apache.tapestry.coerce.ValueConverter;
 29   
 import org.apache.tapestry.services.ExpressionEvaluator;
 30   
 
 31   
 /**
 32   
  * Defines an "extension", which is much like a helper bean, but is part of a library or application
 33   
  * specification (and has the same lifecycle as the application).
 34   
  * 
 35   
  * @author Howard Lewis Ship
 36   
  * @since 2.2
 37   
  */
 38   
 
 39   
 public class ExtensionSpecification extends LocatablePropertyHolder implements
 40   
         IExtensionSpecification
 41   
 {
 42   
     private static final Log LOG = LogFactory.getLog(ExtensionSpecification.class);
 43   
 
 44   
     private String _className;
 45   
 
 46   
     protected Map _configuration = new HashMap();
 47   
 
 48   
     private boolean _immediate;
 49   
 
 50   
     /** @since 3.1 */
 51   
 
 52   
     private ClassResolver _resolver;
 53   
 
 54   
     /** @since 3.1 */
 55   
     private ValueConverter _converter;
 56   
 
 57   
     /** @since 3.1 */
 58  13
     public ExtensionSpecification(ClassResolver resolver, ValueConverter valueConverter)
 59   
     {
 60  13
         _resolver = resolver;
 61  13
         _converter = valueConverter;
 62   
     }
 63   
 
 64  0
     public String getClassName()
 65   
     {
 66  0
         return _className;
 67   
     }
 68   
 
 69  13
     public void setClassName(String className)
 70   
     {
 71  13
         _className = className;
 72   
     }
 73   
 
 74  33
     public void addConfiguration(String propertyName, String value)
 75   
     {
 76  33
         if (_configuration.containsKey(propertyName))
 77  0
             throw new IllegalArgumentException(Tapestry.format(
 78   
                     "ExtensionSpecification.duplicate-property",
 79   
                     this,
 80   
                     propertyName));
 81   
 
 82  33
         _configuration.put(propertyName, value);
 83   
     }
 84   
 
 85   
     /**
 86   
      * Returns an immutable Map of the configuration; keyed on property name, with values as
 87   
      * properties to assign.
 88   
      */
 89   
 
 90  4
     public Map getConfiguration()
 91   
     {
 92  4
         return Collections.unmodifiableMap(_configuration);
 93   
     }
 94   
 
 95   
     /**
 96   
      * Invoked to instantiate an instance of the extension and return it. It also configures
 97   
      * properties of the extension.
 98   
      */
 99   
 
 100  10
     public Object instantiateExtension()
 101   
     {
 102  10
         if (LOG.isDebugEnabled())
 103  0
             LOG.debug("Instantiating extension class " + _className + ".");
 104   
 
 105  10
         Class extensionClass = null;
 106  10
         Object result = null;
 107   
 
 108  10
         try
 109   
         {
 110  10
             extensionClass = _resolver.findClass(_className);
 111   
         }
 112   
         catch (Exception ex)
 113   
         {
 114  0
             throw new ApplicationRuntimeException(Tapestry.format(
 115   
                     "ExtensionSpecification.bad-class",
 116   
                     _className), getLocation(), ex);
 117   
         }
 118   
 
 119  10
         result = instantiateInstance(extensionClass, result);
 120   
 
 121  10
         initializeProperties(result);
 122   
 
 123  10
         return result;
 124   
     }
 125   
 
 126  10
     private void initializeProperties(Object extension)
 127   
     {
 128   
 
 129  10
         Iterator i = _configuration.entrySet().iterator();
 130  10
         while (i.hasNext())
 131   
         {
 132  25
             Map.Entry entry = (Map.Entry) i.next();
 133   
 
 134  25
             String propertyName = (String) entry.getKey();
 135  25
             String textValue = (String) entry.getValue();
 136   
 
 137  25
             try
 138   
             {
 139  25
                 Class propertyType = PropertyUtils.getPropertyType(extension, propertyName);
 140   
 
 141  25
                 Object objectValue = _converter.coerceValue(textValue, propertyType);
 142   
 
 143  25
                 PropertyUtils.write(extension, propertyName, objectValue);
 144   
             }
 145   
             catch (Exception ex)
 146   
             {
 147  0
                 throw new ApplicationRuntimeException(ex.getMessage(), getLocation(), ex);
 148   
             }
 149   
         }
 150   
     }
 151   
 
 152  10
     private Object instantiateInstance(Class extensionClass, Object result)
 153   
     {
 154  10
         try
 155   
         {
 156  10
             result = extensionClass.newInstance();
 157   
         }
 158   
         catch (Exception ex)
 159   
         {
 160  0
             throw new ApplicationRuntimeException(ex.getMessage(), getLocation(), ex);
 161   
         }
 162   
 
 163  10
         return result;
 164   
     }
 165   
 
 166  0
     public String toString()
 167   
     {
 168  0
         StringBuffer buffer = new StringBuffer("ExtensionSpecification@");
 169  0
         buffer.append(Integer.toHexString(hashCode()));
 170  0
         buffer.append('[');
 171  0
         buffer.append(_className);
 172   
 
 173  0
         if (_configuration != null)
 174   
         {
 175  0
             buffer.append(' ');
 176  0
             buffer.append(_configuration);
 177   
         }
 178   
 
 179  0
         buffer.append(']');
 180   
 
 181  0
         return buffer.toString();
 182   
     }
 183   
 
 184   
     /**
 185   
      * Returns true if the extensions should be instantiated immediately after the containing
 186   
      * {@link org.apache.tapestry.spec.LibrarySpecification}if parsed. Non-immediate extensions are
 187   
      * instantiated only as needed.
 188   
      */
 189   
 
 190  13
     public boolean isImmediate()
 191   
     {
 192  13
         return _immediate;
 193   
     }
 194   
 
 195  13
     public void setImmediate(boolean immediate)
 196   
     {
 197  13
         _immediate = immediate;
 198   
     }
 199   
 
 200   
 }