01:// ObjectDB for Java/JDO - The Guest Book - A Mimimal JDO Based Web Application
02:// Copyright (C) 2001-2003, ObjectDB Software. All rights reserved.
03:
04:XXXX
05:package guestbook;
06:
07:import java.io.File;
08:import java.util.Properties;
09:import javax.jdo.*;
10:import javax.servlet.*;
11:
12:/**
13: * This class handles JDO enhancement and PersistenceManager allocations.
14: */
15:public class WebAppMgr {
16:
17: /**
18: * Ensures on the fly JDO enhancement of persistence capable classes.
19: * NOTE: NO PERSISTENCE CAPABLE CLASS IS MENTIONED IN THIS CLASS!
20: * (otherwise classes might be loaded into JVM before enhancement).
21: */
22: public static void enhanceAll() {
23: if (!enhanced) {
24: com.objectdb.Enhancer.enhance("guestbook.pc.*");
25: enhanced = true;
26: }
27: }
28: private static boolean enhanced; // indicates if already been done
29:
30: /**
31: * Obtains a PersistenceManager instance.
32: */
33: public static PersistenceManager getPersistenceManager(
34: ServletContext context) {
35:
36: // Prepare an application scope PersistenceManagerFactory when needed:
37: if (pmf == null) {
38: Properties properties = new Properties();
39: properties.setProperty(
40: "javax.jdo.PersistenceManagerFactoryClass",
41: "com.objectdb.jdo.PMF" // always the same for ObjectDB
42: );
43: properties.setProperty(
44: "javax.jdo.option.ConnectionURL",
45: context.getRealPath("/WEB-INF/db/guestbook.odb")
46: // path is relative to web application root
47: );
48: pmf = JDOHelper.getPersistenceManagerFactory(properties);
49: }
50:
51: // Return a request scope PersistenceManager instance:
52: return pmf.getPersistenceManager();
53: }
54: private static PersistenceManagerFactory pmf; // holds a global PMF
55:}
|