1 package com.db4odoc.f1.clientserver; 2 3 import java.io.*; 4 import com.db4o.*; 5 import com.db4odoc.f1.*; 6 7 8 public class ClientServerExample { 9 private final static int PORT=0xdb40; 10 private final static String USER="user"; 11 private final static String PASSWORD="password"; 12 13 public static void main(String [] args) throws IOException { 14 new File(Util.YAPFILENAME).delete(); 15 accessLocalServer(); 16 new File(Util.YAPFILENAME).delete(); 17 ObjectContainer db=Db4o.openFile(Util.YAPFILENAME); 18 try { 19 setFirstCar(db); 20 setSecondCar(db); 21 } 22 finally { 23 db.close(); 24 } 25 configureDb4o(); 26 ObjectServer server=Db4o.openServer(Util.YAPFILENAME,0); 27 try { 28 queryLocalServer(server); 29 demonstrateLocalReadCommitted(server); 30 demonstrateLocalRollback(server); 31 } 32 finally { 33 server.close(); 34 } 35 accessRemoteServer(); 36 server=Db4o.openServer(Util.YAPFILENAME,PORT); 37 server.grantAccess(USER,PASSWORD); 38 try { 39 queryRemoteServer(PORT,USER,PASSWORD); 40 demonstrateRemoteReadCommitted(PORT,USER,PASSWORD); 41 demonstrateRemoteRollback(PORT,USER,PASSWORD); 42 } 43 finally { 44 server.close(); 45 } 46 } 47 49 public static void setFirstCar(ObjectContainer db) { 50 Pilot pilot=new Pilot("Rubens Barrichello",99); 51 Car car=new Car("BMW"); 52 car.setPilot(pilot); 53 db.set(car); 54 } 55 57 public static void setSecondCar(ObjectContainer db) { 58 Pilot pilot=new Pilot("Michael Schumacher",100); 59 Car car=new Car("Ferrari"); 60 car.setPilot(pilot); 61 db.set(car); 62 } 63 65 public static void accessLocalServer() { 66 ObjectServer server=Db4o.openServer(Util.YAPFILENAME,0); 67 try { 68 ObjectContainer client=server.openClient(); 69 client.close(); 71 } 72 finally { 73 server.close(); 74 } 75 } 76 78 public static void queryLocalServer(ObjectServer server) { 79 ObjectContainer client=server.openClient(); 80 listResult(client.get(new Car(null))); 81 client.close(); 82 } 83 85 public static void configureDb4o() { 86 Db4o.configure().objectClass(Car.class).updateDepth(3); 87 } 88 90 public static void demonstrateLocalReadCommitted(ObjectServer server) { 91 ObjectContainer client1=server.openClient(); 92 ObjectContainer client2=server.openClient(); 93 Pilot pilot=new Pilot("David Coulthard",98); 94 ObjectSet result=client1.get(new Car("BMW")); 95 Car car=(Car)result.next(); 96 car.setPilot(pilot); 97 client1.set(car); 98 listResult(client1.get(new Car(null))); 99 listResult(client2.get(new Car(null))); 100 client1.commit(); 101 listResult(client1.get(Car.class)); 102 listRefreshedResult(client2,client2.get(Car.class),2); 103 client1.close(); 104 client2.close(); 105 } 106 108 public static void demonstrateLocalRollback(ObjectServer server) { 109 ObjectContainer client1=server.openClient(); 110 ObjectContainer client2=server.openClient(); 111 ObjectSet result=client1.get(new Car("BMW")); 112 Car car=(Car)result.next(); 113 car.setPilot(new Pilot("Someone else",0)); 114 client1.set(car); 115 listResult(client1.get(new Car(null))); 116 listResult(client2.get(new Car(null))); 117 client1.rollback(); 118 client1.ext().refresh(car,2); 119 listResult(client1.get(new Car(null))); 120 listResult(client2.get(new Car(null))); 121 client1.close(); 122 client2.close(); 123 } 124 126 public static void accessRemoteServer() throws IOException { 127 ObjectServer server=Db4o.openServer(Util.YAPFILENAME,PORT); 128 server.grantAccess(USER,PASSWORD); 129 try { 130 ObjectContainer client=Db4o.openClient("localhost",PORT,USER,PASSWORD); 131 client.close(); 133 } 134 finally { 135 server.close(); 136 } 137 } 138 140 public static void queryRemoteServer(int port,String user,String password) throws IOException { 141 ObjectContainer client=Db4o.openClient("localhost",port,user,password); 142 listResult(client.get(new Car(null))); 143 client.close(); 144 } 145 147 public static void demonstrateRemoteReadCommitted(int port,String user,String password) throws IOException { 148 ObjectContainer client1=Db4o.openClient("localhost",port,user,password); 149 ObjectContainer client2=Db4o.openClient("localhost",port,user,password); 150 Pilot pilot=new Pilot("Jenson Button",97); 151 ObjectSet result=client1.get(new Car(null)); 152 Car car=(Car)result.next(); 153 car.setPilot(pilot); 154 client1.set(car); 155 listResult(client1.get(new Car(null))); 156 listResult(client2.get(new Car(null))); 157 client1.commit(); 158 listResult(client1.get(new Car(null))); 159 listRefreshedResult(client2,client2.get(Car.class),2); 160 client1.close(); 161 client2.close(); 162 } 163 165 public static void demonstrateRemoteRollback(int port,String user,String password) throws IOException { 166 ObjectContainer client1=Db4o.openClient("localhost",port,user,password); 167 ObjectContainer client2=Db4o.openClient("localhost",port,user,password); 168 ObjectSet result=client1.get(new Car(null)); 169 Car car=(Car)result.next(); 170 car.setPilot(new Pilot("Someone else",0)); 171 client1.set(car); 172 listResult(client1.get(new Car(null))); 173 listResult(client2.get(new Car(null))); 174 client1.rollback(); 175 client1.ext().refresh(car,2); 176 listResult(client1.get(new Car(null))); 177 listResult(client2.get(new Car(null))); 178 client1.close(); 179 client2.close(); 180 } 181 183 public static void listRefreshedResult(ObjectContainer container,ObjectSet result,int depth) { 184 System.out.println(result.size()); 185 while(result.hasNext()) { 186 Object obj = result.next(); 187 container.ext().refresh(obj, depth); 188 System.out.println(obj); 189 } 190 } 191 193 public static void listResult(ObjectSet result) { 194 System.out.println(result.size()); 195 while(result.hasNext()) { 196 System.out.println(result.next()); 197 } 198 } 199 } 201 | Popular Tags |