001    /**
002     * Copyright (c) 2000-2013 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.security.pacl.DoPrivileged;
023    import com.liferay.portal.kernel.util.InstanceFactory;
024    import com.liferay.portal.util.PropsValues;
025    
026    import org.hibernate.dialect.DB2Dialect;
027    import org.hibernate.dialect.DerbyDialect;
028    import org.hibernate.dialect.Dialect;
029    import org.hibernate.dialect.FirebirdDialect;
030    import org.hibernate.dialect.HSQLDialect;
031    import org.hibernate.dialect.InformixDialect;
032    import org.hibernate.dialect.IngresDialect;
033    import org.hibernate.dialect.InterbaseDialect;
034    import org.hibernate.dialect.JDataStoreDialect;
035    import org.hibernate.dialect.MySQLDialect;
036    import org.hibernate.dialect.Oracle8iDialect;
037    import org.hibernate.dialect.Oracle9Dialect;
038    import org.hibernate.dialect.PostgreSQLDialect;
039    import org.hibernate.dialect.SAPDBDialect;
040    import org.hibernate.dialect.SQLServerDialect;
041    import org.hibernate.dialect.Sybase11Dialect;
042    import org.hibernate.dialect.SybaseASE15Dialect;
043    import org.hibernate.dialect.SybaseAnywhereDialect;
044    import org.hibernate.dialect.SybaseDialect;
045    
046    /**
047     * @author Alexander Chow
048     * @author Brian Wing Shun Chan
049     */
050    @DoPrivileged
051    @SuppressWarnings("deprecation")
052    public class DBFactoryImpl implements DBFactory {
053    
054            public DB getDB() {
055                    if (_db == null) {
056                            try {
057                                    if (_log.isInfoEnabled()) {
058                                            _log.info("Using dialect " + PropsValues.HIBERNATE_DIALECT);
059                                    }
060    
061                                    Dialect dialect = (Dialect)InstanceFactory.newInstance(
062                                            PropsValues.HIBERNATE_DIALECT);
063    
064                                    setDB(dialect);
065                            }
066                            catch (Exception e) {
067                                    _log.error(e, e);
068                            }
069                    }
070    
071                    return _db;
072            }
073    
074            public DB getDB(Object dialect) {
075                    DB db = null;
076    
077                    if (dialect instanceof DialectImpl) {
078                            DialectImpl dialectImpl = (DialectImpl)dialect;
079    
080                            dialect = dialectImpl.getWrappedDialect();
081                    }
082    
083                    if (dialect instanceof DB2Dialect) {
084                            if (dialect instanceof DerbyDialect) {
085                                    db = DerbyDB.getInstance();
086                            }
087                            else {
088                                    db = DB2DB.getInstance();
089                            }
090                    }
091                    else if (dialect instanceof HSQLDialect) {
092                            db = HypersonicDB.getInstance();
093                    }
094                    else if (dialect instanceof InformixDialect) {
095                            db = InformixDB.getInstance();
096                    }
097                    else if (dialect instanceof IngresDialect) {
098                            db = IngresDB.getInstance();
099                    }
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                    _db = getDB(dialect);
190    
191                    if (_db == null) {
192                            Class<?> clazz = dialect.getClass();
193    
194                            _log.error("No DB implementation exists for " + clazz.getName());
195                    }
196                    else {
197                            if (_log.isDebugEnabled()) {
198                                    Class<?> dbClazz = _db.getClass();
199                                    Class<?> dialectClazz = dialect.getClass();
200    
201                                    _log.debug(
202                                            "Using DB implementation " + dbClazz.getName() + " for " +
203                                                    dialectClazz.getName());
204                            }
205                    }
206            }
207    
208            public void setDB(String type) {
209                    if (_db != null) {
210                            return;
211                    }
212    
213                    _db = getDB(type);
214    
215                    if (_db == null) {
216                            _log.error("No DB implementation exists for " + type);
217                    }
218                    else {
219                            if (_log.isDebugEnabled()) {
220                                    Class<?> clazz = _db.getClass();
221    
222                                    _log.debug(
223                                            "Using DB implementation " + clazz.getName() + " for " +
224                                                    type);
225                            }
226                    }
227            }
228    
229            private static Log _log = LogFactoryUtil.getLog(DBFactoryImpl.class);
230    
231            private static DB _db;
232    
233    }