01: /*
02: * $Id: TokenAction.java 484899 2006-12-09 03:02:39Z mrdon $
03: *
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "License"); you may not use this file except in compliance
10: * with the License. You may obtain a copy of the License at
11: *
12: * http://www.apache.org/licenses/LICENSE-2.0
13: *
14: * Unless required by applicable law or agreed to in writing,
15: * software distributed under the License is distributed on an
16: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17: * KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations
19: * under the License.
20: */
21: package org.apache.struts2.showcase.token;
22:
23: import java.util.Date;
24:
25: import com.opensymphony.xwork2.ActionContext;
26: import com.opensymphony.xwork2.ActionSupport;
27:
28: /**
29: * Example to illustrate the <code>token</code> and <code>token-session</code> interceptor.
30: *
31: */
32: public class TokenAction extends ActionSupport {
33:
34: private static final long serialVersionUID = 616150375751184884L;
35:
36: private int amount;
37:
38: public String execute() throws Exception {
39: // transfer from source to destination
40:
41: Integer balSource = (Integer) ActionContext.getContext()
42: .getSession().get("balanceSource");
43: Integer balDest = (Integer) ActionContext.getContext()
44: .getSession().get("balanceDestination");
45:
46: Integer newSource = new Integer(balSource.intValue() - amount);
47: Integer newDest = new Integer(balDest.intValue() + amount);
48:
49: ActionContext.getContext().getSession().put("balanceSource",
50: newSource);
51: ActionContext.getContext().getSession().put(
52: "balanceDestination", newDest);
53: ActionContext.getContext().getSession().put("time", new Date());
54:
55: Thread.sleep(2000); // to simulate processing time
56:
57: return SUCCESS;
58: }
59:
60: public String input() throws Exception {
61: // prepare input form
62: Integer balSource = (Integer) ActionContext.getContext()
63: .getSession().get("balanceSource");
64: Integer balDest = (Integer) ActionContext.getContext()
65: .getSession().get("balanceDestination");
66:
67: if (balSource == null) {
68: // first time set up an initial account balance
69: balSource = new Integer(1200);
70: ActionContext.getContext().getSession().put(
71: "balanceSource", balSource);
72: }
73:
74: if (balDest == null) {
75: // first time set up an initial account balance
76: balDest = new Integer(2500);
77: ActionContext.getContext().getSession().put(
78: "balanceDestination", balDest);
79: }
80:
81: return INPUT;
82: }
83:
84: public int getAmount() {
85: return amount;
86: }
87:
88: public void setAmount(int amount) {
89: this.amount = amount;
90: }
91:
92: }
|