View Javadoc

1   package org.apache.torque;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.torque.adapter.DB;
23  import org.apache.torque.dsfactory.DataSourceFactory;
24  import org.apache.torque.map.DatabaseMap;
25  import org.apache.torque.oid.IDBroker;
26  import org.apache.torque.oid.IdGenerator;
27  
28  /***
29   * Bundles all information about a database. This includes the database adapter,
30   * the database Map and the Data Source Factory.
31   */
32  public class Database
33  {
34      /***
35       * The name of the database. Must be the same as the key in Torque's
36       * databaseMap.
37       */
38      private String name;
39  
40      /***
41       * The Database adapter which encapsulates database-specific peculiarities.
42       */
43      private DB adapter;
44  
45      /***
46       * the Map of this database.
47       */
48      private DatabaseMap databaseMap;
49  
50      /***
51       * The DataSourceFactory to optain connections to this database.
52       */
53      private DataSourceFactory dataSourceFactory;
54  
55      /***
56       * Creates a new Database with the given name.
57       *
58       * @param aName the name of the database, not null.
59       */
60      Database(String aName)
61      {
62          this.name = aName;
63      }
64  
65      /***
66       * returns the name of the database.
67       *
68       * @return the name of the database. May be null.
69       */
70      public String getName()
71      {
72          return name;
73      }
74  
75      /***
76       * Returns the adapther to this database.
77       *
78       * @return the adapter to this database, or null if no adapter is set.
79       */
80      public DB getAdapter()
81      {
82          return adapter;
83      }
84  
85      /***
86       * Sets the adapter for this database.
87       *
88       * @param anAdapter The adapter for this database, or null to remove the
89       *        current adapter from this database.
90       */
91      public void setAdapter(DB anAdapter)
92      {
93          this.adapter = anAdapter;
94      }
95  
96      /***
97       * Returns the database map for this database.
98       * If the database map does not exist yet, it is created by this method.
99       */
100     public synchronized DatabaseMap getDatabaseMap()
101     {
102         if (databaseMap == null)
103         {
104             databaseMap = new DatabaseMap(name);
105         }
106         return databaseMap;
107     }
108 
109     /***
110      * Returns the DataSourceFactory for this database.
111      * The DataSourceFactory is responsible to create connections
112      * to this database.
113      *
114      * @return the DataSourceFactory for this database, or null if no
115      *         DataSourceFactory exists for this database.
116      */
117     public DataSourceFactory getDataSourceFactory()
118     {
119         return dataSourceFactory;
120     }
121 
122     /***
123      * Sets the DataSourceFactory for this database.
124      * The DataSourceFactory is responsible to create connections
125      * to this database.
126      *
127      * @param aDataSourceFactory The new DataSorceFactory for this database,
128      *        or null to remove the current DataSourceFactory.
129      */
130     public void setDataSourceFactory(DataSourceFactory aDataSourceFactory)
131     {
132         this.dataSourceFactory = aDataSourceFactory;
133     }
134 
135     /***
136      * Get the IDBroker for this database.
137      *
138      * @return The IDBroker for this database, or null if no IdBroker has
139      *         been started for this database.
140      */
141     public IDBroker getIDBroker()
142     {
143         if (databaseMap == null)
144         {
145             return null;
146         }
147         return databaseMap.getIDBroker();
148     }
149 
150     /***
151      * Creates the IDBroker for this DatabaseMap and starts it for the
152      * given database.
153      * The information about the IdTable is stored in the databaseMap.
154      * If an IDBroker already exists for the DatabaseMap, the method
155      * does nothing.
156      *
157      * @return true if a new IDBroker was created, false otherwise.
158      */
159     public synchronized boolean startIDBroker()
160     {
161         DatabaseMap dbMap = getDatabaseMap();
162         if (dbMap.getIDBroker() != null)
163         {
164             return false;
165         }
166         return dbMap.startIdBroker();
167     }
168 
169     /***
170      * Returns the IdGenerator of the given type for this Database.
171      * @param type The type (i.e.name) of the IdGenerator
172      * @return The IdGenerator of the requested type, or null if no IdGenerator
173      *         exists for the requested type.
174      */
175     public IdGenerator getIdGenerator(String type)
176     {
177         if (databaseMap == null)
178         {
179             return null;
180         }
181         return databaseMap.getIdGenerator(type);
182     }
183 
184     /***
185      * Adds an IdGenerator to the database.
186      * @param type The type of the IdGenerator
187      * @param idGen The new IdGenerator for the type, or null
188      *        to remove the IdGenerator of the given type.
189      */
190     public void addIdGenerator(String type, IdGenerator idGen)
191     {
192         getDatabaseMap().addIdGenerator(type, idGen);
193     }
194 
195     /***
196      * Returns the database schema for this Database.
197      * @return the database schema for this database, or null if no schema
198      *         has been set.
199      */
200     public String getSchema()
201     {
202         DataSourceFactory dsf = getDataSourceFactory();
203         if (dsf == null)
204         {
205             return null;
206         }
207         return dsf.getSchema();
208     }
209 
210     /***
211      * Sets the schema for this database.
212      * @param schema the name of the database schema to set, or null to remove
213      *        the current schema.
214      * @throws NullPointerException if no DatasourceFactory exists for this
215      *         database.
216      */
217     public void setSchema(String schema)
218     {
219         getDataSourceFactory().setSchema(schema);
220     }
221 }