001 package com.sptci.jdo;
002
003 import java.io.FileInputStream;
004 import java.util.Properties;
005 import java.util.logging.Logger;
006
007 import javax.jdo.JDOHelper;
008 import javax.jdo.PersistenceManager;
009
010 import org.rakeshv.utils.StringUtilities;
011
012 /**
013 * A factory for accessing instances of a <code>JDO Persistence
014 * Manager</code> that are necessary for interacting with the
015 * <code>JDO data store</code>.
016 *
017 * @author Rakesh Vidyadharan 05<sup><small>th</small></sup> February, 2006
018 * <p>Copyright 2006, Sans Pareil Technologies, Inc.</p>
019 *
020 * @version $Id: PersistenceManagerFactory.java,v 1.2 2006/02/14 22:41:41 rakesh Exp $
021 */
022 public final class PersistenceManagerFactory extends Object
023 {
024 /**
025 * The logger used to log messages to.
026 */
027 private static final Logger logger =
028 Logger.getLogger( "com.sptci.jdo.PersistenceManagerFactory" );
029
030 /**
031 * The name of the system property used to specify the location of
032 * the JDO DataStore connection properties.
033 */
034 private static final String JDO_DATASTORE_PROPERTIES = "JDODataStoreProperties";
035
036 /**
037 * The single application wide instanace that is used to manage
038 * read only objects.
039 */
040 private static PersistenceManager persistenceManager;
041
042 /**
043 * Initialise the {@link #persistenceManager}
044 */
045 static
046 {
047 try
048 {
049 initPersistenceManager();
050 }
051 catch ( Throwable t )
052 {
053 logger.severe(
054 "Error initialising PersistenceManager.\n" +
055 StringUtilities.stackTrace( t ) );
056 }
057 }
058
059 /**
060 * Default constructor. Cannot be initialsed.
061 */
062 private PersistenceManagerFactory() {}
063
064 /**
065 * Return an instance of a <code>PersistenceManager</code> for
066 * interacting with the <code>JDO data store</code>.
067 *
068 * @see #initPersistenceManager
069 * @throws RuntimeException If errors are encountered while reading
070 * the properties file
071 */
072 public static final javax.jdo.PersistenceManager getPersistenceManager()
073 throws RuntimeException
074 {
075 if ( persistenceManager.isClosed() )
076 {
077 initPersistenceManager();
078 }
079
080 return persistenceManager;
081 }
082
083 /**
084 * Initialise the {@link #persistenceManager} if it has not already
085 * been initialised, or has been closed.
086 *
087 * @see #fetchProperties
088 * @throws RuntimeException If errors are encountered while reading
089 * the properties file while initialising the persistence
090 * manager.
091 */
092 private synchronized static final void initPersistenceManager()
093 throws RuntimeException
094 {
095 logger.info(
096 "Initialising persistence manager" );
097 PersistenceManager pm = JDOHelper.getPersistenceManagerFactory(
098 fetchProperties() ).getPersistenceManager();
099 persistenceManager = pm;
100 }
101
102 /**
103 * Open the properties file and read the properties from it.
104 * The full path to the properties file must be specified as a
105 * system property with name <code>ObjectDBProperties</code>
106 *
107 * @see #JDO_DATASTORE_PROPERTIES
108 * @return Properties - The initialised properties object.
109 * @throws RuntimeException If errors are encountered while reading
110 * the properties file
111 */
112 private static final Properties fetchProperties() throws RuntimeException
113 {
114 Properties properties = new Properties();
115
116 try
117 {
118 FileInputStream fis = new FileInputStream(
119 System.getProperties().getProperty( JDO_DATASTORE_PROPERTIES ) );
120 properties.load( fis );
121 fis.close();
122 }
123 catch ( Throwable t )
124 {
125 throw new RuntimeException( t.getMessage(), t );
126 }
127
128 return properties;
129 }
130 }