KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > Db4o


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;
22
23 import java.io.IOException JavaDoc;
24
25 import com.db4o.config.Configuration;
26 import com.db4o.cs.*;
27 import com.db4o.ext.*;
28 import com.db4o.foundation.*;
29 import com.db4o.foundation.network.YapSocketReal;
30 import com.db4o.inside.*;
31 import com.db4o.reflect.Reflector;
32
33 /**
34  * factory class to start db4o database engines.
35  * <br><br>This class provides static methods to<br>
36  * - open single-user databases {@link #openFile(String)} <br>
37  * - open db4o servers {@link #openServer(String, int)} <br>
38  * - connect to db4o servers {@link #openClient(String, int, String, String)} <br>
39  * - provide access to the global configuration context {@link #configure()} <br>
40  * - print the version number of this db4o version {@link #main(String[])}
41  * @see ExtDb4o ExtDb4o for extended functionality.
42  *
43  * @sharpen.rename Db4oFactory
44  */

45 public class Db4o {
46     
47     static final Config4Impl i_config = new Config4Impl();
48     private static Sessions i_sessions = new Sessions();
49     
50     static {
51         Platform4.getDefaultConfiguration(i_config);
52     }
53
54     /**
55      * prints the version name of this db4o version to <code>System.out</code>.
56      */

57     public static void main(String JavaDoc args[]){
58         System.out.println(version());
59     }
60
61     /**
62      * returns the global db4o
63      * {@link Configuration Configuration} context
64      * for the running JVM session.
65      * <br><br>
66      * The {@link Configuration Configuration}
67      * can be overriden in each
68      * {@link com.db4o.ext.ExtObjectContainer#configure ObjectContainer}.<br><br>
69      * @return the global {@link Configuration configuration} context
70      */

71     public static Configuration configure(){
72         return i_config;
73     }
74     
75     /**
76      * Creates a fresh {@link Configuration Configuration} instance.
77      *
78      * @return a fresh, independent configuration with all options set to their default values
79      */

80     public static Configuration newConfiguration() {
81         Config4Impl config = new Config4Impl();
82         Platform4.getDefaultConfiguration(config);
83         return config;
84     }
85
86     /**
87      * Creates a clone of the global db4o {@link Configuration Configuration}.
88      *
89      * @return a fresh configuration with all option values set to the values
90      * currently configured for the global db4o configuration context
91      */

92     public static Configuration cloneConfiguration() {
93         return (Config4Impl) ((DeepClone) Db4o.configure()).deepClone(null);
94     }
95
96     /**
97      * Operates just like {@link Db4o#openClient(Configuration, String, int, String, String)}, but uses
98      * the global db4o {@link Configuration Configuration} context.
99      *
100      * opens an {@link ObjectContainer ObjectContainer}
101      * client and connects it to the specified named server and port.
102      * <br><br>
103      * The server needs to
104      * {@link ObjectServer#grantAccess allow access} for the specified user and password.
105      * <br><br>
106      * A client {@link ObjectContainer ObjectContainer} can be cast to
107      * {@link ExtClient ExtClient} to use extended
108      * {@link ExtObjectContainer ExtObjectContainer}
109      * and {@link ExtClient ExtClient} methods.
110      * <br><br>
111      * @param hostName the host name
112      * @param port the port the server is using
113      * @param user the user name
114      * @param password the user password
115      * @return an open {@link ObjectContainer ObjectContainer}
116      * @see ObjectServer#grantAccess
117      */

118     public static ObjectContainer openClient(String JavaDoc hostName, int port, String JavaDoc user, String JavaDoc password)
119         throws IOException JavaDoc {
120         return openClient(Db4o.cloneConfiguration(),hostName,port,user,password);
121     }
122
123     /**
124      * opens an {@link ObjectContainer ObjectContainer}
125      * client and connects it to the specified named server and port.
126      * <br><br>
127      * The server needs to
128      * {@link ObjectServer#grantAccess allow access} for the specified user and password.
129      * <br><br>
130      * A client {@link ObjectContainer ObjectContainer} can be cast to
131      * {@link ExtClient ExtClient} to use extended
132      * {@link ExtObjectContainer ExtObjectContainer}
133      * and {@link ExtClient ExtClient} methods.
134      * <br><br>
135      * @param config a custom {@link Configuration Configuration} instance to be obtained via {@link Db4o#newConfiguration()}
136      * @param hostName the host name
137      * @param port the port the server is using
138      * @param user the user name
139      * @param password the user password
140      * @return an open {@link ObjectContainer ObjectContainer}
141      * @see ObjectServer#grantAccess
142      */

143     public static ObjectContainer openClient(Configuration config,String JavaDoc hostName, int port, String JavaDoc user, String JavaDoc password)
144             throws IOException JavaDoc {
145         synchronized(Global4.lock){
146             return new YapClient(config,new YapSocketReal(hostName, port), user, password, true);
147         }
148     }
149
150     /**
151      * Operates just like {@link Db4o#openFile(Configuration, String)}, but uses
152      * the global db4o {@link Configuration Configuration} context.
153      *
154      * opens an {@link ObjectContainer ObjectContainer}
155      * on the specified database file for local use.
156      * <br><br>Subsidiary calls with the same database file name will return the same
157      * {@link ObjectContainer ObjectContainer} object.<br><br>
158      * Every call to <code>openFile()</code> requires a corresponding
159      * {@link ObjectContainer#close ObjectContainer.close}.<br><br>
160      * Database files can only be accessed for readwrite access from one process
161      * (one Java VM) at one time. All versions except for db4o mobile edition use an
162      * internal mechanism to lock the database file for other processes.
163      * <br><br>
164      * @param databaseFileName an absolute or relative path to the database file
165      * @return an open {@link ObjectContainer ObjectContainer}
166      * @see Configuration#readOnly
167      * @see Configuration#encrypt
168      * @see Configuration#password
169      */

170     public static final ObjectContainer openFile(String JavaDoc databaseFileName) throws DatabaseFileLockedException {
171         return openFile(cloneConfiguration(),databaseFileName);
172     }
173
174     /**
175      * opens an {@link ObjectContainer ObjectContainer}
176      * on the specified database file for local use.
177      * <br><br>Subsidiary calls with the same database file name will return the same
178      * {@link ObjectContainer ObjectContainer} object.<br><br>
179      * Every call to <code>openFile()</code> requires a corresponding
180      * {@link ObjectContainer#close ObjectContainer.close}.<br><br>
181      * Database files can only be accessed for readwrite access from one process
182      * (one Java VM) at one time. All versions except for db4o mobile edition use an
183      * internal mechanism to lock the database file for other processes.
184      * <br><br>
185      * @param config a custom {@link Configuration Configuration} instance to be obtained via {@link Db4o#newConfiguration()}
186      * @param databaseFileName an absolute or relative path to the database file
187      * @return an open {@link ObjectContainer ObjectContainer}
188      * @see Configuration#readOnly
189      * @see Configuration#encrypt
190      * @see Configuration#password
191      */

192     public static final ObjectContainer openFile(Configuration config,String JavaDoc databaseFileName) throws DatabaseFileLockedException {
193         synchronized(Global4.lock){
194             return i_sessions.open(config,databaseFileName);
195         }
196     }
197
198     protected static final ObjectContainer openMemoryFile1(Configuration config,MemoryFile memoryFile) {
199         synchronized(Global4.lock){
200             if(memoryFile == null){
201                 memoryFile = new MemoryFile();
202             }
203             ObjectContainer oc = null;
204             if (Deploy.debug) {
205                 System.out.println("db4o Debug is ON");
206                 oc = new YapMemoryFile(config,memoryFile);
207     
208                 // intentionally no exception handling,
209
// in order to follow uncaught errors
210
}
211             else {
212                 try {
213                     oc = new YapMemoryFile(config,memoryFile);
214                 }
215                 catch(Throwable JavaDoc t) {
216                     Messages.logErr(i_config, 4, "Memory File", t);
217                     return null;
218                 }
219             }
220             Platform4.postOpen(oc);
221             Messages.logMsg(i_config, 5, "Memory File");
222             return oc;
223         }
224     }
225     
226     
227     /**
228      * Operates just like {@link Db4o#openServer(Configuration, String, int)}, but uses
229      * the global db4o {@link Configuration Configuration} context.
230      *
231      * opens an {@link ObjectServer ObjectServer}
232      * on the specified database file and port.
233      * <br><br>
234      * If the server does not need to listen on a port because it will only be used
235      * in embedded mode with {@link ObjectServer#openClient}, specify '0' as the
236      * port number.
237      * @param databaseFileName an absolute or relative path to the database file
238      * @param port the port to be used, or 0, if the server should not open a port,
239      * because it will only be used with {@link ObjectServer#openClient()}
240      * @return an {@link ObjectServer ObjectServer} listening
241      * on the specified port.
242      * @see Configuration#readOnly
243      * @see Configuration#encrypt
244      * @see Configuration#password
245      */

246     public static final ObjectServer openServer(String JavaDoc databaseFileName, int port) throws DatabaseFileLockedException {
247         return openServer(cloneConfiguration(),databaseFileName,port);
248     }
249
250     /**
251      * opens an {@link ObjectServer ObjectServer}
252      * on the specified database file and port.
253      * <br><br>
254      * If the server does not need to listen on a port because it will only be used
255      * in embedded mode with {@link ObjectServer#openClient}, specify '0' as the
256      * port number.
257      * @param config a custom {@link Configuration Configuration} instance to be obtained via {@link Db4o#newConfiguration()}
258      * @param databaseFileName an absolute or relative path to the database file
259      * @param port the port to be used, or 0, if the server should not open a port,
260      * because it will only be used with {@link ObjectServer#openClient()}
261      * @return an {@link ObjectServer ObjectServer} listening
262      * on the specified port.
263      * @see Configuration#readOnly
264      * @see Configuration#encrypt
265      * @see Configuration#password
266      */

267     public static final ObjectServer openServer(Configuration config,String JavaDoc databaseFileName, int port) throws DatabaseFileLockedException {
268         synchronized(Global4.lock){
269             YapFile stream = (YapFile)openFile(config,databaseFileName);
270             if(stream == null){
271                 return null;
272             }
273             synchronized(stream.lock()){
274                 return new YapServer(stream, port);
275             }
276         }
277     }
278
279     static Reflector reflector(){
280         return i_config.reflector();
281     }
282     
283     static void forEachSession(Visitor4 visitor){
284         i_sessions.forEach(visitor);
285     }
286
287     static void sessionStopped(Session a_session){
288         i_sessions.remove(a_session);
289     }
290     
291     /**
292      * returns the version name of the used db4o version.
293      * <br><br>
294      * @return version information as a <code>String</code>.
295      */

296     public static final String JavaDoc version () {
297          return "db4o " + Db4oVersion.NAME;
298     }
299 }
300
Popular Tags