001: package org.apache.torque;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.torque.adapter.DB;
023: import org.apache.torque.dsfactory.DataSourceFactory;
024: import org.apache.torque.map.DatabaseMap;
025: import org.apache.torque.oid.IDBroker;
026: import org.apache.torque.oid.IdGenerator;
027:
028: /**
029: * Bundles all information about a database. This includes the database adapter,
030: * the database Map and the Data Source Factory.
031: */
032: public class Database {
033: /**
034: * The name of the database. Must be the same as the key in Torque's
035: * databaseMap.
036: */
037: private String name;
038:
039: /**
040: * The Database adapter which encapsulates database-specific peculiarities.
041: */
042: private DB adapter;
043:
044: /**
045: * the Map of this database.
046: */
047: private DatabaseMap databaseMap;
048:
049: /**
050: * The DataSourceFactory to optain connections to this database.
051: */
052: private DataSourceFactory dataSourceFactory;
053:
054: /**
055: * Creates a new Database with the given name.
056: *
057: * @param aName the name of the database, not null.
058: */
059: Database(String aName) {
060: this .name = aName;
061: }
062:
063: /**
064: * returns the name of the database.
065: *
066: * @return the name of the database. May be null.
067: */
068: public String getName() {
069: return name;
070: }
071:
072: /**
073: * Returns the adapther to this database.
074: *
075: * @return the adapter to this database, or null if no adapter is set.
076: */
077: public DB getAdapter() {
078: return adapter;
079: }
080:
081: /**
082: * Sets the adapter for this database.
083: *
084: * @param anAdapter The adapter for this database, or null to remove the
085: * current adapter from this database.
086: */
087: public void setAdapter(DB anAdapter) {
088: this .adapter = anAdapter;
089: }
090:
091: /**
092: * Returns the database map for this database.
093: * If the database map does not exist yet, it is created by this method.
094: */
095: public synchronized DatabaseMap getDatabaseMap() {
096: if (databaseMap == null) {
097: databaseMap = new DatabaseMap(name);
098: }
099: return databaseMap;
100: }
101:
102: /**
103: * Returns the DataSourceFactory for this database.
104: * The DataSourceFactory is responsible to create connections
105: * to this database.
106: *
107: * @return the DataSourceFactory for this database, or null if no
108: * DataSourceFactory exists for this database.
109: */
110: public DataSourceFactory getDataSourceFactory() {
111: return dataSourceFactory;
112: }
113:
114: /**
115: * Sets the DataSourceFactory for this database.
116: * The DataSourceFactory is responsible to create connections
117: * to this database.
118: *
119: * @param aDataSourceFactory The new DataSorceFactory for this database,
120: * or null to remove the current DataSourceFactory.
121: */
122: public void setDataSourceFactory(
123: DataSourceFactory aDataSourceFactory) {
124: this .dataSourceFactory = aDataSourceFactory;
125: }
126:
127: /**
128: * Get the IDBroker for this database.
129: *
130: * @return The IDBroker for this database, or null if no IdBroker has
131: * been started for this database.
132: */
133: public IDBroker getIDBroker() {
134: if (databaseMap == null) {
135: return null;
136: }
137: return databaseMap.getIDBroker();
138: }
139:
140: /**
141: * Creates the IDBroker for this DatabaseMap and starts it for the
142: * given database.
143: * The information about the IdTable is stored in the databaseMap.
144: * If an IDBroker already exists for the DatabaseMap, the method
145: * does nothing.
146: *
147: * @return true if a new IDBroker was created, false otherwise.
148: */
149: public synchronized boolean startIDBroker() {
150: DatabaseMap dbMap = getDatabaseMap();
151: if (dbMap.getIDBroker() != null) {
152: return false;
153: }
154: return dbMap.startIdBroker();
155: }
156:
157: /**
158: * Returns the IdGenerator of the given type for this Database.
159: * @param type The type (i.e.name) of the IdGenerator
160: * @return The IdGenerator of the requested type, or null if no IdGenerator
161: * exists for the requested type.
162: */
163: public IdGenerator getIdGenerator(String type) {
164: if (databaseMap == null) {
165: return null;
166: }
167: return databaseMap.getIdGenerator(type);
168: }
169:
170: /**
171: * Adds an IdGenerator to the database.
172: * @param type The type of the IdGenerator
173: * @param idGen The new IdGenerator for the type, or null
174: * to remove the IdGenerator of the given type.
175: */
176: public void addIdGenerator(String type, IdGenerator idGen) {
177: getDatabaseMap().addIdGenerator(type, idGen);
178: }
179:
180: /**
181: * Returns the database schema for this Database.
182: * @return the database schema for this database, or null if no schema
183: * has been set.
184: */
185: public String getSchema() {
186: DataSourceFactory dsf = getDataSourceFactory();
187: if (dsf == null) {
188: return null;
189: }
190: return dsf.getSchema();
191: }
192:
193: /**
194: * Sets the schema for this database.
195: * @param schema the name of the database schema to set, or null to remove
196: * the current schema.
197: * @throws NullPointerException if no DatasourceFactory exists for this
198: * database.
199: */
200: public void setSchema(String schema) {
201: getDataSourceFactory().setSchema(schema);
202: }
203: }
|