ObjectDB for Java/JDO Demo class directory.Utilities
|
|
|
The directory.Utilities class contains a single static method, getPersistenceManager(...), which is used by all the demo steps to obtain a PersistenceManager instance, representing a connection to the database.
directory/Utilities.java
// ObjectDB for Java Demo - JDO Directory - A Brief JDO Tutorial
// Copyright (C) 2001-2003, ObjectDB Software. All rights reserved.
package directory;
import java.io.*;
import java.util.Properties;
import javax.jdo.*;
/**
* General JDO Utilities
*/
public class Utilities {
/**
* Obtains a PersistenceManager representing a database connection.
*
* @param clean indicates if a clean new database is preferred
*/
public static PersistenceManager getPersistenceManager(boolean clean) {
try {
// Prepare the jdo.properties file for reading:
InputStream in =
Utilities.class.getResourceAsStream("jdo.properties");
try {
// Load the properties from the file:
Properties properties = new Properties();
properties.load(in);
// If requested - try to delete an old local odb file:
if (clean) {
String path = (String)properties.get(
"javax.jdo.option.ConnectionURL");
if (!path.startsWith("objectdb://")) // only local
new File(path).delete();
}
// Obtain a PersistenceManagerFactory and a PersistenceManager:
PersistenceManagerFactory pmf =
JDOHelper.getPersistenceManagerFactory(
properties, JDOHelper.class.getClassLoader());
return pmf.getPersistenceManager();
}
finally {
in.close();
}
}
// Handle errors:
catch (IOException x) {
throw new RuntimeException("Error reading jdo.properties");
}
}
}
|
The connection details are provided in a jdo.properties resource file that is loaded by the getPersistenceManager(...) method above.
directory/jdo.properties
# ObjectDB PMF class:
javax.jdo.PersistenceManagerFactoryClass=com.objectdb.jdo.PMF
# Connection URL for embedded mode:
javax.jdo.option.ConnectionURL=directory.odb
# TODO: Comment the above line to use client-server mode
# Connection URL for client-server mode:
#javax.jdo.option.ConnectionURL=objectdb://localhost/directory2.odb
# TODO: Uncomment the above line to use client-server mode
# User Details (for client-server mode):
javax.jdo.option.ConnectionUserName=admin
javax.jdo.option.ConnectionPassword=admin
|
|