01:// ObjectDB for Java Demo - JDO Directory - A Brief JDO Tutorial
02:// Copyright (C) 2001-2003, ObjectDB Software. All rights reserved.
03:
04:XXXX
05:
06:package directory;
07:
08:import java.io.File;
09:import java.util.Properties;
10:import javax.jdo.*;
11:
12:/**
13: * General JDO Utilities
14: */
15:public class Utilities {
16:
17: /**
18: * Obtains a PersistenceManager representing a database connection.
19: *
20: * @param clean indicates if a clean new database is preferred
21: */
22: public static PersistenceManager getPersistenceManager(boolean clean) {
23:
24: // Initialize JDO properties:
25: Properties jdoProperties = new Properties();
26: jdoProperties.setProperty(
27: "javax.jdo.PersistenceManagerFactoryClass", "com.objectdb.jdo.PMF");
28:
29: // By default - try accessing the database locally (without server):
30: if (Utilities.class.getResource("local.html") != null)
31: {
32: // Use relative path to get the database file:
33: File dbFile = new File(
34: Utilities.class.getResource("..").getFile(), "directory.odb");
35: jdoProperties.setProperty(
36: "javax.jdo.option.ConnectionURL", dbFile.getPath());
37:
38: // Delete old database file if exists:
39: if (clean)
40: dbFile.delete();
41: }
42:
43: // If "local.txt" does not exist - try a connection to a server running
44: // on localhost (supported only by ObjectDB Server Edition):
45: else
46: jdoProperties.setProperty("javax.jdo.option.ConnectionURL",
47: "objectdb://localhost/directory.odb");
48:
49: // Obtain a PersistenceManagerFactory and then a PersistenceManager:
50: PersistenceManagerFactory pmf =
51: JDOHelper.getPersistenceManagerFactory(jdoProperties);
52: return pmf.getPersistenceManager();
53: }
54:}
|