| 1 package org.sapia.soto.state.config; 2 3 import org.apache.commons.lang.StringUtils; 4 5 import org.sapia.soto.state.Result; 6 import org.sapia.soto.state.State; 7 import org.sapia.soto.state.StateExecException; 8 9 import org.sapia.util.xml.confix.ConfigurationException; 10 import org.sapia.util.xml.confix.ObjectHandlerIF; 11 12 13 21 public class StateInterceptor implements State, ObjectHandlerIF { 22 private String [] _preExec; 23 private String [] _postExec; 24 private State _toExecute; 25 26 public StateInterceptor() { 27 } 28 29 33 public void setPreExec(String list) { 34 _preExec = StringUtils.split(list, ","); 35 36 for (int i = 0; i < _preExec.length; i++) { 37 _preExec[i] = _preExec[i].trim(); 38 } 39 } 40 41 45 public void setPostExec(String list) { 46 _postExec = StringUtils.split(list, ","); 47 48 for (int i = 0; i < _postExec.length; i++) { 49 _postExec[i] = _postExec[i].trim(); 50 } 51 } 52 53 56 public String getId() { 57 return _toExecute.getId(); 58 } 59 60 63 public void execute(Result st) { 64 if (_toExecute == null) { 65 throw new IllegalStateException ( 66 "State decorator does not contain any state"); 67 } 68 69 if (_preExec != null) { 70 for (int i = 0; i < _preExec.length; i++) { 71 try { 72 st.exec(_preExec[i], null); 73 } catch (StateExecException e) { 74 st.error(e); 75 } 76 } 77 } 78 79 if (st.isError()) { 80 return; 81 } 82 83 _toExecute.execute(st); 84 85 if (st.isError()) { 86 return; 87 } 88 89 if (_postExec != null) { 90 for (int i = 0; i < _postExec.length; i++) { 91 try { 92 st.exec(_postExec[i], null); 93 } catch (StateExecException e) { 94 st.error(e); 95 } 96 97 if (st.isError()) { 98 return; 99 } 100 } 101 } 102 } 103 104 public void setState(State state) { 105 _toExecute = state; 106 } 107 108 public void addPreExec(String stateId) { 109 _preExec = grow(_preExec, stateId); 110 } 111 112 public void addPostExec(String stateId) { 113 _postExec = grow(_postExec, stateId); 114 } 115 116 static String [] grow(String [] in, String s) { 117 if (in == null) { 118 in = new String [0]; 119 } 120 121 String [] newArr = new String [in.length + 1]; 122 System.arraycopy(in, 0, newArr, 0, in.length); 123 124 return newArr; 125 } 126 127 130 public void handleObject(String name, Object obj) 131 throws ConfigurationException { 132 if (obj instanceof State) { 133 _toExecute = (State) obj; 134 } 135 } 136 } 137 | Popular Tags |