1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.dao.db;
24  
25  import com.liferay.portal.kernel.dao.db.DB;
26  import com.liferay.portal.kernel.dao.db.DBFactory;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.util.PropsValues;
30  
31  import org.hibernate.dialect.DB2Dialect;
32  import org.hibernate.dialect.DerbyDialect;
33  import org.hibernate.dialect.Dialect;
34  import org.hibernate.dialect.FirebirdDialect;
35  import org.hibernate.dialect.HSQLDialect;
36  import org.hibernate.dialect.InformixDialect;
37  import org.hibernate.dialect.IngresDialect;
38  import org.hibernate.dialect.InterbaseDialect;
39  import org.hibernate.dialect.JDataStoreDialect;
40  import org.hibernate.dialect.MySQLDialect;
41  import org.hibernate.dialect.Oracle8iDialect;
42  import org.hibernate.dialect.Oracle9Dialect;
43  import org.hibernate.dialect.PostgreSQLDialect;
44  import org.hibernate.dialect.SAPDBDialect;
45  import org.hibernate.dialect.SQLServerDialect;
46  import org.hibernate.dialect.Sybase11Dialect;
47  import org.hibernate.dialect.SybaseASE15Dialect;
48  import org.hibernate.dialect.SybaseAnywhereDialect;
49  import org.hibernate.dialect.SybaseDialect;
50  
51  /**
52   * <a href="DBFactoryImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Alexander Chow
55   * @author Brian Wing Shun Chan
56   */
57  @SuppressWarnings("deprecation")
58  public class DBFactoryImpl implements DBFactory {
59  
60      public DB getDB() {
61          if (_db == null) {
62              try {
63                  if (_log.isInfoEnabled()) {
64                      _log.info("Using dialect " + PropsValues.HIBERNATE_DIALECT);
65                  }
66  
67                  Dialect dialect = (Dialect)Class.forName(
68                      PropsValues.HIBERNATE_DIALECT).newInstance();
69  
70                  setDB(dialect);
71              }
72              catch (Exception e) {
73                  _log.error(e, e);
74              }
75          }
76  
77          return _db;
78      }
79  
80      public DB getDB(Object dialect) {
81          DB db = null;
82  
83          if (dialect instanceof DB2Dialect) {
84              if (dialect instanceof DerbyDialect) {
85                  db = DerbyDB.getInstance();
86              }
87              else {
88                  db = DB2DB.getInstance();
89              }
90          }
91          else if (dialect instanceof HSQLDialect) {
92              db = HypersonicDB.getInstance();
93          }
94          else if (dialect instanceof InformixDialect) {
95              db = InformixDB.getInstance();
96          }
97          else if (dialect instanceof IngresDialect) {
98              db = IngresDB.getInstance();
99          }
100         else if (dialect instanceof InterbaseDialect) {
101             if (dialect instanceof FirebirdDialect) {
102                 db = FirebirdDB.getInstance();
103             }
104             else {
105                 db = InterBaseDB.getInstance();
106             }
107         }
108         else if (dialect instanceof JDataStoreDialect) {
109             db = JDataStoreDB.getInstance();
110         }
111         else if (dialect instanceof MySQLDialect) {
112             db = MySQLDB.getInstance();
113         }
114         else if (dialect instanceof Oracle8iDialect ||
115                  dialect instanceof Oracle9Dialect) {
116 
117             db = OracleDB.getInstance();
118         }
119         else if (dialect instanceof PostgreSQLDialect) {
120             db = PostgreSQLDB.getInstance();
121         }
122         else if (dialect instanceof SAPDBDialect) {
123             db = SAPDB.getInstance();
124         }
125         else if (dialect instanceof SQLServerDialect) {
126             db = SQLServerDB.getInstance();
127         }
128         else if (dialect instanceof SybaseDialect ||
129                  dialect instanceof Sybase11Dialect ||
130                  dialect instanceof SybaseAnywhereDialect ||
131                  dialect instanceof SybaseASE15Dialect) {
132 
133             db = SybaseDB.getInstance();
134         }
135 
136         return db;
137     }
138 
139     public DB getDB(String type) {
140         DB db = null;
141 
142         if (type.equals(DB.TYPE_DB2)) {
143             db = DB2DB.getInstance();
144         }
145         else if (type.equals(DB.TYPE_DERBY)) {
146             db = DerbyDB.getInstance();
147         }
148         else if (type.equals(DB.TYPE_FIREBIRD)) {
149             db = FirebirdDB.getInstance();
150         }
151         else if (type.equals(DB.TYPE_HYPERSONIC)) {
152             db = HypersonicDB.getInstance();
153         }
154         else if (type.equals(DB.TYPE_INFORMIX)) {
155             db = InformixDB.getInstance();
156         }
157         else if (type.equals(DB.TYPE_INGRES)) {
158             db = IngresDB.getInstance();
159         }
160         else if (type.equals(DB.TYPE_INTERBASE)) {
161             db = InterBaseDB.getInstance();
162         }
163         else if (type.equals(DB.TYPE_JDATASTORE)) {
164             db = JDataStoreDB.getInstance();
165         }
166         else if (type.equals(DB.TYPE_MYSQL)) {
167             db = MySQLDB.getInstance();
168         }
169         else if (type.equals(DB.TYPE_ORACLE)) {
170             db = OracleDB.getInstance();
171         }
172         else if (type.equals(DB.TYPE_POSTGRESQL)) {
173             db = PostgreSQLDB.getInstance();
174         }
175         else if (type.equals(DB.TYPE_SAP)) {
176             db = SAPDB.getInstance();
177         }
178         else if (type.equals(DB.TYPE_SQLSERVER)) {
179             db = SQLServerDB.getInstance();
180         }
181         else if (type.equals(DB.TYPE_SYBASE)) {
182             db = SybaseDB.getInstance();
183         }
184 
185         return db;
186     }
187 
188     public void setDB(Object dialect) {
189         if (_db == null) {
190             _db = getDB(dialect);
191 
192             if (_db == null) {
193                 _log.error(
194                     "No DB implementation exists for " +
195                         dialect.getClass().getName());
196             }
197             else {
198                 if (_log.isDebugEnabled()) {
199                     _log.debug(
200                         "Using DB implementation " + _db.getClass().getName() +
201                             " for " + dialect.getClass().getName());
202                 }
203             }
204         }
205     }
206 
207     public void setDB(String type) {
208         if (_db == null) {
209             _db = getDB(type);
210 
211             if (_db == null) {
212                 _log.error("No DB implementation exists for " + type);
213             }
214             else {
215                 if (_log.isDebugEnabled()) {
216                     _log.debug(
217                         "Using DB implementation " + _db.getClass().getName() +
218                             " for " + type);
219                 }
220             }
221         }
222     }
223 
224     private static Log _log = LogFactoryUtil.getLog(DBFactoryImpl.class);
225 
226     private static DB _db;
227 
228 }