KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > test > core > remote > TestRemote


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.test.core.remote;
25
26 import org.objectweb.jalisto.se.JalistoFactory;
27 import org.objectweb.jalisto.se.api.Session;
28 import org.objectweb.jalisto.se.test.data.Book;
29
30 import java.util.ArrayList JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 public class TestRemote {
34
35     public static void main(String JavaDoc[] args) {
36         Session session = JalistoFactory.getSession("jalisto-client.properties");
37         session.openSession();
38         session.defineClass(Book.getMetaDescription());
39
40         for (int i = 0; i < 5; i++) {
41             createAndReadBook(session);
42         }
43
44         for (int i = 0; i < 3; i++) {
45             ArrayList JavaDoc oids = createBooks(session, 5);
46             updateBooks(session, oids);
47             deleteBooks(session, oids);
48         }
49
50         {
51             ArrayList JavaDoc oids = createBooks(session, 20);
52             updateBooks(session, oids);
53             testExtent(session);
54         }
55
56         session.closeSession();
57     }
58
59     public static void createAndReadBook(Session session) {
60         session.currentTransaction().begin();
61         Object JavaDoc oid = session.createObject(Book.newBook().toArray(), Book.class);
62         session.currentTransaction().commit();
63
64         session.currentTransaction().begin();
65         System.out.println(Book.toBook(session.readObjectByOid(oid)));
66         session.currentTransaction().commit();
67
68         try {
69             Thread.sleep(1000);
70         } catch (InterruptedException JavaDoc e) {
71             e.printStackTrace();
72         }
73     }
74
75     public static ArrayList JavaDoc createBooks(Session session, int nbrBooks) {
76         ArrayList JavaDoc oids = new ArrayList JavaDoc();
77
78         session.currentTransaction().begin();
79         for (int i = 0; i < nbrBooks; i++) {
80             Object JavaDoc oid = session.makeNewFileOid(Book.class);
81             System.out.println("make oid : " + oid);
82             oids.add(oid);
83         }
84         session.currentTransaction().commit();
85
86         session.currentTransaction().begin();
87         for (int i = 0; i < nbrBooks; i++) {
88             Book book = Book.newBook();
89             System.out.println("create : " + oids.get(i) + " " + book);
90             session.createObject(oids.get(i), book.toArray());
91         }
92         session.currentTransaction().commit();
93
94         return oids;
95     }
96
97     public static void updateBooks(Session session, ArrayList JavaDoc oids) {
98         session.currentTransaction().begin();
99         for (int i = 0; i < oids.size(); i++) {
100             Object JavaDoc oid = oids.get(i);
101             Book book = Book.toBook(session.readObjectByOid(oid));
102             book.setAuthor(book.getAuthor() + "pouet");
103             System.out.println("update : " + book);
104             session.updateObjectByOid(oid, book.toArray());
105         }
106         session.currentTransaction().commit();
107     }
108
109     public static void deleteBooks(Session session, ArrayList JavaDoc oids) {
110         session.currentTransaction().begin();
111         for (int i = 0; i < oids.size(); i++) {
112             Object JavaDoc oid = oids.get(i);
113             System.out.println("delete : " + oid);
114             session.deleteObjectByOid(oid);
115         }
116         session.currentTransaction().commit();
117     }
118
119     public static void testExtent(Session session) {
120         session.currentTransaction().begin();
121         Iterator JavaDoc extent = session.getExtent(Book.class).readFully().iterator();
122         while (extent.hasNext()) {
123             Object JavaDoc oid = extent.next();
124             Book book = Book.toBook(session.readObjectByOid(oid));
125             System.out.println("readed book : " + String.valueOf(book));
126         }
127         session.currentTransaction().commit();
128     }
129 }
130
Popular Tags