KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > state > config > StateInterceptor


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 /**
14  * @author Yanick Duchesne
15  * <dl>
16  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
17  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
18  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
19  * </dl>
20  */

21 public class StateInterceptor implements State, ObjectHandlerIF {
22   private String JavaDoc[] _preExec;
23   private String JavaDoc[] _postExec;
24   private State _toExecute;
25
26   public StateInterceptor() {
27   }
28
29   /**
30    * @param list the comma delimited list of states that must be executed
31    * prior to the state that this instance wraps.
32    */

33   public void setPreExec(String JavaDoc 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   /**
42    * @param list the comma delimited list of states that must be executed
43    * after the state that this instance wraps.
44    */

45   public void setPostExec(String JavaDoc 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   /**
54    * @see State#getId()
55    */

56   public String JavaDoc getId() {
57     return _toExecute.getId();
58   }
59
60   /**
61    * @see org.sapia.soto.state.Executable#execute(org.sapia.soto.state.Result)
62    */

63   public void execute(Result st) {
64     if (_toExecute == null) {
65       throw new IllegalStateException JavaDoc(
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 JavaDoc stateId) {
109     _preExec = grow(_preExec, stateId);
110   }
111
112   public void addPostExec(String JavaDoc stateId) {
113     _postExec = grow(_postExec, stateId);
114   }
115
116   static String JavaDoc[] grow(String JavaDoc[] in, String JavaDoc s) {
117     if (in == null) {
118       in = new String JavaDoc[0];
119     }
120
121     String JavaDoc[] newArr = new String JavaDoc[in.length + 1];
122     System.arraycopy(in, 0, newArr, 0, in.length);
123
124     return newArr;
125   }
126
127   /**
128    * @see org.sapia.util.xml.confix.ObjectHandlerIF#handleObject(java.lang.String, java.lang.Object)
129    */

130   public void handleObject(String JavaDoc name, Object JavaDoc obj)
131     throws ConfigurationException {
132     if (obj instanceof State) {
133       _toExecute = (State) obj;
134     }
135   }
136 }
137
Popular Tags