1 29 package net.sourceforge.groboutils.mbtf.v1.ant; 30 31 import org.apache.tools.ant.Project; 32 import org.apache.tools.ant.ProjectComponent; 33 import org.apache.tools.ant.Task; 34 import org.apache.tools.ant.TaskAdapter; 35 import org.apache.tools.ant.BuildException; 36 import org.apache.tools.ant.types.Reference; 37 import org.apache.tools.ant.types.DataType; 38 import org.apache.tools.ant.taskdefs.Definer; 39 40 import java.util.Vector ; 41 42 import net.sourceforge.groboutils.mbtf.v1.IValidate; 43 import net.sourceforge.groboutils.mbtf.v1.ISystem; 44 import net.sourceforge.groboutils.mbtf.v1.IAction; 45 46 import net.sourceforge.groboutils.mbtf.v1.assembler.AsmblTransition; 47 48 import org.apache.log4j.Logger; 49 50 51 61 public class TransitionType extends AbstractStateMachineType 62 { 63 private static final Logger LOG = Logger.getLogger( TransitionType.class ); 64 65 public static final String DEFAULT_ANT_NAME = "mbtf.transition"; 66 67 private Vector transitions = new Vector (); 68 private String nextState; 69 private IAction action; 70 71 72 public void setNextState( String name ) 73 { 74 this.nextState = name; 75 } 76 77 78 public String getNextState() 79 { 80 if (isReference()) 81 { 82 return ((TransitionType)getCheckedRef( 83 TransitionType.class, DEFAULT_ANT_NAME )). 84 getNextState(); 85 } 86 return this.nextState; 87 } 88 89 90 public void addAntAction( ValidateTaskType action ) 91 throws BuildException 92 { 93 if (this.action != null) 94 { 95 throw new BuildException( 96 "too many action elements: only one allowed" ); 97 } 98 this.action = action; 99 } 100 101 102 public void addJavaAction( ValidateJavaType action ) 103 throws BuildException 104 { 105 if (this.action != null) 106 { 107 throw new BuildException( 108 "too many action elements: only one allowed" ); 109 } 110 LOG.debug("Adding ValidateJavaType"); 111 this.action = action; 112 } 113 114 115 public void setAntActionRef( Reference ref ) 116 { 117 ValidateTaskType vtt = new ValidateTaskType(); 118 vtt.setProject( project ); 119 vtt.setRefid( ref ); 120 121 addAntAction( vtt ); 122 } 123 124 125 public void setJavaActionRef( Reference ref ) 126 { 127 Object o = ref.getReferencedObject( project ); 128 if (o != null && o instanceof TaskAdapter) 129 { 130 ((TaskAdapter)o).execute(); 131 o = ((TaskAdapter)o).getProxy(); 132 } 133 addJavaAction( (ValidateJavaType)o ); 134 } 135 136 137 public IAction getAction() 138 { 139 if (isReference()) 140 { 141 return ((TransitionType)getCheckedRef( 142 TransitionType.class, DEFAULT_ANT_NAME )). 143 getAction(); 144 } 145 if (project != null) 146 { 147 if (this.action instanceof ProjectComponent) 148 { 149 LOG.debug("Setting action "+this.action+"'s project to "+ 150 getProject()); 151 ((ProjectComponent)this.action).setProject( getProject() ); 152 } 153 else 154 { 155 LOG.debug("Action "+this.action+ 156 " is not a ProjectComponent, but "+this.action.getClass(). 157 getName()); 158 } 159 } 160 else 161 { 162 LOG.debug("Transition "+getName()+" has null project"); 163 } 164 return this.action; 165 } 166 167 168 public AsmblTransition createTransition() 169 { 170 if (isReference()) 171 { 172 return ((TransitionType)getCheckedRef( 173 TransitionType.class, DEFAULT_ANT_NAME )). 174 createTransition(); 175 } 176 verifySettings(); 177 178 AsmblTransition at = new AsmblTransition(); 179 at.setName( getName() ); 180 at.setNextStateName( getNextState() ); 181 at.setAction( getAction() ); 182 183 IValidate v[] = getValidates(); 184 for (int i = 0; i < v.length; ++i) 185 { 186 at.addValidate( v[i] ); 187 } 188 189 return at; 190 } 191 192 193 protected void verifySettings() 194 throws BuildException 195 { 196 if (isReference()) 197 { 198 ((TransitionType)getCheckedRef( 199 TransitionType.class, DEFAULT_ANT_NAME )). 200 verifySettings(); 201 return; 202 } 203 super.verifySettings(); 204 if (this.nextState == null) 205 { 206 throw new BuildException( "next state name not set", location ); 207 } 208 if (this.action == null) 209 { 210 throw new BuildException( "next state name not set", location ); 211 } 212 } 213 } 214 215 | Popular Tags |