Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 153 154 155 156 157 158 < 159 > 160 161 162 163 164 165 .. 166 >> Next

01 class MementoExample {
02 Memento currentState = null;
03 Memento priorState = null;
04 private class Memento {
05 int imaInt;
06 double imaDouble;
07 Memento(int i, double d) {
08 imaInt = i;
09 imaDouble = d;
10 }
11 void setFrom(Memento source) {
12 imaInt=source.imaInt;
13 imaDouble=source.imaDouble;
14 }
15
Listing 50.2 MementoExample.java (continued)
440 Item 50
16 }
17
18 public MementoExample() {
19 currentState = new Memento(1,20.0);
20 priorState = new Memento(0,0.0);
21 priorState.setFrom(currentState);
22 }
23
24 public void changeState(int i, double d) {
25 priorState.setFrom(currentState);
26 currentState.imaInt = i;
27 currentState.imaDouble = d;
28 }
29
30 public void restoreState() {
31 currentState.setFrom(priorState);
32 }
33
34 public void printState() {
35 System.out.println("State:");
36 System.out.println(" imaInt = " +
37 currentState.imaInt);
38 System.out.println(" imaDouble = " +
39 currentState.imaDouble);
40 }
41 public static void main(String[] arg) {
42 MementoExample mt = new MementoExample();
43 System.out.println("Original state to 1,20.0");
44 mt.printState();
45 System.out.println("Changing state to 3, 7");
46 mt.changeState(3,7.00);
47 mt.printState();
48 System.out.println("Restoring state");
49 mt.restoreState();
50 mt.printState();
51
52 }
53
54 } // end MementoExample
Listing 50.2 (continued)
The StateTellerBean interface, shown in Listing 50.3, is a remote interface that extends the EJBObject class so that all the methods in it will be exposed to remote clients.
When Transactions Go Awry, or Consistent State in Stateful Session EJBs 441
01 package org.javapitfalls.item50;
02
03 import javax.ejb.EJBObject;
04 import java.rmi.RemoteException;
05
06 public interface StateTellerBean extends EJBObject {
07
08 public double getBalance() throws RemoteException;
09
10 public double Withdraw(double amount ) throws RemoteException;
11
13 public double Deposit(double amount ) throws RemoteException;
14
15 }
Listing 50.3 StateTellerBean.java
The StateTellerBeanHome interface, shown in Listing 50.4, extends the EJBHome class so that remote clients can access the application. Our application does not use this, but it is provided so that other applications can use it if they want to.
01 package org.javapitfalls.item50;
02
03 import java.rmi.RemoteException;
04 import javax.ejb.CreateException;
05 import javax.ejb.EJBHome;
06
07 public interface StateTellerBeanHome extends EJBHome {
08
09 StateTellerBean create() throws RemoteException, CreateException;
10
11 }
12
Listing 50.4 StateTellerBeanHome.java
Listed below is the output if the Memento pattern application is run. Notice the transitions between states and the restoration of the original values. This is the result of the Memento pattern implementation.
442 Item 50
01: prompt> java org.javapitfalls.item50.MementoExample
02:
03:
04: Original state 1,20.0 05: State:
06: imaInt = 1
07: imaDouble = 20.0
08: Changing state 3, 7 09: State:
10: imaInt = 3
11: imaDouble = 7.0
12: Restoring state 13: State
14: imaInt = 1
15: imaDouble = 20.0
16:
As shown in the rather simple Memento application, the memento operation remembers the context of your call, which allows an application to roll back the state of all the data within an EJB.
Index
SYMBOLS AND NUMERICS
@ (at sign), 264 { } braces, 262 [ ] brackets, 94 . (dot), 94
4 KB buffer size, 21 16-bit unicode characters, 18
A
abort() method, 393 AbstractCollection class, 162 abstracting details, bad example of, 39-40 AbstractList class, 162 AbstractSelectableChannel class, 18 AbstractSet class, 162 Abstract Windowing Toolkit (AWT), 122, 352 acceptChanges() method, 33 accept() method, 33, 152 AccessControl, 39-40 AccessException class, 41-42 Action event handler, 167 actionPerformed() method, 177-178 addMode() method, 173, 194 addOperation() method, 173 addType() method, 173 afterBegin() method, 434-436 afterCompletion() method, 434-435 allocateDirect() method, 26 AllTests.java example, 100 Another Neat Tool (ANT)
BugRat tracking tool and, 92 Checkstyle tool and, 94
command-line build operations, 94 CruiseControl and, 98
database creation, destruction and population, 96-98 Help targets, 91 JavaNCSS utility and, 93 JDepend utility and, 93 overview, 88-89 running JUnit tests using, 90 Apache Software Foundation (ASF), 391 applets, problems with, 227-231 application programming interfaces (APIs) collision of, 53-57 Java Regular Expressions, 266 applications Java Web Start, 231-232 servlet-based, 223 XSLTransformFilter, 249 ArrayList class, 162 ASF (Apache Software Foundation), 391 assertions AssertionExample.java, 61 enabling, 64-65 errors, 60, 62-63 how to use, 59 invariants, 60 postconditions, 60, 63-64 preconditions, 60 unreachable conditions, 63 at sign (@), 264 attachment() method, 31 attach() method, 31 authenticateHelper class, 239
443
444 Index
authenticate() method, 40, 42-44 authentication, 236, 238 AWT (Abstract Windowing Toolkit), 122, 352 AxisConstants class, 352
Previous << 1 .. 153 154 155 156 157 158 < 159 > 160 161 162 163 164 165 .. 166 >> Next