KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > legacy > soda > engines > db4o > STDb4oClientServer


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.test.legacy.soda.engines.db4o;
22
23 import java.io.*;
24
25 import com.db4o.*;
26 import com.db4o.foundation.*;
27 import com.db4o.query.*;
28 import com.db4o.test.legacy.soda.*;
29
30 public class STDb4oClientServer implements STEngine {
31
32     private static final int PORT = 4044;
33     private static final String JavaDoc HOST = "localhost";
34     private static final String JavaDoc FILE = "sodacs.yap";
35     private static final String JavaDoc USER = "S.O.D.A.";
36     private static final String JavaDoc PASS = "rocks";
37
38     private static final boolean IN_PROCESS_SERVER = true;
39
40     private com.db4o.ObjectServer server;
41     private com.db4o.ObjectContainer con;
42
43     /**
44      * starts a db4o server.
45      * <br>To test with a remote server:<br>
46      * - set IN_PROCESS_SERVER to false
47      * - start STDb4oClientServer on the server
48      * - run SodaTest on the client
49      * - STDb4oClientServer needs to be uncommented in SodaTest#ENGINES
50      * The server can be stopped with CTRL + C.
51      */

52     public static void main(String JavaDoc[] args) {
53         new File(FILE).delete();
54         ObjectServer server = Db4o.openServer(FILE, PORT);
55         server.grantAccess(USER, PASS);
56         server.ext().configure().messageLevel(-1);
57     }
58
59     public void reset() {
60         new File(FILE).delete();
61     }
62
63     public Query query() {
64         return con.query();
65     }
66     
67     public void open() {
68         Db4o.configure().messageLevel(-1);
69
70         if (IN_PROCESS_SERVER) {
71             server = Db4o.openServer(FILE, PORT);
72             server.grantAccess(USER, PASS);
73             // wait for the server to be online
74
Cool.sleepIgnoringInterruption(3000);
75         }
76         try {
77             con = Db4o.openClient(HOST, PORT, USER, PASS);
78         } catch (Exception JavaDoc e) {
79             e.printStackTrace();
80         }
81     }
82
83     public void close() {
84         con.close();
85         if (IN_PROCESS_SERVER) {
86             server.close();
87         }
88     }
89     
90     public void store(Object JavaDoc obj) {
91         con.set(obj);
92     }
93
94     public void commit(){
95         con.commit();
96     }
97     
98     public void delete(Object JavaDoc obj){
99         con.delete(obj);
100     }
101 }
102
Popular Tags