1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package mnemosyne.samples.simple;
17
18 import mnemosyne.aop.aspectwerkz.AspectwerkzAopSystem;
19 import mnemosyne.archiver.FileSystemArchiver;
20 import mnemosyne.core.DefaultPersistentObjectFactory;
21 import mnemosyne.core.VersionManager;
22 import mnemosyne.guid.RmiUidGuidGenerator;
23 import mnemosyne.system.LocalPersistentSystem;
24 import mnemosyne.system.PersistentSystem;
25 import org.picocontainer.MutablePicoContainer;
26 import org.picocontainer.Parameter;
27 import org.picocontainer.defaults.ComponentParameter;
28 import org.picocontainer.defaults.ConstantParameter;
29 import org.picocontainer.defaults.DefaultPicoContainer;
30
31 /***
32 * @version $Id: SimpleSample.java,v 1.2 2004/08/12 14:01:07 charlesblaxland Exp $
33 */
34 public class SimpleSample
35 {
36 private static final String DATA_DIR = "data";
37 private static final String ROOT_NAME = "SimpleSample";
38
39 private PersistentSystem system;
40
41 public static void main(String[] args)
42 {
43 SimpleSample sample = new SimpleSample();
44 try
45 {
46 sample.run();
47 }
48 catch (Exception e)
49 {
50 e.printStackTrace();
51 }
52 }
53
54 private void run() throws Exception
55 {
56 System.out.println("Initializing persistent system");
57 system = initializePersistentSystem();
58 System.out.println("Persistent system initialized");
59
60 TestObject obj = (TestObject) system.getPersistentRoot(ROOT_NAME);
61 if (obj != null)
62 {
63
64 System.out.println("Old TestObject:" + obj.getI());
65 }
66 else
67 {
68
69 obj = new TestObject();
70 System.out.println("Storing TestObject: " + obj.getI());
71 system.beginTransaction();
72 {
73 system.setPersistentRoot(ROOT_NAME, obj);
74 }
75 system.endTransaction();
76 }
77
78 int newValue;
79 system.beginTransaction();
80 {
81 newValue = obj.getI() + 1;
82 System.out.println("Incrementing TestObject to " + newValue);
83 obj.setI(newValue);
84 }
85 system.endTransaction();
86
87 System.out.println("Incremented TestObject: " + obj.getI());
88
89 Thread otherThread = new TestThread();
90 otherThread.start();
91 try
92 {
93 Thread.sleep(500);
94 }
95 catch (InterruptedException e)
96 {
97 e.printStackTrace();
98 }
99
100 system.beginTransaction();
101 {
102 newValue = obj.getI() + 1;
103 System.out.println("Incrementing TestObject to " + newValue);
104 obj.setI(newValue);
105 }
106 system.endTransaction();
107
108 System.out.println("Incremented TestObject: " + obj.getI());
109
110 otherThread.join();
111 }
112
113 private PersistentSystem initializePersistentSystem()
114 {
115 MutablePicoContainer pico = new DefaultPicoContainer();
116 pico.registerComponentImplementation(VersionManager.class);
117 pico.registerComponentImplementation(RmiUidGuidGenerator.class);
118 pico.registerComponentImplementation(AspectwerkzAopSystem.class);
119 pico.registerComponentImplementation(DefaultPersistentObjectFactory.class);
120 pico.registerComponentImplementation(FileSystemArchiver.class, FileSystemArchiver.class, new Parameter[] {
121 new ComponentParameter(), new ComponentParameter(),
122 new ConstantParameter(DATA_DIR)
123 });
124 pico.registerComponentImplementation(LocalPersistentSystem.class);
125 return (LocalPersistentSystem) pico.getComponentInstanceOfType(LocalPersistentSystem.class);
126 }
127
128 class TestThread extends Thread
129 {
130 public void run()
131 {
132 TestObject obj = (TestObject) system.getPersistentRoot(ROOT_NAME);
133 System.out.println("Thread2: TestObject before sleep: " + obj.getI());
134 try
135 {
136 Thread.sleep(2000);
137 }
138 catch (InterruptedException e)
139 {
140 }
141
142 System.out.println("Thread2: TestObject after sleep: " + obj.getI());
143 system.advanceVersion();
144 System.out.println("Thread2: TestObject after advance version: " + obj.getI());
145 }
146 }
147 }