001: /*
002: * Copyright (c) 2001 Silvere Martin-Michiellot All Rights Reserved.
003: *
004: * Silvere Martin-Michiellot grants you ("Licensee") a non-exclusive,
005: * royalty free, license to use, modify and redistribute this
006: * software in source and binary code form,
007: * provided that i) this copyright notice and license appear on all copies of
008: * the software; and ii) Licensee does not utilize the software in a manner
009: * which is disparaging to Silvere Martin-Michiellot.
010: *
011: * This software is provided "AS IS," without a warranty of any kind. ALL
012: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
013: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
014: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. Silvere Martin-Michiellot
015: * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
016: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
017: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
018: * Silvere Martin-Michiellot OR ITS LICENSORS BE LIABLE
019: * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
020: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
021: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
022: * OR INABILITY TO USE SOFTWARE, EVEN IF Silvere Martin-Michiellot HAS BEEN
023: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
024: *
025: * This software is not designed or intended for use in on-line control of
026: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
027: * the design, construction, operation or maintenance of any nuclear
028: * facility. Licensee represents and warrants that it will not use or
029: * redistribute the Software for such purposes.
030: *
031: * @Author: Silvere Martin-Michiellot
032: *
033: */
034:
035: package com.db.server;
036:
037: import java.util.*;
038: import java.io.*;
039:
040: import javax.jdo.*;
041:
042: // Prismtech JDO implementation
043: import com.prismt.j2ee.jdo.PersistenceManagerFactoryImpl;
044: import com.prismt.j2ee.jdo.*;
045:
046: //Database access
047: import java.sql.*;
048: import javax.sql.*;
049: import javax.sql.DataSource;
050: import oracle.jdbc.pool.OracleDataSource;
051:
052: /**
053: * Gives static access to a properly configured
054: * PersistenceManagerFactory. This class provides vendor-specific code
055: * necessary to bootstrap the JDO system into existence.
056: */
057: public class JDOFactory {
058:
059: // singleton factory
060: private static PersistenceManagerFactoryImpl persistenceManagerFactoryImpl;
061:
062: static {
063: DataSource dataSource;
064: Properties properties;
065: InputStream inputStream;
066: OracleDataSource oracleDataSource;
067:
068: properties = new Properties();
069: inputStream = this .getClass().getResourceAsStream(
070: "database.properties");
071: if (inputStream == null) {
072: System.out
073: .println("Could not load propeties from file \"database.properties\"");
074: System.exit(1);
075: }
076: try {
077: properties.load(inputStream);
078: } catch (IOException iOException) {
079: iOException.printStackTrace();
080: System.exit(1);
081: }
082:
083: try {
084: oracleDataSource = new oracle.jdbc.pool.OracleDataSource();
085: oracleDataSource.setUser(properties
086: .getProperty("oracle.user"));
087: oracleDataSource.setPassword(properties
088: .getProperty("oracle.password"));
089: oracleDataSource.setURL(properties
090: .getProperty("oracle.url"));
091: dataSource = oracleDataSource;
092: } catch (java.sql.SQLException sQLException) {
093: sQLException.printStackTrace();
094: System.exit(1);
095: }
096: }
097:
098: public JDOFactory() {
099:
100: persistenceManagerFactoryImpl = new PersistenceManagerFactoryImpl();
101: persistenceManagerFactoryImpl.setConnectionFactory(dataSource);
102:
103: persistenceManager = persistenceManagerFactoryImpl
104: .getPersistenceManager();
105:
106: // allow reads outside of transactions & use optimistic locking;
107: // this cuts down on database resources
108: persistenceManagerFactoryImpl.setNontransactionalRead(true);
109: persistenceManagerFactoryImpl.setRetainValues(true);
110: persistenceManagerFactoryImpl.setOptimistic(true);
111:
112: // Log SQL to a file.
113: try {
114: persistenceManagerFactoryImpl.setLogWriter(new PrintWriter(
115: new FileOutputStream("sql.txt")));
116: } catch (FileNotFoundException fileNotFoundException) {
117: }
118:
119: }
120:
121: /**
122: * @return the singleton persistence manager factory instance.
123: */
124: public static PersistenceManagerFactory getPersistenceManagerFactory() {
125:
126: return persistenceManagerFactoryImpl;
127:
128: }
129:
130: /**
131: * @return a <code>PersistenceManager</code> obtained from this
132: * class's singleton persistence manager factory.
133: */
134: public static PersistenceManager getPersistenceManager() {
135:
136: return persistenceManagerFactoryImpl.getPersistenceManager();
137:
138: }
139:
140: /**
141: * Prepare the persistence manager factory for shutdown.
142: */
143: public static void close() {
144:
145: persistenceManagerFactoryImpl.close();
146:
147: }
148:
149: }
|