Source code: org/vrspace/server/db/TextDB.java
1 package org.vrspace.server.db;
2
3 import java.lang.reflect.*;
4 import java.util.*;
5 import java.io.*;
6
7 import org.vrspace.server.*;
8 import org.vrspace.util.*;
9
10 /**
11 * Database class
12 *
13 */
14 public class TextDB extends DB {
15 private String db;
16 private File file;
17 private boolean commit = false;
18
19 public String create( String fileName ) throws Exception {
20 File file = new File( fileName );
21 if ( ! file.exists() ) {
22 file.createNewFile();
23 }
24 if ( ! file.canWrite() ) {
25 throw new DBException ( "Cannot write to file " + fileName );
26 }
27 return file.getCanonicalPath();
28 }
29
30 /** Connect to file <fileName> **/
31 public void connect( String fileName )
32 throws Exception
33 {
34 db = fileName;
35 file = new File( fileName );
36 if ( ! file.exists() ) {
37 file.createNewFile();
38 }
39 if ( ! file.canWrite() ) {
40 throw new DBException ( "Cannot write to file " + fileName );
41 }
42 load( file );
43 }
44
45 /** Disconnect, commit all changes **/
46 public void disconnect()
47 {
48 try {
49 commit();
50 } catch ( Throwable t ) {
51 Logger.logError( "Error during disconect", t );
52 }
53 }
54
55 /**
56 Saves the file
57 */
58 public void commit() {
59 if ( ! commit ) {
60 commit = true;
61 try {
62 Vector objects = new Vector();
63 Collection t = cache.getTables().values();
64 Iterator iClass = t.iterator();
65 while ( iClass.hasNext() ) {
66 Collection table = ((TreeMap)iClass.next()).values();
67 Iterator iObject = table.iterator();
68 while ( iObject.hasNext() ) {
69 objects.add( iObject.next() );
70 }
71 }
72 String s = VRObject.toText( objects.toArray() );
73 FileWriter writer = new FileWriter( file );
74 writer.write( s, 0, s.length() );
75 writer.flush();
76 } catch ( Exception e ) {
77 Logger.logError( "Unable to commit database", e );
78 }
79 commit=false;
80 }
81 }
82
83 /**
84 Not implemented. This DB depends on cache and only commit() does something.
85 */
86 public Object get ( Object obj )
87 {
88 return null;
89 }
90 /**
91 Not implemented. This DB depends on cache and only commit() does something.
92 */
93 public Object[] getAll( String className ) {
94 return null;
95 }
96 /**
97 Not implemented. This DB depends on cache and only commit() does something.
98 */
99 public void delete( Object obj )
100 {
101 }
102 /**
103 Not implemented. This DB depends on cache and only commit() does something.
104 */
105 public Object get( String className, long id )
106 {
107 return null;
108 }
109
110 /**
111 Not implemented. This DB depends on cache and only commit() does something.
112 */
113 public Object get( String className, String field, Object value )
114 {
115 return null;
116 }
117
118 /**
119 Not implemented. This DB depends on cache and only commit() does something.
120 */
121 public Object[] getRange( String className, String field, Object value )
122 {
123 return new Object[0];
124 }
125 /**
126 Not implemented. This DB depends on cache and only commit() does something.
127 */
128 public Object[] getRange( Object o1, Object o2 )
129 {
130 return new Object[0];
131 }
132 /**
133 Not implemented. This DB depends on cache and only commit() does something.
134 */
135 public void put( Object obj ) {}
136 /**
137 Not implemented. This DB depends on cache and only commit() does something.
138 */
139 public void update( Request r ) {
140 }
141 }