1 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 30 37 38 public class ExtensionSpecification extends LocatablePropertyHolder implements 39 IExtensionSpecification 40 { 41 private static final Log LOG = LogFactory.getLog(ExtensionSpecification.class); 42 43 private String _className; 44 45 protected Map _configuration = new HashMap (); 46 47 private boolean _immediate; 48 49 50 51 private ClassResolver _resolver; 52 53 54 private ValueConverter _converter; 55 56 57 public ExtensionSpecification(ClassResolver resolver, ValueConverter valueConverter) 58 { 59 _resolver = resolver; 60 _converter = valueConverter; 61 } 62 63 public String getClassName() 64 { 65 return _className; 66 } 67 68 public void setClassName(String className) 69 { 70 _className = className; 71 } 72 73 public void addConfiguration(String propertyName, String value) 74 { 75 if (_configuration.containsKey(propertyName)) 76 throw new IllegalArgumentException (Tapestry.format( 77 "ExtensionSpecification.duplicate-property", 78 this, 79 propertyName)); 80 81 _configuration.put(propertyName, value); 82 } 83 84 88 89 public Map getConfiguration() 90 { 91 return Collections.unmodifiableMap(_configuration); 92 } 93 94 98 99 public Object instantiateExtension() 100 { 101 if (LOG.isDebugEnabled()) 102 LOG.debug("Instantiating extension class " + _className + "."); 103 104 Class extensionClass = null; 105 Object result = null; 106 107 try 108 { 109 extensionClass = _resolver.findClass(_className); 110 } 111 catch (Exception ex) 112 { 113 throw new ApplicationRuntimeException(Tapestry.format( 114 "ExtensionSpecification.bad-class", 115 _className), getLocation(), ex); 116 } 117 118 result = instantiateInstance(extensionClass, result); 119 120 initializeProperties(result); 121 122 return result; 123 } 124 125 private void initializeProperties(Object extension) 126 { 127 128 Iterator i = _configuration.entrySet().iterator(); 129 while (i.hasNext()) 130 { 131 Map.Entry entry = (Map.Entry ) i.next(); 132 133 String propertyName = (String ) entry.getKey(); 134 String textValue = (String ) entry.getValue(); 135 136 try 137 { 138 Class propertyType = PropertyUtils.getPropertyType(extension, propertyName); 139 140 Object objectValue = _converter.coerceValue(textValue, propertyType); 141 142 PropertyUtils.write(extension, propertyName, objectValue); 143 } 144 catch (Exception ex) 145 { 146 throw new ApplicationRuntimeException(ex.getMessage(), getLocation(), ex); 147 } 148 } 149 } 150 151 private Object instantiateInstance(Class extensionClass, Object result) 152 { 153 try 154 { 155 result = extensionClass.newInstance(); 156 } 157 catch (Exception ex) 158 { 159 throw new ApplicationRuntimeException(ex.getMessage(), getLocation(), ex); 160 } 161 162 return result; 163 } 164 165 public String toString() 166 { 167 StringBuffer buffer = new StringBuffer ("ExtensionSpecification@"); 168 buffer.append(Integer.toHexString(hashCode())); 169 buffer.append('['); 170 buffer.append(_className); 171 172 if (_configuration != null) 173 { 174 buffer.append(' '); 175 buffer.append(_configuration); 176 } 177 178 buffer.append(']'); 179 180 return buffer.toString(); 181 } 182 183 188 189 public boolean isImmediate() 190 { 191 return _immediate; 192 } 193 194 public void setImmediate(boolean immediate) 195 { 196 _immediate = immediate; 197 } 198 199 } | Popular Tags |