1 28 29 package com.idaremedia.antx.starters; 30 31 import java.lang.reflect.Field ; 32 33 import org.apache.tools.ant.BuildException; 34 import org.apache.tools.ant.Project; 35 36 import com.idaremedia.antx.AssertableLibDefinition; 37 import com.idaremedia.antx.FixtureInitializer; 38 import com.idaremedia.antx.Iteration; 39 import com.idaremedia.antx.helpers.Tk; 40 41 55 56 public abstract class IterationConfigurator extends AssertableLibDefinition 57 implements FixtureInitializer 58 { 59 63 protected IterationConfigurator(String iam) 64 { 65 super(iam); 66 } 67 68 69 70 76 protected IterationConfigurator(String iam, String fieldName) 77 { 78 super(iam); 79 setInjectionFieldName(fieldName); 80 } 81 82 83 84 88 protected void setInjectionFieldName(String fieldName) 89 { 90 require_(fieldName!=null,"setField- nonzro field"); 91 m_fieldName = fieldName; 92 } 93 94 95 96 101 protected final String getInjectionFieldName() 102 { 103 return m_fieldName; 104 } 105 106 107 108 118 protected abstract Object newInjectionField(Object currentValue); 119 120 121 122 128 protected void verifyCanExecute_(String calr) 129 { 130 verifyInProject_(calr); 131 132 if (m_fieldName==null) { 133 String e = uistrs().get("fixture.ioc.unknown.field", 134 Tk.leafNameFrom(getClass())); 135 log(e,Project.MSG_ERR); 136 throw new BuildException(e,getLocation()); 137 } 138 } 139 140 141 142 149 protected boolean doInjection() 150 { 151 return true; 152 } 153 154 155 156 162 protected void verifyInitialFieldState(Iteration context, 163 Object currentValue) 164 { 165 } 167 168 169 170 176 protected void verifyAfterInjection() 177 { 178 } 180 181 182 183 191 public void execute() 192 { 193 if (doInjection()) { 194 verifyCanExecute_("exec"); 195 196 Iteration context = Iteration.get(); 197 try { 198 Field f = Tk.findField(context.getClass(),m_fieldName); 199 boolean open = openField(f); 200 try { 201 204 synchronized(context.getConfigLock(m_fieldName)) { 205 Object current = f.get(context); 206 verifyInitialFieldState(context,current); 207 f.set(context,newInjectionField(current)); 208 } 209 210 } finally { 211 closeField(f,open); 212 } 213 214 } catch(NoSuchFieldException nfX) { 215 String e = uistrs().get("fixture.ioc.bad.field", 216 Tk.leafNameFrom(context.getClass()),m_fieldName); 217 log(e,Project.MSG_ERR); 218 throw new BuildException(e,nfX,getLocation()); 219 } catch(IllegalAccessException iaX) { 220 throw new BuildException(iaX,getLocation()); 221 } catch(IllegalArgumentException baX) { 222 throw new BuildException(baX,getLocation()); 223 } 224 225 verifyAfterInjection(); 226 } 227 } 228 229 230 231 236 private boolean openField(Field f) 237 { 238 boolean is = f.isAccessible(); 239 if (!is) { 240 f.setAccessible(true); 241 } 242 return is; 243 } 244 245 246 247 253 private void closeField(Field f, boolean was) 254 { 255 if (!was) { 256 f.setAccessible(false); 257 } 258 } 259 260 261 private String m_fieldName; 262 } 263 264 265 | Popular Tags |