001    /**
002     * Copyright (c) 2000-2012 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.dao.orm.hibernate.DialectImpl;
018    import com.liferay.portal.kernel.dao.db.DB;
019    import com.liferay.portal.kernel.dao.db.DBFactory;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.InstanceFactory;
023    import com.liferay.portal.util.PropsValues;
024    
025    import org.hibernate.dialect.DB2Dialect;
026    import org.hibernate.dialect.DerbyDialect;
027    import org.hibernate.dialect.Dialect;
028    import org.hibernate.dialect.FirebirdDialect;
029    import org.hibernate.dialect.HSQLDialect;
030    import org.hibernate.dialect.InformixDialect;
031    import org.hibernate.dialect.IngresDialect;
032    import org.hibernate.dialect.InterbaseDialect;
033    import org.hibernate.dialect.JDataStoreDialect;
034    import org.hibernate.dialect.MySQLDialect;
035    import org.hibernate.dialect.Oracle8iDialect;
036    import org.hibernate.dialect.Oracle9Dialect;
037    import org.hibernate.dialect.PostgreSQLDialect;
038    import org.hibernate.dialect.SAPDBDialect;
039    import org.hibernate.dialect.SQLServerDialect;
040    import org.hibernate.dialect.Sybase11Dialect;
041    import org.hibernate.dialect.SybaseASE15Dialect;
042    import org.hibernate.dialect.SybaseAnywhereDialect;
043    import org.hibernate.dialect.SybaseDialect;
044    
045    /**
046     * @author Alexander Chow
047     * @author Brian Wing Shun Chan
048     */
049    @SuppressWarnings("deprecation")
050    public class DBFactoryImpl implements DBFactory {
051    
052            public DB getDB() {
053                    if (_db == null) {
054                            try {
055                                    if (_log.isInfoEnabled()) {
056                                            _log.info("Using dialect " + PropsValues.HIBERNATE_DIALECT);
057                                    }
058    
059                                    Dialect dialect = (Dialect)InstanceFactory.newInstance(
060                                            PropsValues.HIBERNATE_DIALECT);
061    
062                                    setDB(dialect);
063                            }
064                            catch (Exception e) {
065                                    _log.error(e, e);
066                            }
067                    }
068    
069                    return _db;
070            }
071    
072            public DB getDB(Object dialect) {
073                    DB db = null;
074    
075                    if (dialect instanceof DialectImpl) {
076                            DialectImpl dialectImpl = (DialectImpl)dialect;
077    
078                            dialect = dialectImpl.getWrappedDialect();
079                    }
080    
081                    if (dialect instanceof DB2Dialect) {
082                            if (dialect instanceof DerbyDialect) {
083                                    db = DerbyDB.getInstance();
084                            }
085                            else {
086                                    db = DB2DB.getInstance();
087                            }
088                    }
089                    else if (dialect instanceof HSQLDialect) {
090                            db = HypersonicDB.getInstance();
091                    }
092                    else if (dialect instanceof InformixDialect) {
093                            db = InformixDB.getInstance();
094                    }
095                    else if (dialect instanceof IngresDialect) {
096                            db = IngresDB.getInstance();
097                    }
098                    else if (dialect instanceof InterbaseDialect) {
099                            if (dialect instanceof FirebirdDialect) {
100                                    db = FirebirdDB.getInstance();
101                            }
102                            else {
103                                    db = InterBaseDB.getInstance();
104                            }
105                    }
106                    else if (dialect instanceof JDataStoreDialect) {
107                            db = JDataStoreDB.getInstance();
108                    }
109                    else if (dialect instanceof MySQLDialect) {
110                            db = MySQLDB.getInstance();
111                    }
112                    else if (dialect instanceof Oracle8iDialect ||
113                                     dialect instanceof Oracle9Dialect) {
114    
115                            db = OracleDB.getInstance();
116                    }
117                    else if (dialect instanceof PostgreSQLDialect) {
118                            db = PostgreSQLDB.getInstance();
119                    }
120                    else if (dialect instanceof SAPDBDialect) {
121                            db = SAPDB.getInstance();
122                    }
123                    else if (dialect instanceof SQLServerDialect) {
124                            db = SQLServerDB.getInstance();
125                    }
126                    else if (dialect instanceof SybaseDialect ||
127                                     dialect instanceof Sybase11Dialect ||
128                                     dialect instanceof SybaseAnywhereDialect ||
129                                     dialect instanceof SybaseASE15Dialect) {
130    
131                            db = SybaseDB.getInstance();
132                    }
133    
134                    return db;
135            }
136    
137            public DB getDB(String type) {
138                    DB db = null;
139    
140                    if (type.equals(DB.TYPE_DB2)) {
141                            db = DB2DB.getInstance();
142                    }
143                    else if (type.equals(DB.TYPE_DERBY)) {
144                            db = DerbyDB.getInstance();
145                    }
146                    else if (type.equals(DB.TYPE_FIREBIRD)) {
147                            db = FirebirdDB.getInstance();
148                    }
149                    else if (type.equals(DB.TYPE_HYPERSONIC)) {
150                            db = HypersonicDB.getInstance();
151                    }
152                    else if (type.equals(DB.TYPE_INFORMIX)) {
153                            db = InformixDB.getInstance();
154                    }
155                    else if (type.equals(DB.TYPE_INGRES)) {
156                            db = IngresDB.getInstance();
157                    }
158                    else if (type.equals(DB.TYPE_INTERBASE)) {
159                            db = InterBaseDB.getInstance();
160                    }
161                    else if (type.equals(DB.TYPE_JDATASTORE)) {
162                            db = JDataStoreDB.getInstance();
163                    }
164                    else if (type.equals(DB.TYPE_MYSQL)) {
165                            db = MySQLDB.getInstance();
166                    }
167                    else if (type.equals(DB.TYPE_ORACLE)) {
168                            db = OracleDB.getInstance();
169                    }
170                    else if (type.equals(DB.TYPE_POSTGRESQL)) {
171                            db = PostgreSQLDB.getInstance();
172                    }
173                    else if (type.equals(DB.TYPE_SAP)) {
174                            db = SAPDB.getInstance();
175                    }
176                    else if (type.equals(DB.TYPE_SQLSERVER)) {
177                            db = SQLServerDB.getInstance();
178                    }
179                    else if (type.equals(DB.TYPE_SYBASE)) {
180                            db = SybaseDB.getInstance();
181                    }
182    
183                    return db;
184            }
185    
186            public void setDB(Object dialect) {
187                    _db = getDB(dialect);
188    
189                    if (_db == null) {
190                            Class<?> clazz = dialect.getClass();
191    
192                            _log.error("No DB implementation exists for " + clazz.getName());
193                    }
194                    else {
195                            if (_log.isDebugEnabled()) {
196                                    Class<?> dbClazz = _db.getClass();
197                                    Class<?> dialectClazz = dialect.getClass();
198    
199                                    _log.debug(
200                                            "Using DB implementation " + dbClazz.getName() + " for " +
201                                                    dialectClazz.getName());
202                            }
203                    }
204            }
205    
206            public void setDB(String type) {
207                    if (_db != null) {
208                            return;
209                    }
210    
211                    _db = getDB(type);
212    
213                    if (_db == null) {
214                            _log.error("No DB implementation exists for " + type);
215                    }
216                    else {
217                            if (_log.isDebugEnabled()) {
218                                    Class<?> clazz = _db.getClass();
219    
220                                    _log.debug(
221                                            "Using DB implementation " + clazz.getName() + " for " +
222                                                    type);
223                            }
224                    }
225            }
226    
227            private static Log _log = LogFactoryUtil.getLog(DBFactoryImpl.class);
228    
229            private static DB _db;
230    
231    }