KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > myoodb > selfHealing > Client


1 ///////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
4
//
5
// All Rights Reserved
6
//
7
// This program is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License and GNU Library
9
// General Public License as published by the Free Software Foundation;
10
// either version 2, or (at your option) any later version.
11
//
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License and GNU Library General Public License
16
// for more details.
17
//
18
// You should have received a copy of the GNU General Public License
19
// and GNU Library General Public License along with this program; if
20
// not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
21
// MA 02139, USA.
22
//
23
///////////////////////////////////////////////////////////////////////////////
24
package org.myoodb.selfHealing;
25
26 public class Client
27 {
28     public static int PORT = 54321;
29     public static String JavaDoc USERNAME = "admin";
30     public static String JavaDoc PASSWORD = "admin";
31
32     public static void main(String JavaDoc args[]) throws Exception JavaDoc
33     {
34         org.myoodb.MyOodbDatabase db = org.myoodb.MyOodbDatabase.open("myoodb://localhost:" + PORT, USERNAME, PASSWORD);
35
36         Family family = (Family) db.getRoot("The Smith Family");
37
38         if (family == null)
39         {
40             family = (Family) db.createRoot(FamilyDbImpl.class, "The Smith Family");
41             //family = (Family) db.createRoot("org.myoodb.selfHealing.FamilyDbImpl", "The Smith Family");
42

43             family.setName("Smith");
44         }
45         else
46         {
47             System.out.println("The Smith Family already created");
48         }
49
50         Person person = (Person) db.createObject(PersonDbImpl.class);
51         //Person person = (Person) db.createObject("org.myoodb.selfHealing.PersonDbImpl");
52

53         person.setName("John Smith");
54
55         family.add(person);
56
57         // XXX: now delete "John Smith" without removing him from the Family tree
58
db.delete(person);
59
60         // XXX: now try and get "John Smith" to show the tree first entry is now "corrupted"
61
checkFamilyIntegrity(family);
62
63         // XXX: now show how to "Self Heal" this tree ( pass in current time to protect against recursion )
64
family.fixUpReference(family.getLocalTime());
65
66         // XXX: now try and get "John Smith" to show the tree first entry is no longer there
67
checkFamilyIntegrity(family);
68
69         // XXX: for how fixUpReference cleaned the tree, take a look at TreeSetDbImpl.java, but first
70
// XXX: take look at derived FamilyDbImpl.java to see what methods you might need to override.
71
}
72
73     public static void checkFamilyIntegrity(Family family)
74     {
75         try
76         {
77             Person person = (Person) family.first();
78
79             try
80             {
81                 person.getName();
82             }
83             catch (org.myoodb.exception.ObjectNotFoundException e)
84             {
85                 System.out.println(" The family tree has a bad reference (it was suppose to): " + e);
86             }
87         }
88         catch (java.util.NoSuchElementException JavaDoc e)
89         {
90             System.out.println(" The family tree should have no elements, fix up worked: " + e);
91         }
92     }
93 }
94
Popular Tags