0001: /**
0002: * Copyright (C) 2006, 2007 David Bulmore, Software Sensation Inc.
0003: * All Rights Reserved.
0004: *
0005: * This file is part of JPersist.
0006: *
0007: * JPersist is free software; you can redistribute it and/or modify it under
0008: * the terms of the GNU General Public License (Version 2) as published by
0009: * the Free Software Foundation.
0010: *
0011: * JPersist is distributed in the hope that it will be useful, but WITHOUT
0012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0014: * for more details.
0015: *
0016: * You should have received a copy of the GNU General Public License
0017: * along with JPersist; if not, write to the Free Software Foundation,
0018: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
0019: */package jpersist;
0020:
0021: import javax.naming.NamingException;
0022: import jcommontk.utils.XMLParser;
0023: import java.io.IOException;
0024: import java.net.URL;
0025: import java.sql.Connection;
0026: import java.sql.DriverManager;
0027: import java.util.HashMap;
0028: import java.util.Map;
0029: import java.util.Vector;
0030: import java.util.logging.Level;
0031: import java.util.logging.Logger;
0032: import javax.naming.InitialContext;
0033: import javax.sql.DataSource;
0034: import jcommontk.utils.StringUtils;
0035: import jcommontk.utils.XMLParserException;
0036: import java.util.Collection;
0037: import java.util.Iterator;
0038: import java.util.Set;
0039: import jcommontk.utils.LoggerUtils;
0040: import jpersist.interfaces.ColumnMapping;
0041: import jpersist.interfaces.TableMapping;
0042: import jpersist.utils.ResultSetUtils;
0043: import org.w3c.dom.Element;
0044: import org.w3c.dom.NodeList;
0045:
0046: /**
0047: * The DatabaseManager provides pooling and management for database handlers and
0048: * connection instances. It also provides access to XML defined databases,
0049: * logging, sql statements, and object-oriented database access. An application
0050: * can have any number of DatabaseManagers, but only one is typically needed.
0051: */
0052:
0053: @SuppressWarnings("unchecked")
0054: // working to complete a Java 1.5 version
0055: public class DatabaseManager {
0056: private static final int CONNECTION_SOURCE_IS_UNDEFINED = 0;
0057: private static final int CONNECTION_SOURCE_IS_JNDI = 1;
0058: private static final int CONNECTION_SOURCE_IS_DATA_SOURCE = 2;
0059: private static final int CONNECTION_SOURCE_IS_DRIVER_MANAGER = 3;
0060:
0061: private static Logger logger = Logger
0062: .getLogger(DatabaseManager.class.getName());
0063:
0064: private static Map dbDefinitionsHash = new HashMap();
0065:
0066: private String databaseName, databaseDriver, databaseUrl,
0067: databaseUsername, databasePassword, catalogPattern,
0068: schemaPattern;
0069: private int maxPoolSize, databasesAllocated,
0070: connectionSourceType = CONNECTION_SOURCE_IS_UNDEFINED;
0071: private Vector connectionsVector = new Vector(),
0072: databaseFreePool = new Vector();
0073: private TableMapping tableMapper;
0074: private ColumnMapping columnMapper;
0075: private DataSource dataSource;
0076: private Map sqlStatements;
0077: private boolean isClosed;
0078:
0079: static {
0080: Logger logger = Logger.getLogger("jpersist");
0081:
0082: logger.setUseParentHandlers(false);
0083: logger.addHandler(new LoggerUtils.OutputStreamHandler(
0084: System.out, true, true));
0085: logger.setLevel(Level.FINER);
0086:
0087: try {
0088: URL url = ClassLoader.getSystemResource("databases.xml");
0089: boolean validate = false;
0090:
0091: if (url == null) {
0092: url = ClassLoader.getSystemResource("databases_v.xml");
0093: validate = true;
0094: }
0095:
0096: if (url != null)
0097: new LoadXMLDefinition(url, validate);
0098: } catch (Exception e) {
0099: logger.log(Level.SEVERE, e.toString(), e);
0100: }
0101: }
0102:
0103: static class LoadXMLDefinition extends XMLParser {
0104: LoadXMLDefinition(URL fileUrl, boolean validate)
0105: throws IOException, XMLParserException {
0106: super (fileUrl, validate);
0107: }
0108:
0109: public void processXML(Element e) {
0110: NodeList nl = e.getElementsByTagName("database");
0111:
0112: for (int i = 0; i < nl.getLength(); i++) {
0113: Element e2 = (Element) nl.item(i);
0114:
0115: String name = StringUtils.emptyToDefault(e2
0116: .getAttribute("name"), null), useJndi = StringUtils
0117: .emptyToDefault(e2.getAttribute("useJndi"),
0118: null), poolSize = StringUtils
0119: .emptyToDefault(e2.getAttribute("poolSize"),
0120: "10"), driver = StringUtils
0121: .emptyToDefault(e2.getAttribute("driver"), null), url = StringUtils
0122: .emptyToDefault(e2.getAttribute("url"), null), catalogPattern = StringUtils
0123: .emptyToDefault(e2
0124: .getAttribute("catalogPattern"), null), schemaPattern = StringUtils
0125: .emptyToDefault(e2
0126: .getAttribute("schemaPattern"), null), user = StringUtils
0127: .emptyToDefault(e2.getAttribute("username"),
0128: null), password = StringUtils
0129: .emptyToDefault(e2.getAttribute("password"),
0130: null);
0131:
0132: DefinedDatabase dd = new DefinedDatabase(name, driver,
0133: url, catalogPattern, schemaPattern, user,
0134: password, new Integer(poolSize).intValue(),
0135: useJndi == null ? false : new Boolean(useJndi)
0136: .booleanValue());
0137: dbDefinitionsHash.put(name, dd);
0138: logger.config(dd.toString());
0139: }
0140: }
0141: }
0142:
0143: static class DefinedDatabase {
0144: String name, driver, url, catalogPattern, schemaPattern, user,
0145: password;
0146: int poolSize;
0147: boolean useJndi;
0148:
0149: DefinedDatabase(String name, String driver, String url,
0150: String catalogPattern, String schemaPattern,
0151: String user, String password, int poolSize,
0152: boolean useJndi) {
0153: this .name = name;
0154: this .driver = driver;
0155: this .url = url;
0156: this .catalogPattern = catalogPattern;
0157: this .schemaPattern = schemaPattern;
0158: this .user = user;
0159: this .password = password;
0160: this .poolSize = poolSize;
0161: this .useJndi = useJndi;
0162: }
0163:
0164: String getName() {
0165: return name;
0166: }
0167:
0168: String getDriver() {
0169: return driver;
0170: }
0171:
0172: String getUrl() {
0173: return url;
0174: }
0175:
0176: String getCatalogPattern() {
0177: return catalogPattern;
0178: }
0179:
0180: String getSchemaPattern() {
0181: return schemaPattern;
0182: }
0183:
0184: String getUsername() {
0185: return user;
0186: }
0187:
0188: String getPassword() {
0189: return password;
0190: }
0191:
0192: int getPoolSize() {
0193: return poolSize;
0194: }
0195:
0196: boolean useJndi() {
0197: return useJndi;
0198: }
0199:
0200: public String toString() {
0201: if (useJndi)
0202: return "Name: " + name + ", poolsize = " + poolSize
0203: + ", useJndi = true, url = " + url
0204: + ", catalogPattern = " + catalogPattern
0205: + ", schemaPattern = " + schemaPattern
0206: + ", user = " + user + ", password = "
0207: + password;
0208: else
0209: return "Name: " + name + ", poolsize = " + poolSize
0210: + ", driver = " + driver + ", url = " + url
0211: + ", catalogPattern = " + catalogPattern
0212: + ", schemaPattern = " + schemaPattern
0213: + ", user = " + user + ", password = "
0214: + password;
0215: }
0216: }
0217:
0218: /**
0219: * Returns a DatabaseManager instance as defined in the databases.xml file. The format
0220: * of the databases file, which is found via ClassLoader.getSystemResource(), is:
0221: *
0222: * <pre>
0223: * <databases>
0224: * <database name="" useJndi="" url="" [poolSize=""] [catalogPattern=""] [schemaPattern=""] [user=""] [password=""] />
0225: * <database name="" driver="" url="" [poolSize=""] [catalogPattern=""] [schemaPattern=""] [user=""] [password=""] />
0226: * </databases>
0227: * </pre>
0228: *
0229: *
0230: * @param dbName is the name defined in the database element
0231: * @return an instance os DatabaseManager, or null if not defined
0232: * @throws JPersistException
0233: */
0234:
0235: public static DatabaseManager getXmlDefinedDatabaseManager(
0236: String dbName) throws JPersistException {
0237: DefinedDatabase definedDatabase = (DefinedDatabase) dbDefinitionsHash
0238: .get(dbName);
0239:
0240: if (logger.isLoggable(Level.FINER))
0241: if (definedDatabase != null)
0242: logger.finer("Defined database found: "
0243: + definedDatabase);
0244: else
0245: logger.finer("Defined database was not found: name = "
0246: + dbName);
0247:
0248: if (definedDatabase != null) {
0249: if (definedDatabase.useJndi())
0250: return new DatabaseManager(dbName, definedDatabase
0251: .getPoolSize(), definedDatabase.getUrl(),
0252: definedDatabase.getCatalogPattern(),
0253: definedDatabase.getSchemaPattern());
0254: else {
0255: if (definedDatabase.getUsername() == null)
0256: return new DatabaseManager(dbName, definedDatabase
0257: .getPoolSize(),
0258: definedDatabase.getDriver(),
0259: definedDatabase.getUrl(), definedDatabase
0260: .getCatalogPattern(),
0261: definedDatabase.getSchemaPattern());
0262: else
0263: return new DatabaseManager(dbName, definedDatabase
0264: .getPoolSize(),
0265: definedDatabase.getDriver(),
0266: definedDatabase.getUrl(), definedDatabase
0267: .getCatalogPattern(),
0268: definedDatabase.getSchemaPattern(),
0269: definedDatabase.getUsername(),
0270: definedDatabase.getPassword());
0271: }
0272: }
0273:
0274: throw new JPersistException("Defined Database '" + dbName
0275: + "' not found");
0276: }
0277:
0278: /**
0279: * Create a DatabaseManager instance using JNDI.
0280: *
0281: * @param databaseName the name to associate with the DatabaseManager instance
0282: * @param poolSize the number of instances to manage
0283: * @param jndiUri the JNDI URI
0284: * @param catalogPattern the catalogPattern (can contain SQL wildcards)
0285: * @param schemaPattern the schemaPattern (can contain SQL wildcards)
0286: */
0287:
0288: public static DatabaseManager getJndiDefinedDatabaseManager(
0289: String databaseName, int poolSize, String jndiUri,
0290: String catalogPattern, String schemaPattern) {
0291: return new DatabaseManager(databaseName, poolSize, jndiUri,
0292: catalogPattern, schemaPattern);
0293: }
0294:
0295: /**
0296: * Create a DatabaseManager instance using JNDI.
0297: *
0298: * @param databaseName the name to associate with the DatabaseManager instance
0299: * @param poolSize the number of instances to manage
0300: * @param jndiUri the JNDI URI
0301: * @param catalogPattern the catalogPattern (can contain SQL wildcards)
0302: * @param schemaPattern the schemaPattern (can contain SQL wildcards)
0303: * @param username the username to use for signon
0304: * @param password the password to use for signon
0305: */
0306:
0307: public static DatabaseManager getJndiDefinedDatabaseManager(
0308: String databaseName, int poolSize, String jndiUri,
0309: String catalogPattern, String schemaPattern,
0310: String username, String password) {
0311: return new DatabaseManager(databaseName, poolSize, jndiUri,
0312: catalogPattern, schemaPattern, username, password);
0313: }
0314:
0315: /**
0316: * Create a DatabaseManager instance using a supplied database driver.
0317: *
0318: * @param databaseName the name to associate with the DatabaseManager instance
0319: * @param poolSize the number of instances to manage
0320: * @param driver the database driver class name
0321: * @param url the driver oriented database url
0322: * @param catalogPattern the catalogPattern (can contain SQL wildcards)
0323: * @param schemaPattern the schemaPattern (can contain SQL wildcards)
0324: */
0325:
0326: public static DatabaseManager getUrlDefinedDatabaseManager(
0327: String databaseName, int poolSize, String driver,
0328: String url, String catalogPattern, String schemaPattern) {
0329: return new DatabaseManager(databaseName, poolSize, driver, url,
0330: catalogPattern, schemaPattern);
0331: }
0332:
0333: /**
0334: * Create a DatabaseManager instance using a supplied database driver.
0335: *
0336: * @param databaseName the name to associate with the DatabaseManager instance
0337: * @param poolSize the number of instances to manage
0338: * @param driver the database driver class name
0339: * @param url the driver oriented database url
0340: * @param catalogPattern the catalogPattern (can contain SQL wildcards)
0341: * @param schemaPattern the schemaPattern (can contain SQL wildcards)
0342: * @param username the username to use for signon
0343: * @param password the password to use for signon
0344: */
0345:
0346: public static DatabaseManager getUrlDefinedDatabaseManager(
0347: String databaseName, int poolSize, String driver,
0348: String url, String catalogPattern, String schemaPattern,
0349: String username, String password) {
0350: return new DatabaseManager(databaseName, poolSize, driver, url,
0351: catalogPattern, schemaPattern, username, password);
0352: }
0353:
0354: /**
0355: * Create a DatabaseManager instance using a supplied DataSource.
0356: *
0357: * @param databaseName the name to associate with the DatabaseManager instance
0358: * @param poolSize the number of instances to manage
0359: * @param dataSource the data source that supplies connections
0360: * @param catalogPattern the catalogPattern (can contain SQL wildcards)
0361: * @param schemaPattern the schemaPattern (can contain SQL wildcards)
0362: */
0363:
0364: public static DatabaseManager getDataSourceDatabaseManager(
0365: String databaseName, int poolSize, DataSource dataSource,
0366: String catalogPattern, String schemaPattern) {
0367: return new DatabaseManager(databaseName, poolSize, dataSource,
0368: catalogPattern, schemaPattern);
0369: }
0370:
0371: private DatabaseManager(String databaseName, int poolSize,
0372: DataSource dataSource, String catalogPattern,
0373: String schemaPattern) {
0374: if (logger.isLoggable(Level.FINER))
0375: logger.finer("Creating new DatabaseManager: name = "
0376: + databaseName + ", poolSize = " + poolSize
0377: + ", catalogPattern = " + catalogPattern
0378: + ", schemaPattern = " + schemaPattern);
0379:
0380: this .databaseName = databaseName;
0381: this .maxPoolSize = poolSize;
0382: this .dataSource = dataSource;
0383: this .catalogPattern = catalogPattern;
0384: this .schemaPattern = schemaPattern;
0385: this .connectionSourceType = CONNECTION_SOURCE_IS_DATA_SOURCE;
0386: }
0387:
0388: private DatabaseManager(String databaseName, int poolSize,
0389: String jndiUri, String catalogPattern, String schemaPattern) {
0390: if (logger.isLoggable(Level.FINER))
0391: logger.finer("Creating new DatabaseManager: name = "
0392: + databaseName + ", poolSize = " + poolSize
0393: + ", jndiUrl = " + jndiUri + ", catalogPattern = "
0394: + catalogPattern + ", schemaPattern = "
0395: + schemaPattern);
0396:
0397: this .databaseName = databaseName;
0398: this .maxPoolSize = poolSize;
0399: this .databaseUrl = jndiUri;
0400: this .catalogPattern = catalogPattern;
0401: this .schemaPattern = schemaPattern;
0402: this .connectionSourceType = CONNECTION_SOURCE_IS_JNDI;
0403: }
0404:
0405: private DatabaseManager(String databaseName, int poolSize,
0406: String jndiUri, String catalogPattern,
0407: String schemaPattern, String username, String password) {
0408: if (logger.isLoggable(Level.FINER))
0409: logger.finer("Creating new DatabaseManager: name = "
0410: + databaseName + ", poolSize = " + poolSize
0411: + ", jndiUrl = " + jndiUri + ", catalogPattern = "
0412: + catalogPattern + ", schemaPattern = "
0413: + schemaPattern + ", username = " + username
0414: + ", password = " + password);
0415:
0416: this .databaseName = databaseName;
0417: this .maxPoolSize = poolSize;
0418: this .databaseUrl = jndiUri;
0419: this .catalogPattern = catalogPattern;
0420: this .schemaPattern = schemaPattern;
0421: this .databaseUsername = username;
0422: this .databasePassword = password;
0423: this .connectionSourceType = CONNECTION_SOURCE_IS_JNDI;
0424: }
0425:
0426: private DatabaseManager(String databaseName, int poolSize,
0427: String driver, String url, String catalogPattern,
0428: String schemaPattern) {
0429: if (logger.isLoggable(Level.FINER))
0430: logger.finer("Creating new DatabaseManager: name = "
0431: + databaseName + ", poolSize = " + poolSize
0432: + ", driver = " + driver + ", url = " + url
0433: + ", catalogPattern = " + catalogPattern
0434: + ", schemaPattern = " + schemaPattern);
0435: this .databaseName = databaseName;
0436: this .maxPoolSize = poolSize;
0437: this .databaseDriver = driver;
0438: this .databaseUrl = url;
0439: this .catalogPattern = catalogPattern;
0440: this .schemaPattern = schemaPattern;
0441: this .connectionSourceType = CONNECTION_SOURCE_IS_DRIVER_MANAGER;
0442: }
0443:
0444: private DatabaseManager(String databaseName, int poolSize,
0445: String driver, String url, String catalogPattern,
0446: String schemaPattern, String username, String password) {
0447: if (logger.isLoggable(Level.FINER))
0448: logger.finer("Creating new DatabaseManager: name = "
0449: + databaseName + ", poolSize = " + poolSize
0450: + ", driver = " + driver + ", url = " + url
0451: + ", catalogPattern = " + catalogPattern
0452: + ", schemaPattern = " + schemaPattern
0453: + ", username = " + username + ", password = "
0454: + password);
0455: this .databaseName = databaseName;
0456: this .maxPoolSize = poolSize;
0457: this .databaseDriver = driver;
0458: this .databaseUrl = url;
0459: this .catalogPattern = catalogPattern;
0460: this .schemaPattern = schemaPattern;
0461: this .databaseUsername = username;
0462: this .databasePassword = password;
0463: this .connectionSourceType = CONNECTION_SOURCE_IS_DRIVER_MANAGER;
0464: }
0465:
0466: /**
0467: * Returns the database name.
0468: * @return the database name
0469: */
0470:
0471: public String getDatabaseName() {
0472: return databaseName;
0473: }
0474:
0475: /**
0476: * Closes all resources associated with the DatabaseManager. If the DatabaseManager
0477: * is managing pooled connections via JNDI, then this method does nothing. However,
0478: * if the connections are allocated by the manager (non-JNDI), they will be closed.
0479: */
0480: public void close() throws JPersistException {
0481: if (!isClosed)
0482: try {
0483: if (getDatabase().getConnection().getMetaData()
0484: .getURL().indexOf("hsql") != -1)
0485: getDatabase().executeUpdate("shutdown;");
0486:
0487: if (connectionSourceType == CONNECTION_SOURCE_IS_DRIVER_MANAGER)
0488: for (Iterator it = connectionsVector.iterator(); it
0489: .hasNext();)
0490: ((Connection) it.next()).close();
0491:
0492: isClosed = true;
0493: } catch (Exception e) {
0494: throw new JPersistException(e);
0495: }
0496: }
0497:
0498: /**
0499: * Closes the database manager (simply calls close()), can be used in EL. For example:
0500: *
0501: * <pre>
0502: * ${dbm.close}
0503: * </pre>
0504: *
0505: * @throws JPersistException
0506: */
0507:
0508: public String getClose() throws JPersistException {
0509: close();
0510:
0511: return null;
0512: }
0513:
0514: /**
0515: * Closes the database manager (simply calls close()), via a JavaBeans setter. For example:
0516: * <pre>
0517: * <jsp:useBean id="dbm" scope="request" class="jpersist.DatabaseManager" />
0518: * <jsp:setProperty name="dbm" property="closed" value="true"/>
0519: * </pre>
0520: *
0521: * @throws JPersistException
0522: */
0523:
0524: public void setClosed(boolean true_only) throws JPersistException {
0525: close();
0526: }
0527:
0528: /**
0529: * Returns true if the database manager is closed, false otherwise.
0530: *
0531: * @return true or false
0532: */
0533:
0534: public boolean isClosed() {
0535: return isClosed;
0536: }
0537:
0538: /**
0539: * Sets the logging level to the given level.
0540: *
0541: * @param level an instance of java.util.logging.Level
0542: */
0543:
0544: public static void setLogLevel(Level level) {
0545: logger.info("Setting log level to " + level.getName());
0546: Logger.getLogger("jpersist").setLevel(level);
0547: }
0548:
0549: /**
0550: * Returns the database meta data associated with the current database.
0551: * The MetaData class can be used to access information about tables in
0552: * the database. It can also be used to add table and column mapping.
0553: *
0554: * @return an instance of MetaData
0555: *
0556: * @throws JPersistException
0557: */
0558:
0559: public MetaData getMetaData() throws JPersistException {
0560: try {
0561: Database db = getDatabase();
0562:
0563: try {
0564: return db.getMetaData();
0565: } finally {
0566: db.close();
0567: }
0568: } catch (Exception e) {
0569: throw new JPersistException(e);
0570: }
0571: }
0572:
0573: /**
0574: * Sets limits on the meta data table information returned. Defining
0575: * catalogPattern and schemaPattern can help reduce the amount of time
0576: * spent loading table information. With some databases, it is absolutely
0577: * needed. This can also be set with databases.xml and/or DatabaseManager
0578: * constructors.
0579: *
0580: * @param catalogPattern the catalogPattern (can contain SQL wildcards)
0581: * @param schemaPattern the schemaPattern (can contain SQL wildcards)
0582: */
0583:
0584: public void setMetaDataLimits(String catalogPattern,
0585: String schemaPattern) {
0586: logger.fine("Limiting meta data with catalog = "
0587: + catalogPattern + ", schema = " + schemaPattern);
0588:
0589: this .catalogPattern = catalogPattern;
0590: this .schemaPattern = schemaPattern;
0591: }
0592:
0593: /**
0594: * This method can be used to add global table and column mappers.
0595: * Objects implementing TableMapping and/or ColumnMapping override the global mappers.
0596: *
0597: * @param tableMapper an instance of TableMapping
0598: * @param columnMapper an instance of ColumnMapping
0599: */
0600: public void setGlobalMappers(TableMapping tableMapper,
0601: ColumnMapping columnMapper) {
0602: this .tableMapper = tableMapper;
0603: this .columnMapper = columnMapper;
0604: }
0605:
0606: /**
0607: * Returns an instance of the defined database.
0608: *
0609: * @return an instance of Database
0610: * @throws JPersistException
0611: */
0612:
0613: public synchronized Database getDatabase() throws JPersistException {
0614: try {
0615: Database db = null;
0616: Vector freePool = getFreePool();
0617:
0618: if (logger.isLoggable(Level.FINER))
0619: logger.finer("Retrieving database");
0620:
0621: if (freePool.size() > 0) {
0622: db = (Database) freePool.remove(0);
0623:
0624: if (logger.isLoggable(Level.FINE))
0625: logger.fine("Database allocated from free pool");
0626: } else if (freePool.size() < maxPoolSize) {
0627: db = new Database(this ); //(Database)databaseClass.getDeclaredConstructor(new Class[] { DatabaseManager.class }).newInstance(new Object[] { this });
0628: db.setDatabaseName(databaseName);
0629:
0630: if (connectionSourceType == CONNECTION_SOURCE_IS_DRIVER_MANAGER) {
0631: Class.forName(databaseDriver);
0632:
0633: Connection connection = null;
0634:
0635: if (databaseUsername != null)
0636: connection = DriverManager.getConnection(
0637: databaseUrl, databaseUsername,
0638: databasePassword);
0639: else
0640: connection = DriverManager
0641: .getConnection(databaseUrl);
0642:
0643: db.setConnection(connection);
0644: connectionsVector.add(connection);
0645: }
0646:
0647: if (logger.isLoggable(Level.FINE))
0648: logger
0649: .fine("Database added to pool; size is now = "
0650: + freePool.size()
0651: + ", max size = "
0652: + maxPoolSize);
0653: } else
0654: throw new JPersistException("Database pool is empty");
0655:
0656: if (connectionSourceType != CONNECTION_SOURCE_IS_DRIVER_MANAGER) {
0657: try {
0658: if (connectionSourceType == CONNECTION_SOURCE_IS_JNDI) {
0659: if (databaseUsername != null)
0660: db
0661: .setConnection(((DataSource) new InitialContext()
0662: .lookup("java:comp/env/"
0663: + databaseUrl))
0664: .getConnection(
0665: databaseUsername,
0666: databasePassword));
0667: else
0668: db
0669: .setConnection(((DataSource) new InitialContext()
0670: .lookup("java:comp/env/"
0671: + databaseUrl))
0672: .getConnection());
0673: } else if (connectionSourceType == CONNECTION_SOURCE_IS_DATA_SOURCE)
0674: db.setConnection(dataSource.getConnection());
0675: else
0676: throw new JPersistException(
0677: "connectionSourceType = "
0678: + connectionSourceType);
0679: } catch (NamingException ex) {
0680: throw new JPersistException("Error for database = "
0681: + databaseUrl, ex);
0682: }
0683: }
0684:
0685: if (logger.isLoggable(Level.FINE))
0686: logger.fine("Databases allocated = "
0687: + ++databasesAllocated);
0688:
0689: db.initDatabase();
0690: db.setMetaDataLimits(catalogPattern, schemaPattern);
0691: db.setGlobalMappers(tableMapper, columnMapper);
0692:
0693: return db;
0694: } catch (Exception e) {
0695: throw new JPersistException(e);
0696: }
0697: }
0698:
0699: Vector getFreePool() {
0700: return databaseFreePool;
0701: }
0702:
0703: synchronized void releaseDatabase(Database db) {
0704: if (connectionSourceType != CONNECTION_SOURCE_IS_DRIVER_MANAGER)
0705: try {
0706: db.getConnection().close();
0707: } catch (Exception e) {
0708: logger.log(Level.SEVERE, e.toString(), e);
0709: }
0710:
0711: Vector freePool = getFreePool();
0712:
0713: freePool.addElement(db);
0714:
0715: if (logger.isLoggable(Level.FINE)) {
0716: logger
0717: .fine("Database added back to free pool; size is now = "
0718: + freePool.size());
0719: logger
0720: .fine("Databases allocated = "
0721: + --databasesAllocated);
0722: }
0723: }
0724:
0725: /**
0726: * Loads SQL statements based on a supplied query. The SQL statements are
0727: * loaded into a hash table based on column 1 (id/key column) and column 2
0728: * (SQL statement/value column) of your SQL query.
0729: *
0730: * @param sql the SQL statement that loads the statements (e.g. "select sql_id, sql_statement from sql_statements")
0731: *
0732: * @throws JPersistException
0733: */
0734:
0735: public void loadSQLStatements(String sql) throws JPersistException {
0736: Database db = getDatabase();
0737:
0738: try {
0739: sqlStatements = ResultSetUtils.loadMap(db.executeQuery(sql)
0740: .getResultSet(), new HashMap());
0741: } catch (Exception e) {
0742: throw new JPersistException(e);
0743: } finally {
0744: db.close();
0745: }
0746: }
0747:
0748: /**
0749: * Set the map that sql statements are retrieved from (used in place of loadSQLStatements()).
0750: *
0751: * @param sqlStatements the SQL statements map that sql statements are retrieved from
0752: *
0753: * @throws JPersistException
0754: */
0755:
0756: public void setSQLStatements(Map sqlStatements)
0757: throws JPersistException {
0758: this .sqlStatements = sqlStatements;
0759: }
0760:
0761: /**
0762: * Returns the SQL statement associated with the SQL Id (key column from loadSQLStatements()).
0763: *
0764: * @param key the sql id
0765: *
0766: * @return the SQL statement associated with the key (SQL Id)
0767: *
0768: * @throws JPersistException
0769: */
0770:
0771: public String getSQLStatement(String key) throws JPersistException {
0772: if (sqlStatements == null)
0773: throw new JPersistException(
0774: "SQL Statements have not been initialized");
0775:
0776: return (String) sqlStatements.get(key);
0777: }
0778:
0779: /**
0780: * Builds a select query from a class that matches up to a table, and then loads the
0781: * object (using set methods that match columns in a table matched to the class name)
0782: * with the result.
0783: *
0784: * <p>This is a one line convienence method for:
0785: * <pre>
0786: * Database db = getDatabase();
0787: *
0788: * try
0789: * {
0790: * Result result = db.queryObject(object);
0791: *
0792: * if (result.hasNext())
0793: * return result.next(object);
0794: * }
0795: * finally
0796: * {
0797: * db.close();
0798: * }
0799: * </pre>
0800: *
0801: * @param object the object to load
0802: *
0803: * @return returns the object passed in
0804: *
0805: * @throws JPersistException
0806: */
0807:
0808: public <T> T loadObject(T object) throws JPersistException {
0809: return loadObject(object, true, null, null, (Object[]) null);
0810: }
0811:
0812: /**
0813: * Builds a select query from a class that matches up to a table, and then loads the
0814: * object (using set methods that match columns in a table matched to the class name)
0815: * with the result.
0816: *
0817: * <p>This is a one line convienence method for:
0818: * <pre>
0819: * Database db = getDatabase();
0820: *
0821: * try
0822: * {
0823: * Result result = db.queryObject(object);
0824: *
0825: * if (result.hasNext())
0826: * return result.next(object, loadAssociations);
0827: * }
0828: * finally
0829: * {
0830: * db.close();
0831: * }
0832: * </pre>
0833: *
0834: * @param object the object to load
0835: * @param loadAssociations true to load associations
0836: *
0837: * @return returns the object passed in
0838: *
0839: * @throws JPersistException
0840: */
0841:
0842: public <T> T loadObject(T object, boolean loadAssociations)
0843: throws JPersistException {
0844: return loadObject(object, loadAssociations, null, null,
0845: (Object[]) null);
0846: }
0847:
0848: /**
0849: * Builds a select query from a class that matches up to a table, and then loads the
0850: * object (using set methods that match columns in a table matched to the class name)
0851: * with the result.
0852: *
0853: * <p>This is a one line convienence method for:
0854: * <pre>
0855: * Database db = getDatabase();
0856: *
0857: * try
0858: * {
0859: * Result result = db.queryObject(object, nullValuesToInclude);
0860: *
0861: * if (result.hasNext())
0862: * return result.next(object);
0863: * }
0864: * finally
0865: * {
0866: * db.close();
0867: * }
0868: * </pre>
0869: *
0870: * @param object the object to load
0871: * @param nullValuesToInclude is a Set of set methods without the 'set', or table column names, to include in the where clause if null
0872: *
0873: * @return returns the object passed in
0874: *
0875: * @throws JPersistException
0876: */
0877:
0878: public <T> T loadObject(T object, Set<String> nullValuesToInclude)
0879: throws JPersistException {
0880: return loadObject(object, true, nullValuesToInclude, null,
0881: (Object[]) null);
0882: }
0883:
0884: /**
0885: * Builds a select query from a class that matches up to a table, and then loads the
0886: * object (using set methods that match columns in a table matched to the class name)
0887: * with the result.
0888: *
0889: * <p>This is a one line convienence method for:
0890: * <pre>
0891: * Database db = getDatabase();
0892: *
0893: * try
0894: * {
0895: * Result result = db.queryObject(object, nullValuesToInclude);
0896: *
0897: * if (result.hasNext())
0898: * return result.next(object, loadAssociations);
0899: * }
0900: * finally
0901: * {
0902: * db.close();
0903: * }
0904: * </pre>
0905: *
0906: * @param object the object to load
0907: * @param loadAssociations true to load associations
0908: * @param nullValuesToInclude is a Set of set methods without the 'set', or table column names, to include in the where clause if null
0909: *
0910: * @return returns the object passed in
0911: *
0912: * @throws JPersistException
0913: */
0914:
0915: public <T> T loadObject(T object, boolean loadAssociations,
0916: Set<String> nullValuesToInclude) throws JPersistException {
0917: return loadObject(object, loadAssociations,
0918: nullValuesToInclude, null, (Object[]) null);
0919: }
0920:
0921: /**
0922: * Builds a select query from a class that matches up to a table, and then loads the
0923: * object (using set methods that match columns in a table matched to the class name)
0924: * with the result.
0925: *
0926: * <p>This is a one line convienence method for:
0927: * <pre>
0928: * Database db = getDatabase();
0929: *
0930: * try
0931: * {
0932: * Result result = db.queryObject(object, externalClauses, externalClausesParameters);
0933: *
0934: * if (result.hasNext())
0935: * return result.next(object);
0936: * }
0937: * finally
0938: * {
0939: * db.close();
0940: * }
0941: * </pre>
0942: *
0943: * @param object the object to load
0944: * @param externalClauses external clauses, which can begin with a where clause or any clause after the where clause.
0945: * @param externalClausesParameters the parameters to use with external clauses, can be null (1.5+ can use varargs)
0946: *
0947: * @return returns the object passed in
0948: *
0949: * @throws JPersistException
0950: */
0951:
0952: public <T> T loadObject(T object, String externalClauses,
0953: Object... externalClausesParameters)
0954: throws JPersistException {
0955: return loadObject(object, true, null, externalClauses,
0956: externalClausesParameters);
0957: }
0958:
0959: /**
0960: * Builds a select query from a class that matches up to a table, and then loads the
0961: * object (using set methods that match columns in a table matched to the class name)
0962: * with the result.
0963: *
0964: * <p>This is a one line convienence method for:
0965: * <pre>
0966: * Database db = getDatabase();
0967: *
0968: * try
0969: * {
0970: * Result result = db.queryObject(object, externalClauses, externalClausesParameters);
0971: *
0972: * if (result.hasNext())
0973: * return result.next(object, loadAssociations);
0974: * }
0975: * finally
0976: * {
0977: * db.close();
0978: * }
0979: * </pre>
0980: *
0981: * @param object the object to load
0982: * @param loadAssociations true to load associations
0983: * @param externalClauses external clauses, which can begin with a where clause or any clause after the where clause.
0984: * @param externalClausesParameters the parameters to use with external clauses, can be null (1.5+ can use varargs)
0985: *
0986: * @return returns the object passed in
0987: *
0988: * @throws JPersistException
0989: */
0990:
0991: public <T> T loadObject(T object, boolean loadAssociations,
0992: String externalClauses, Object... externalClausesParameters)
0993: throws JPersistException {
0994: return loadObject(object, loadAssociations, null,
0995: externalClauses, externalClausesParameters);
0996: }
0997:
0998: /**
0999: * Builds a select query from a class that matches up to a table, and then loads the
1000: * object (using set methods that match columns in a table matched to the class name)
1001: * with the result.
1002: *
1003: * <p>This is a one line convienence method for:
1004: * <pre>
1005: * Database db = getDatabase();
1006: *
1007: * try
1008: * {
1009: * Result result = db.queryObject(object, nullValuesToInclude, externalClauses, externalClausesParameters);
1010: *
1011: * if (result.hasNext())
1012: * return result.next(object);
1013: * }
1014: * finally
1015: * {
1016: * db.close();
1017: * }
1018: * </pre>
1019: *
1020: * @param object the object to load
1021: * @param nullValuesToInclude is a Set of set methods without the 'set', or table column names, to include in the where clause if null
1022: * @param externalClauses external clauses, which can begin with a where clause or any clause after the where clause.
1023: * @param externalClausesParameters the parameters to use with external clauses, can be null (1.5+ can use varargs)
1024: *
1025: * @return returns the object passed in
1026: *
1027: * @throws JPersistException
1028: */
1029:
1030: public <T> T loadObject(T object, Set<String> nullValuesToInclude,
1031: String externalClauses, Object... externalClausesParameters)
1032: throws JPersistException {
1033: return loadObject(object, true, nullValuesToInclude,
1034: externalClauses, externalClausesParameters);
1035: }
1036:
1037: /**
1038: * Builds a select query from a class that matches up to a table, and then loads the
1039: * object (using set methods that match columns in a table matched to the class name)
1040: * with the result.
1041: *
1042: * <p>This is a one line convienence method for:
1043: * <pre>
1044: * Database db = getDatabase();
1045: *
1046: * try
1047: * {
1048: * Result result = db.queryObject(object, nullValuesToInclude, externalClauses, externalClausesParameters);
1049: *
1050: * if (result.hasNext())
1051: * return result.next(object, loadAssociations);
1052: * }
1053: * finally
1054: * {
1055: * db.close();
1056: * }
1057: * </pre>
1058: *
1059: * @param object the object to load
1060: * @param loadAssociations true to load associations
1061: * @param nullValuesToInclude is a Set of set methods without the 'set', or table column names, to include in the where clause if null
1062: * @param externalClauses external clauses, which can begin with a where clause or any clause after the where clause.
1063: * @param externalClausesParameters the parameters to use with external clauses, can be null (1.5+ can use varargs)
1064: *
1065: * @return returns the object passed in
1066: *
1067: * @throws JPersistException
1068: */
1069:
1070: public <T> T loadObject(T object, boolean loadAssociations,
1071: Set<String> nullValuesToInclude, String externalClauses,
1072: Object... externalClausesParameters)
1073: throws JPersistException {
1074: Database db = getDatabase();
1075:
1076: try {
1077: Result<T> result = db.queryObject(object,
1078: nullValuesToInclude, externalClauses,
1079: externalClausesParameters);
1080:
1081: if (result.hasNext())
1082: return result.next(object, loadAssociations);
1083: } finally {
1084: db.close();
1085: }
1086:
1087: return null;
1088: }
1089:
1090: /**
1091: * Builds a select query from a class that matches up to a table, and then loads the
1092: * object (using set methods that match columns in a table matched to the class name)
1093: * with the result.
1094: *
1095: * <p>This is a one line convienence method for:
1096: * <pre>
1097: * Database db = getDatabase();
1098: *
1099: * try
1100: * {
1101: * Result result = db.queryObject(cs);
1102: *
1103: * if (result.hasNext())
1104: * return result.next();
1105: * }
1106: * finally
1107: * {
1108: * db.close();
1109: * }
1110: * </pre>
1111: *
1112: * @param cs the class to query and create an object instance from
1113: *
1114: * @return returns the object passed in
1115: *
1116: * @throws JPersistException
1117: */
1118:
1119: public <T> T loadObject(Class<T> cs) throws JPersistException {
1120: return loadObject(cs, true, null, (Object[]) null);
1121: }
1122:
1123: /**
1124: * Builds a select query from a class that matches up to a table, and then loads the
1125: * object (using set methods that match columns in a table matched to the class name)
1126: * with the result.
1127: *
1128: * <p>This is a one line convienence method for:
1129: * <pre>
1130: * Database db = getDatabase();
1131: *
1132: * try
1133: * {
1134: * Result result = db.queryObject(cs);
1135: *
1136: * if (result.hasNext())
1137: * return result.next(loadAssociations);
1138: * }
1139: * finally
1140: * {
1141: * db.close();
1142: * }
1143: * </pre>
1144: *
1145: * @param cs the class to query and create an object instance from
1146: * @param loadAssociations true to load associations
1147: *
1148: * @return returns the object passed in
1149: *
1150: * @throws JPersistException
1151: */
1152:
1153: public <T> T loadObject(Class<T> cs, boolean loadAssociations)
1154: throws JPersistException {
1155: return loadObject(cs, loadAssociations, null, (Object[]) null);
1156: }
1157:
1158: /**
1159: * Builds a select query from a class that matches up to a table, and then loads the
1160: * object (using set methods that match columns in a table matched to the class name)
1161: * with the result.
1162: *
1163: * <p>This is a one line convienence method for:
1164: * <pre>
1165: * Database db = getDatabase();
1166: *
1167: * try
1168: * {
1169: * Result result = db.queryObject(cs, externalClauses, externalClausesParameters);
1170: *
1171: * if (result.hasNext())
1172: * return result.next();
1173: * }
1174: * finally
1175: * {
1176: * db.close();
1177: * }
1178: * </pre>
1179: *
1180: * @param cs the class to query and create an object instance from
1181: * @param externalClauses external clauses, which can begin with a where clause or any clause after the where clause.
1182: * @param externalClausesParameters the parameters to use with external clauses, can be null (1.5+ can use varargs)
1183: *
1184: * @return returns the object passed in
1185: *
1186: * @throws JPersistException
1187: */
1188:
1189: public <T> T loadObject(Class<T> cs, String externalClauses,
1190: Object... externalClausesParameters)
1191: throws JPersistException {
1192: return loadObject(cs, true, externalClauses,
1193: externalClausesParameters);
1194: }
1195:
1196: /**
1197: * Builds a select query from a class that matches up to a table, and then loads the
1198: * object (using set methods that match columns in a table matched to the class name)
1199: * with the result.
1200: *
1201: * <p>This is a one line convienence method for:
1202: * <pre>
1203: * Database db = getDatabase();
1204: *
1205: * try
1206: * {
1207: * Result result = db.queryObject(cs, externalClauses, externalClausesParameters);
1208: *
1209: * if (result.hasNext())
1210: * return result.next(loadAssociations);
1211: * }
1212: * finally
1213: * {
1214: * db.close();
1215: * }
1216: * </pre>
1217: *
1218: * @param cs the class to query and create an object instance from
1219: * @param loadAssociations true to load associations
1220: * @param externalClauses external clauses, which can begin with a where clause or any clause after the where clause.
1221: * @param externalClausesParameters the parameters to use with external clauses, can be null (1.5+ can use varargs)
1222: *
1223: * @return returns the object passed in
1224: *
1225: * @throws JPersistException
1226: */
1227:
1228: public <T> T loadObject(Class<T> cs, boolean loadAssociations,
1229: String externalClauses, Object... externalClausesParameters)
1230: throws JPersistException {
1231: Database db = getDatabase();
1232:
1233: try {
1234: Result<T> result = db.queryObject(cs, externalClauses,
1235: externalClausesParameters);
1236:
1237: if (result.hasNext())
1238: return result.next(loadAssociations);
1239: } finally {
1240: db.close();
1241: }
1242:
1243: return null;
1244: }
1245:
1246: /**
1247: * Builds a select query from a class that matches up to a table, and then loads the
1248: * object (using set methods that match columns in a table matched to the class name)
1249: * with the result.
1250: *
1251: * <p>This is a one line convienence method for:
1252: * <pre>
1253: * Database db = getDatabase();
1254: *
1255: * try
1256: * {
1257: * return db.queryObject(object).loadObjects(collection, object.getClass());
1258: * }
1259: * finally
1260: * {
1261: * db.close();
1262: * }
1263: *
1264: * return collection;
1265: * </pre>
1266: *
1267: * @param collection an instance of Collection (i.e. Vector, Set, etc.)
1268: * @param object the object to load
1269: *
1270: * @return the Collection that was passed in
1271: *
1272: * @throws JPersistException
1273: */
1274:
1275: public <T> Collection<T> loadObjects(Collection<T> collection,
1276: T object) throws JPersistException {
1277: return loadObjects(collection, object, true, null, null,
1278: (Object[]) null);
1279: }
1280:
1281: /**
1282: * Builds a select query from a class that matches up to a table, and then loads the
1283: * object (using set methods that match columns in a table matched to the class name)
1284: * with the result.
1285: *
1286: * <p>This is a one line convienence method for:
1287: * <pre>
1288: * Database db = getDatabase();
1289: *
1290: * try
1291: * {
1292: * return db.queryObject(object).loadObjects(collection, object.getClass(), loadAssociations);
1293: * }
1294: * finally
1295: * {
1296: * db.close();
1297: * }
1298: *
1299: * return collection;
1300: * </pre>
1301: *
1302: * @param collection an instance of Collection (i.e. Vector, Set, etc.)
1303: * @param object the object to load
1304: * @param loadAssociations true to load associations
1305: *
1306: * @return the Collection that was passed in
1307: *
1308: * @throws JPersistException
1309: */
1310:
1311: public <T> Collection<T> loadObjects(Collection<T> collection,
1312: T object, boolean loadAssociations)
1313: throws JPersistException {
1314: return loadObjects(collection, object, loadAssociations, null,
1315: null, (Object[]) null);
1316: }
1317:
1318: /**
1319: * Builds a select query from a class that matches up to a table, and then loads the
1320: * object (using set methods that match columns in a table matched to the class name)
1321: * with the result.
1322: *
1323: * <p>This is a one line convienence method for:
1324: * <pre>
1325: * Database db = getDatabase();
1326: *
1327: * try
1328: * {
1329: * return db.queryObject(object, nullValuesToInclude).loadObjects(collection, object.getClass());
1330: * }
1331: * finally
1332: * {
1333: * db.close();
1334: * }
1335: *
1336: * return collection;
1337: * </pre>
1338: *
1339: * @param collection an instance of Collection (i.e. Vector, Set, etc.)
1340: * @param object the object to load
1341: * @param nullValuesToInclude is a Set of set methods without the 'set', or table column names, to include in the where clause if null
1342: *
1343: * @return the Collection that was passed in
1344: *
1345: * @throws JPersistException
1346: */
1347:
1348: public <T> Collection<T> loadObjects(Collection<T> collection,
1349: T object, Set<String> nullValuesToInclude)
1350: throws JPersistException {
1351: return loadObjects(collection, object, true,
1352: nullValuesToInclude, null, (Object[]) null);
1353: }
1354:
1355: /**
1356: * Builds a select query from a class that matches up to a table, and then loads the
1357: * object (using set methods that match columns in a table matched to the class name)
1358: * with the result.
1359: *
1360: * <p>This is a one line convienence method for:
1361: * <pre>
1362: * Database db = getDatabase();
1363: *
1364: * try
1365: * {
1366: * return db.queryObject(object, nullValuesToInclude).loadObjects(collection, object.getClass(), loadAssociations);
1367: * }
1368: * finally
1369: * {
1370: * db.close();
1371: * }
1372: *
1373: * return collection;
1374: * </pre>
1375: *
1376: * @param collection an instance of Collection (i.e. Vector, Set, etc.)
1377: * @param object the object to load
1378: * @param loadAssociations true to load associations
1379: * @param nullValuesToInclude is a Set of set methods without the 'set', or table column names, to include in the where clause if null
1380: *
1381: * @return the Collection that was passed in
1382: *
1383: * @throws JPersistException
1384: */
1385:
1386: public <T> Collection<T> loadObjects(Collection<T> collection,
1387: T object, boolean loadAssociations,
1388: Set<String> nullValuesToInclude) throws JPersistException {
1389: return loadObjects(collection, object, loadAssociations,
1390: nullValuesToInclude, null, (Object[]) null);
1391: }
1392:
1393: /**
1394: * Builds a select query from a class that matches up to a table, and then loads the
1395: * object (using set methods that match columns in a table matched to the class name)
1396: * with the result.
1397: *
1398: * <p>This is a one line convienence method for:
1399: * <pre>
1400: * Database db = getDatabase();
1401: *
1402: * try
1403: * {
1404: * return db.queryObject(object, externalClauses, externalClausesParameters).loadObjects(collection, object.getClass());
1405: * }
1406: * finally
1407: * {
1408: * db.close();
1409: * }
1410: *
1411: * return collection;
1412: * </pre>
1413: *
1414: * @param collection an instance of Collection (i.e. Vector, Set, etc.)
1415: * @param object the object to load
1416: * @param externalClauses external clauses, which can begin with a where clause or any clause after the where clause.
1417: * @param externalClausesParameters the parameters to use with external clauses, can be null (1.5+ can use varargs)
1418: *
1419: * @return the Collection that was passed in
1420: *
1421: * @throws JPersistException
1422: */
1423:
1424: public <T> Collection<T> loadObjects(Collection<T> collection,
1425: T object, String externalClauses,
1426: Object... externalClausesParameters)
1427: throws JPersistException {
1428: return loadObjects(collection, object, true, null,
1429: externalClauses, externalClausesParameters);
1430: }
1431:
1432: /**
1433: * Builds a select query from a class that matches up to a table, and then loads the
1434: * object (using set methods that match columns in a table matched to the class name)
1435: * with the result.
1436: *
1437: * <p>This is a one line convienence method for:
1438: * <pre>
1439: * Database db = getDatabase();
1440: *
1441: * try
1442: * {
1443: * return db.queryObject(object, externalClauses, externalClausesParameters).loadObjects(collection, object.getClass(), loadAssociations);
1444: * }
1445: * finally
1446: * {
1447: * db.close();
1448: * }
1449: *
1450: * return collection;
1451: * </pre>
1452: *
1453: * @param collection an instance of Collection (i.e. Vector, Set, etc.)
1454: * @param object the object to load
1455: * @param loadAssociations true to load associations
1456: * @param externalClauses external clauses, which can begin with a where clause or any clause after the where clause.
1457: * @param externalClausesParameters the parameters to use with external clauses, can be null (1.5+ can use varargs)
1458: *
1459: * @return the Collection that was passed in
1460: *
1461: * @throws JPersistException
1462: */
1463:
1464: public <T> Collection<T> loadObjects(Collection<T> collection,
1465: T object, boolean loadAssociations, String externalClauses,
1466: Object... externalClausesParameters)
1467: throws JPersistException {
1468: return loadObjects(collection, object, loadAssociations, null,
1469: externalClauses, externalClausesParameters);
1470: }
1471:
1472: /**
1473: * Builds a select query from a class that matches up to a table, and then loads the
1474: * object (using set methods that match columns in a table matched to the class name)
1475: * with the result.
1476: *
1477: * <p>This is a one line convienence method for:
1478: * <pre>
1479: * Database db = getDatabase();
1480: *
1481: * try
1482: * {
1483: * return db.queryObject(object, nullValuesToInclude, externalClauses, externalClausesParameters).loadObjects(collection, object.getClass());
1484: * }
1485: * finally
1486: * {
1487: * db.close();
1488: * }
1489: *
1490: * return collection;
1491: * </pre>
1492: *
1493: * @param collection an instance of Collection (i.e. Vector, Set, etc.)
1494: * @param object the object to load
1495: * @param nullValuesToInclude is a Set of set methods without the 'set', or table column names, to include in the where clause if null
1496: * @param externalClauses external clauses, which can begin with a where clause or any clause after the where clause.
1497: * @param externalClausesParameters the parameters to use with external clauses, can be null (1.5+ can use varargs)
1498: *
1499: * @return the Collection that was passed in
1500: *
1501: * @throws JPersistException
1502: */
1503:
1504: public <T> Collection<T> loadObjects(Collection<T> collection,
1505: T object, Set<String> nullValuesToInclude,
1506: String externalClauses, Object... externalClausesParameters)
1507: throws JPersistException {
1508: return loadObjects(collection, object, true,
1509: nullValuesToInclude, externalClauses,
1510: externalClausesParameters);
1511: }
1512:
1513: /**
1514: * Builds a select query from a class that matches up to a table, and then loads the
1515: * object (using set methods that match columns in a table matched to the class name)
1516: * with the result.
1517: *
1518: * <p>This is a one line convienence method for:
1519: * <pre>
1520: * Database db = getDatabase();
1521: *
1522: * try
1523: * {
1524: * return db.queryObject(object, nullValuesToInclude, externalClauses, externalClausesParameters).loadObjects(collection, object.getClass(), loadAssociations);
1525: * }
1526: * finally
1527: * {
1528: * db.close();
1529: * }
1530: *
1531: * return collection;
1532: * </pre>
1533: *
1534: * @param collection an instance of Collection (i.e. Vector, Set, etc.)
1535: * @param object the object to load
1536: * @param loadAssociations true to load associations
1537: * @param nullValuesToInclude is a Set of set methods without the 'set', or table column names, to include in the where clause if null
1538: * @param externalClauses external clauses, which can begin with a where clause or any clause after the where clause.
1539: * @param externalClausesParameters the parameters to use with external clauses, can be null (1.5+ can use varargs)
1540: *
1541: * @return the Collection that was passed in
1542: *
1543: * @throws JPersistException
1544: */
1545:
1546: public <T> Collection<T> loadObjects(Collection<T> collection,
1547: T object, boolean loadAssociations,
1548: Set<String> nullValuesToInclude, String externalClauses,
1549: Object... externalClausesParameters)
1550: throws JPersistException {
1551: Database db = getDatabase();
1552:
1553: try {
1554: return db.queryObject(object, nullValuesToInclude,
1555: externalClauses, externalClausesParameters)
1556: .loadObjects(collection,
1557: (Class<T>) object.getClass(),
1558: loadAssociations);
1559: } finally {
1560: db.close();
1561: }
1562: }
1563:
1564: /**
1565: * Builds a select query from a class that matches up to a table, and then loads an
1566: * instance (using set methods that match columns in a table matched to the class name)
1567: * with the result.
1568: *
1569: * <p>This is a one line convienence method for:
1570: * <pre>
1571: * Database db = getDatabase();
1572: *
1573: * try
1574: * {
1575: * return db.queryObject(cs).loadObjects(cs, collection);
1576: * }
1577: * finally
1578: * {
1579: * db.close();
1580: * }
1581: *
1582: * return collection;
1583: * </pre>
1584: *
1585: * @param collection an instance of Collection (i.e. Vector, Set, etc.)
1586: * @param cs the class to load
1587: *
1588: * @return the Collection that was passed in
1589: *
1590: * @throws JPersistException
1591: */
1592:
1593: public <T> Collection<T> loadObjects(Collection<T> collection,
1594: Class<T> cs) throws JPersistException {
1595: return loadObjects(collection, cs, true, null, (Object[]) null);
1596: }
1597:
1598: /**
1599: * Builds a select query from a class that matches up to a table, and then loads an
1600: * instance (using set methods that match columns in a table matched to the class name)
1601: * with the result.
1602: *
1603: * <p>This is a one line convienence method for:
1604: * <pre>
1605: * Database db = getDatabase();
1606: *
1607: * try
1608: * {
1609: * return db.queryObject(cs).loadObjects(cs, collection, loadAssociations);
1610: * }
1611: * finally
1612: * {
1613: * db.close();
1614: * }
1615: *
1616: * return collection;
1617: * </pre>
1618: *
1619: * @param collection an instance of Collection (i.e. Vector, Set, etc.)
1620: * @param cs the class to load
1621: * @param loadAssociations true to load associations
1622: *
1623: * @return the Collection that was passed in
1624: *
1625: * @throws JPersistException
1626: */
1627:
1628: public <T> Collection<T> loadObjects(Collection<T> collection,
1629: Class<T> cs, boolean loadAssociations)
1630: throws JPersistException {
1631: return loadObjects(collection, cs, loadAssociations, null,
1632: (Object[]) null);
1633: }
1634:
1635: /**
1636: * Builds a select query from a class that matches up to a table, and then loads an
1637: * instance (using set methods that match columns in a table matched to the class name)
1638: * with the result.
1639: *
1640: * <p>This is a one line convienence method for:
1641: * <pre>
1642: * Database db = getDatabase();
1643: *
1644: * try
1645: * {
1646: * return db.queryObject(cs, externalClauses, externalClausesParameters).loadObjects(cs, collection);
1647: * }
1648: * finally
1649: * {
1650: * db.close();
1651: * }
1652: *
1653: * return collection;
1654: * </pre>
1655: *
1656: * @param collection an instance of Collection (i.e. Vector, Set, etc.)
1657: * @param cs the class to load
1658: * @param externalClauses external clauses, which can begin with a where clause or any clause after the where clause.
1659: * @param externalClausesParameters the parameters to use with external clauses, can be null (1.5+ can use varargs)
1660: *
1661: * @return the Collection that was passed in
1662: *
1663: * @throws JPersistException
1664: */
1665:
1666: public <T> Collection<T> loadObjects(Collection<T> collection,
1667: Class<T> cs, String externalClauses,
1668: Object... externalClausesParameters)
1669: throws JPersistException {
1670: return loadObjects(collection, cs, true, externalClauses,
1671: externalClausesParameters);
1672: }
1673:
1674: /**
1675: * Builds a select query from a class that matches up to a table, and then loads an
1676: * instance (using set methods that match columns in a table matched to the class name)
1677: * with the result.
1678: *
1679: * <p>This is a one line convienence method for:
1680: * <pre>
1681: * Database db = getDatabase();
1682: *
1683: * try
1684: * {
1685: * return db.queryObject(cs, externalClauses, externalClausesParameters).loadObjects(cs, collection, loadAssociations);
1686: * }
1687: * finally
1688: * {
1689: * db.close();
1690: * }
1691: *
1692: * return collection;
1693: * </pre>
1694: *
1695: * @param collection an instance of Collection (i.e. Vector, Set, etc.)
1696: * @param cs the class to load
1697: * @param loadAssociations true to load associations
1698: * @param externalClauses external clauses, which can begin with a where clause or any clause after the where clause.
1699: * @param externalClausesParameters the parameters to use with external clauses, can be null (1.5+ can use varargs)
1700: *
1701: * @return the Collection that was passed in
1702: *
1703: * @throws JPersistException
1704: */
1705:
1706: public <T> Collection<T> loadObjects(Collection<T> collection,
1707: Class<T> cs, boolean loadAssociations,
1708: String externalClauses, Object... externalClausesParameters)
1709: throws JPersistException {
1710: Database db = getDatabase();
1711:
1712: try {
1713: return db.queryObject(cs, externalClauses,
1714: externalClausesParameters).loadObjects(collection,
1715: cs, loadAssociations);
1716: } finally {
1717: db.close();
1718: }
1719: }
1720:
1721: /**
1722: * Loads an objects associations.
1723: *
1724: * @param object the object whose associations are to be loaded
1725: *
1726: * @throws jpersist.JPersistException
1727: */
1728: public void loadAssociations(Object object)
1729: throws JPersistException {
1730: Database db = getDatabase();
1731:
1732: try {
1733: db.loadAssociations(object);
1734: } finally {
1735: db.close();
1736: }
1737: }
1738:
1739: /**
1740: * Builds either an update or an insert depending on whether the object is persistent and was previously loaded, or not, respectively.
1741: *
1742: * <p>This method should not be used within a transaction manager. Use TransactionManager.getDatabase() instead.
1743: *
1744: * <p>This is a one line convienence method for:
1745: * <pre>
1746: * Database db = getDatabase();
1747: *
1748: * try
1749: * {
1750: * return db.saveObject(object);
1751: * }
1752: * finally
1753: * {
1754: * db.close();
1755: * }
1756: * </pre>
1757: *
1758: * @param object the object to load
1759: *
1760: * @return the number of rows updated
1761: *
1762: * @throws JPersistException
1763: */
1764:
1765: public int saveObject(Object object) throws JPersistException {
1766: return saveObject(object, null, null, (Object[]) null);
1767: }
1768:
1769: /**
1770: * Builds either an update or an insert depending on whether the object is persistent and was previously loaded, or not, respectively.
1771: *
1772: * <p>This method should not be used within a transaction manager. Use TransactionManager.getDatabase() instead.
1773: *
1774: * <p>This is a one line convienence method for:
1775: * <pre>
1776: * Database db = getDatabase();
1777: *
1778: * try
1779: * {
1780: * return db.saveObject(object, nullValuesToInclude);
1781: * }
1782: * finally
1783: * {
1784: * db.close();
1785: * }
1786: * </pre>
1787: *
1788: * @param object the object to load
1789: * @param nullValuesToInclude is a Set of set methods without the 'set', or table column names, to include in the update
1790: *
1791: * @return the number of rows updated
1792: *
1793: * @throws JPersistException
1794: */
1795:
1796: public int saveObject(Object object, Set<String> nullValuesToInclude)
1797: throws JPersistException {
1798: return saveObject(object, nullValuesToInclude, null,
1799: (Object[]) null);
1800: }
1801:
1802: /**
1803: * Builds either an update or an insert depending on whether the object is persistent and was previously loaded, or not, respectively.
1804: * externalClauses can begin with a where clause or anything after the where clause.
1805: *
1806: * <p>This method should not be used within a transaction manager. Use TransactionManager.getDatabase() instead.
1807: *
1808: * <p>This is a one line convienence method for:
1809: * <pre>
1810: * Database db = getDatabase();
1811: *
1812: * try
1813: * {
1814: * return db.saveObject(object, externalClauses, externalClausesParameters);
1815: * }
1816: * finally
1817: * {
1818: * db.close();
1819: * }
1820: * </pre>
1821: *
1822: *
1823: * @param object the object to load
1824: * @param externalClauses external clauses beginning with a where or after
1825: * @param externalClausesParameters the parameters to use with external clauses, can be null
1826: *
1827: * @return the number of rows updated
1828: *
1829: * @throws JPersistException
1830: */
1831:
1832: public int saveObject(Object object, String externalClauses,
1833: Object... externalClausesParameters)
1834: throws JPersistException {
1835: return saveObject(object, null, externalClauses,
1836: externalClausesParameters);
1837: }
1838:
1839: /**
1840: * Builds either an update or an insert depending on whether the object is persistent and was previously loaded, or not, respectively.
1841: * externalClauses can begin with a where clause or anything after the where clause.
1842: *
1843: * <p>This method should not be used within a transaction manager. Use TransactionManager.getDatabase() instead.
1844: *
1845: * <p>This is a one line convienence method for:
1846: * <pre>
1847: * Database db = getDatabase();
1848: *
1849: * try
1850: * {
1851: * return db.saveObject(object, nullValuesToInclude, externalClauses, externalClausesParameters);
1852: * }
1853: * finally
1854: * {
1855: * db.close();
1856: * }
1857: * </pre>
1858: *
1859: *
1860: * @param object the object to load
1861: * @param nullValuesToInclude is a Set of set methods without the 'set', or table column names, to include in the update
1862: * @param externalClauses external clauses beginning with a where or after
1863: * @param externalClausesParameters the parameters to use with external clauses, can be null
1864: *
1865: * @return the number of rows updated
1866: *
1867: * @throws JPersistException
1868: */
1869:
1870: public int saveObject(Object object,
1871: Set<String> nullValuesToInclude, String externalClauses,
1872: Object... externalClausesParameters)
1873: throws JPersistException {
1874: Database db = getDatabase();
1875:
1876: try {
1877: return db.saveObject(object, nullValuesToInclude,
1878: externalClauses, externalClausesParameters);
1879: } finally {
1880: db.close();
1881: }
1882: }
1883:
1884: /**
1885: * Builds a delete statement from the object.
1886: *
1887: * <p>This method should not be used within a transaction manager. Use TransactionManager.getDatabase() instead.
1888: *
1889: * <p>This is a one line convienence method for:
1890: * <pre>
1891: * Database db = getDatabase();
1892: *
1893: * try
1894: * {
1895: * return db.deleteObject(object);
1896: * }
1897: * finally
1898: * {
1899: * db.close();
1900: * }
1901: * </pre>
1902: *
1903: * @param object the object to load
1904: *
1905: * @return the number of rows updated
1906: *
1907: * @throws JPersistException
1908: */
1909:
1910: public int deleteObject(Object object) throws JPersistException {
1911: return deleteObject(object, null, null, (Object[]) null);
1912: }
1913:
1914: /**
1915: * Builds a delete statement from the object.
1916: *
1917: * <p>This method should not be used within a transaction manager. Use TransactionManager.getDatabase() instead.
1918: *
1919: * <p>This is a one line convienence method for:
1920: * <pre>
1921: * Database db = getDatabase();
1922: *
1923: * try
1924: * {
1925: * return db.deleteObject(object, nullValuesToInclude);
1926: * }
1927: * finally
1928: * {
1929: * db.close();
1930: * }
1931: * </pre>
1932: *
1933: * @param object the object to load
1934: * @param nullValuesToInclude is a Set of set methods without the 'set', or table column names, to include in the where clause if null
1935: *
1936: * @return the number of rows updated
1937: *
1938: * @throws JPersistException
1939: */
1940:
1941: public int deleteObject(Object object,
1942: Set<String> nullValuesToInclude) throws JPersistException {
1943: return deleteObject(object, nullValuesToInclude, null,
1944: (Object[]) null);
1945: }
1946:
1947: /**
1948: * Builds a delete statement from the object. externalClauses can begin with a where clause or anything after
1949: * the where clause.
1950: *
1951: * <p>This method should not be used within a transaction manager. Use TransactionManager.getDatabase() instead.
1952: *
1953: * <p>This is a one line convienence method for:
1954: * <pre>
1955: * Database db = getDatabase();
1956: *
1957: * try
1958: * {
1959: * return db.deleteObject(object, externalClauses, externalClausesParameters);
1960: * }
1961: * finally
1962: * {
1963: * db.close();
1964: * }
1965: * </pre>
1966: *
1967: * @param object the object to load
1968: * @param externalClauses external clauses
1969: * @param externalClausesParameters the parameters to use with external clauses, can be null
1970: *
1971: * @return the number of rows updated
1972: *
1973: * @throws JPersistException
1974: */
1975:
1976: public int deleteObject(Object object, String externalClauses,
1977: Object... externalClausesParameters)
1978: throws JPersistException {
1979: return deleteObject(object, null, externalClauses,
1980: externalClausesParameters);
1981: }
1982:
1983: /**
1984: * Builds a delete statement from the object. externalClauses can begin with a where clause or anything after
1985: * the where clause.
1986: *
1987: * <p>This method should not be used within a transaction manager. Use TransactionManager.getDatabase() instead.
1988: *
1989: * <p>This is a one line convienence method for:
1990: * <pre>
1991: * Database db = getDatabase();
1992: *
1993: * try
1994: * {
1995: * return db.deleteObject(object, nullValuesToInclude, externalClauses, externalClausesParameters);
1996: * }
1997: * finally
1998: * {
1999: * db.close();
2000: * }
2001: * </pre>
2002: *
2003: * @param object the object to load
2004: * @param nullValuesToInclude is a Set of set methods without the 'set', or table column names, to include in the where clause if null
2005: * @param externalClauses external clauses
2006: * @param externalClausesParameters the parameters to use with external clauses, can be null
2007: *
2008: * @return the number of rows updated
2009: *
2010: * @throws JPersistException
2011: */
2012:
2013: public int deleteObject(Object object,
2014: Set<String> nullValuesToInclude, String externalClauses,
2015: Object... externalClausesParameters)
2016: throws JPersistException {
2017: Database db = getDatabase();
2018:
2019: try {
2020: return db.deleteObject(object, nullValuesToInclude,
2021: externalClauses, externalClausesParameters);
2022: } finally {
2023: db.close();
2024: }
2025: }
2026:
2027: /**
2028: * Executes a simple query.
2029: *
2030: * @param sql the SQL statement
2031: *
2032: * @return a jpersist.Result instance
2033: *
2034: * @throws JPersistException
2035: */
2036:
2037: public Result executeQuery(String sql) throws JPersistException {
2038: Database db = getDatabase();
2039:
2040: try {
2041: return db.executeQuery(sql);
2042: } finally {
2043: db.close();
2044: }
2045: }
2046:
2047: /**
2048: * Executes a parameterized query. A paramterized query allows the use of '?' in
2049: * SQL statements.
2050: *
2051: * @param sql the SQL statement
2052: * @param parameters objects used to set the parameters to the query
2053: *
2054: * @return a jpersist.Result instance
2055: *
2056: * @throws JPersistException
2057: */
2058:
2059: public Result parameterizedQuery(String sql, Object... parameters)
2060: throws JPersistException {
2061: Database db = getDatabase();
2062:
2063: try {
2064: return db.parameterizedQuery(sql, parameters);
2065: } finally {
2066: db.close();
2067: }
2068: }
2069:
2070: /**
2071: * Executes a simple update.
2072: *
2073: * @param sql the SQL statement
2074: *
2075: * @throws JPersistException
2076: */
2077:
2078: public int executeUpdate(String sql) throws JPersistException {
2079: Database db = getDatabase();
2080:
2081: try {
2082: return db.executeUpdate(sql);
2083: } finally {
2084: db.close();
2085: }
2086: }
2087:
2088: /**
2089: * Executes a simple update.
2090: *
2091: * @param sql the SQL statement
2092: * @param keys is a Vector. If keys is non-null, then generated keys will be returned in
2093: * the keys vector. Vector can also define the key columns required
2094: * (depending on the database).
2095: *
2096: * @throws JPersistException
2097: */
2098:
2099: public int executeUpdate(String sql, Vector keys)
2100: throws JPersistException {
2101: Database db = getDatabase();
2102:
2103: try {
2104: return db.executeUpdate(sql, keys);
2105: } finally {
2106: db.close();
2107: }
2108: }
2109:
2110: /**
2111: * Executes a parameterized update. A paramterized update allows the use of '?' in
2112: * SQL statements.
2113: *
2114: * @param sql the SQL statement
2115: * @param parameters objects used to set the parameters to the update
2116: *
2117: * @throws JPersistException
2118: */
2119:
2120: public int parameterizedUpdate(String sql, Object... parameters)
2121: throws JPersistException {
2122: Database db = getDatabase();
2123:
2124: try {
2125: return db.parameterizedUpdate(sql, parameters);
2126: } finally {
2127: db.close();
2128: }
2129: }
2130:
2131: /**
2132: * Executes a parameterized update. A paramterized update allows the use of '?' in
2133: * SQL statements.
2134: *
2135: * @param sql the SQL statement
2136: * @param keys is a Vector. If keys is non-null, then generated keys will be returned in
2137: * the keys vector. Vector can also define the key columns required
2138: * (depending on the database).
2139: * @param parameters objects used to set the parameters to the update
2140: *
2141: * @throws JPersistException
2142: */
2143:
2144: public int parameterizedUpdate(String sql, Vector keys,
2145: Object... parameters) throws JPersistException {
2146: Database db = getDatabase();
2147:
2148: try {
2149: return db.parameterizedUpdate(sql, keys, parameters);
2150: } finally {
2151: db.close();
2152: }
2153: }
2154: }
|