001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.dao.db;
016    
017    import com.liferay.portal.kernel.dao.db.DB;
018    import com.liferay.portal.kernel.dao.db.DBFactory;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.util.PropsValues;
022    
023    import org.hibernate.dialect.DB2Dialect;
024    import org.hibernate.dialect.DerbyDialect;
025    import org.hibernate.dialect.Dialect;
026    import org.hibernate.dialect.FirebirdDialect;
027    import org.hibernate.dialect.HSQLDialect;
028    import org.hibernate.dialect.InformixDialect;
029    import org.hibernate.dialect.IngresDialect;
030    import org.hibernate.dialect.InterbaseDialect;
031    import org.hibernate.dialect.JDataStoreDialect;
032    import org.hibernate.dialect.MySQLDialect;
033    import org.hibernate.dialect.Oracle8iDialect;
034    import org.hibernate.dialect.Oracle9Dialect;
035    import org.hibernate.dialect.PostgreSQLDialect;
036    import org.hibernate.dialect.SAPDBDialect;
037    import org.hibernate.dialect.SQLServerDialect;
038    import org.hibernate.dialect.Sybase11Dialect;
039    import org.hibernate.dialect.SybaseASE15Dialect;
040    import org.hibernate.dialect.SybaseAnywhereDialect;
041    import org.hibernate.dialect.SybaseDialect;
042    
043    /**
044     * @author Alexander Chow
045     * @author Brian Wing Shun Chan
046     */
047    @SuppressWarnings("deprecation")
048    public class DBFactoryImpl implements DBFactory {
049    
050            public DB getDB() {
051                    if (_db == null) {
052                            try {
053                                    if (_log.isInfoEnabled()) {
054                                            _log.info("Using dialect " + PropsValues.HIBERNATE_DIALECT);
055                                    }
056    
057                                    Dialect dialect = (Dialect)Class.forName(
058                                            PropsValues.HIBERNATE_DIALECT).newInstance();
059    
060                                    setDB(dialect);
061                            }
062                            catch (Exception e) {
063                                    _log.error(e, e);
064                            }
065                    }
066    
067                    return _db;
068            }
069    
070            public DB getDB(Object dialect) {
071                    DB db = null;
072    
073                    if (dialect instanceof DB2Dialect) {
074                            if (dialect instanceof DerbyDialect) {
075                                    db = DerbyDB.getInstance();
076                            }
077                            else {
078                                    db = DB2DB.getInstance();
079                            }
080                    }
081                    else if (dialect instanceof HSQLDialect) {
082                            db = HypersonicDB.getInstance();
083                    }
084                    else if (dialect instanceof InformixDialect) {
085                            db = InformixDB.getInstance();
086                    }
087                    else if (dialect instanceof IngresDialect) {
088                            db = IngresDB.getInstance();
089                    }
090                    else if (dialect instanceof InterbaseDialect) {
091                            if (dialect instanceof FirebirdDialect) {
092                                    db = FirebirdDB.getInstance();
093                            }
094                            else {
095                                    db = InterBaseDB.getInstance();
096                            }
097                    }
098                    else if (dialect instanceof JDataStoreDialect) {
099                            db = JDataStoreDB.getInstance();
100                    }
101                    else if (dialect instanceof MySQLDialect) {
102                            db = MySQLDB.getInstance();
103                    }
104                    else if (dialect instanceof Oracle8iDialect ||
105                                     dialect instanceof Oracle9Dialect) {
106    
107                            db = OracleDB.getInstance();
108                    }
109                    else if (dialect instanceof PostgreSQLDialect) {
110                            db = PostgreSQLDB.getInstance();
111                    }
112                    else if (dialect instanceof SAPDBDialect) {
113                            db = SAPDB.getInstance();
114                    }
115                    else if (dialect instanceof SQLServerDialect) {
116                            db = SQLServerDB.getInstance();
117                    }
118                    else if (dialect instanceof SybaseDialect ||
119                                     dialect instanceof Sybase11Dialect ||
120                                     dialect instanceof SybaseAnywhereDialect ||
121                                     dialect instanceof SybaseASE15Dialect) {
122    
123                            db = SybaseDB.getInstance();
124                    }
125    
126                    return db;
127            }
128    
129            public DB getDB(String type) {
130                    DB db = null;
131    
132                    if (type.equals(DB.TYPE_DB2)) {
133                            db = DB2DB.getInstance();
134                    }
135                    else if (type.equals(DB.TYPE_DERBY)) {
136                            db = DerbyDB.getInstance();
137                    }
138                    else if (type.equals(DB.TYPE_FIREBIRD)) {
139                            db = FirebirdDB.getInstance();
140                    }
141                    else if (type.equals(DB.TYPE_HYPERSONIC)) {
142                            db = HypersonicDB.getInstance();
143                    }
144                    else if (type.equals(DB.TYPE_INFORMIX)) {
145                            db = InformixDB.getInstance();
146                    }
147                    else if (type.equals(DB.TYPE_INGRES)) {
148                            db = IngresDB.getInstance();
149                    }
150                    else if (type.equals(DB.TYPE_INTERBASE)) {
151                            db = InterBaseDB.getInstance();
152                    }
153                    else if (type.equals(DB.TYPE_JDATASTORE)) {
154                            db = JDataStoreDB.getInstance();
155                    }
156                    else if (type.equals(DB.TYPE_MYSQL)) {
157                            db = MySQLDB.getInstance();
158                    }
159                    else if (type.equals(DB.TYPE_ORACLE)) {
160                            db = OracleDB.getInstance();
161                    }
162                    else if (type.equals(DB.TYPE_POSTGRESQL)) {
163                            db = PostgreSQLDB.getInstance();
164                    }
165                    else if (type.equals(DB.TYPE_SAP)) {
166                            db = SAPDB.getInstance();
167                    }
168                    else if (type.equals(DB.TYPE_SQLSERVER)) {
169                            db = SQLServerDB.getInstance();
170                    }
171                    else if (type.equals(DB.TYPE_SYBASE)) {
172                            db = SybaseDB.getInstance();
173                    }
174    
175                    return db;
176            }
177    
178            public void setDB(Object dialect) {
179                    if (_db == null) {
180                            _db = getDB(dialect);
181    
182                            if (_db == null) {
183                                    _log.error(
184                                            "No DB implementation exists for " +
185                                                    dialect.getClass().getName());
186                            }
187                            else {
188                                    if (_log.isDebugEnabled()) {
189                                            _log.debug(
190                                                    "Using DB implementation " + _db.getClass().getName() +
191                                                            " for " + dialect.getClass().getName());
192                                    }
193                            }
194                    }
195            }
196    
197            public void setDB(String type) {
198                    if (_db == null) {
199                            _db = getDB(type);
200    
201                            if (_db == null) {
202                                    _log.error("No DB implementation exists for " + type);
203                            }
204                            else {
205                                    if (_log.isDebugEnabled()) {
206                                            _log.debug(
207                                                    "Using DB implementation " + _db.getClass().getName() +
208                                                            " for " + type);
209                                    }
210                            }
211                    }
212            }
213    
214            private static Log _log = LogFactoryUtil.getLog(DBFactoryImpl.class);
215    
216            private static DB _db;
217    
218    }