KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > state > cocoon > CocoonStateMachineService


1 package org.sapia.soto.state.cocoon;
2
3 import org.apache.cocoon.environment.ObjectModelHelper;
4 import org.apache.cocoon.environment.Request;
5 import org.apache.cocoon.environment.Session;
6
7 import org.sapia.soto.Env;
8 import org.sapia.soto.EnvAware;
9 import org.sapia.soto.Service;
10 import org.sapia.soto.state.StateMachineService;
11
12 import org.sapia.util.xml.confix.ObjectHandlerIF;
13
14 import org.xml.sax.ContentHandler JavaDoc;
15
16 import java.util.Map JavaDoc;
17 import java.util.Stack JavaDoc;
18
19
20 /**
21  * @author Yanick Duchesne
22  * <dl>
23  * <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>
24  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
25  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
26  * </dl>
27  */

28 public class CocoonStateMachineService extends StateMachineService
29   implements Service, EnvAware, ObjectHandlerIF, Consts {
30   private Env _env;
31
32   /**
33    * @see org.sapia.soto.EnvAware#setEnv(org.sapia.soto.Env)
34    */

35   public void setEnv(Env env) {
36     _env = env;
37   }
38
39   /**
40    * @see org.sapia.soto.Service#start()
41    */

42   public void start() throws Exception JavaDoc {
43   }
44
45   public void execute(String JavaDoc state, String JavaDoc module, Map JavaDoc objectModel,
46     ContentHandler JavaDoc handler) throws Exception JavaDoc {
47     CocoonContext ctx = new CocoonContext(_env, handler, objectModel);
48     Request req = (Request) objectModel.get(ObjectModelHelper.REQUEST_OBJECT);
49     Session sess;
50
51     if (req.getParameter(CONTINUATION_KEY) != null) {
52       sess = req.getSession();
53
54       Stack JavaDoc callStack;
55
56       if ((sess != null) &&
57             ((callStack = (Stack JavaDoc) sess.getAttribute(CONTINUATION_KEY)) != null)) {
58         if (callStack.size() > 0) {
59           Call c = (Call) callStack.peek();
60
61           try {
62             if (c.isExecuted()) {
63               ctx.setStack(c.getContextStack());
64               super.execute(c.getReturnState(), c.getReturnModule(), ctx);
65               callStack.pop();
66             } else {
67               ctx.setStack(c.getContextStack());
68               super.execute(c.getTargetState(), c.getTargetModule(), ctx);
69               c.setExecuted();
70             }
71           } finally {
72             sess.setAttribute(CONTINUATION_KEY, callStack);
73           }
74         } else {
75           super.execute(state, module, ctx);
76         }
77       }
78     } else if ((sess = req.getSession(false)) != null) {
79       Stack JavaDoc callStack;
80
81       if (((callStack = (Stack JavaDoc) sess.getAttribute(CONTINUATION_KEY)) != null) &&
82             (callStack.size() > 0)) {
83         Call c = (Call) callStack.pop();
84
85         try {
86           super.execute(c.getReturnState(), c.getReturnModule(), ctx);
87         } finally {
88           sess.setAttribute(CONTINUATION_KEY, callStack);
89         }
90       } else {
91         super.execute(state, module, ctx);
92       }
93     } else {
94       super.execute(state, module, ctx);
95     }
96   }
97 }
98
Popular Tags