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            @Override
055            public DB getDB() {
056                    if (_db == null) {
057                            try {
058                                    if (_log.isInfoEnabled()) {
059                                            _log.info("Using dialect " + PropsValues.HIBERNATE_DIALECT);
060                                    }
061    
062                                    Dialect dialect = (Dialect)InstanceFactory.newInstance(
063                                            PropsValues.HIBERNATE_DIALECT);
064    
065                                    setDB(dialect);
066                            }
067                            catch (Exception e) {
068                                    _log.error(e, e);
069                            }
070                    }
071    
072                    return _db;
073            }
074    
075            @Override
076            public DB getDB(Object dialect) {
077                    DB db = null;
078    
079                    if (dialect instanceof DialectImpl) {
080                            DialectImpl dialectImpl = (DialectImpl)dialect;
081    
082                            dialect = dialectImpl.getWrappedDialect();
083                    }
084    
085                    if (dialect instanceof DB2Dialect) {
086                            if (dialect instanceof DerbyDialect) {
087                                    db = DerbyDB.getInstance();
088                            }
089                            else {
090                                    db = DB2DB.getInstance();
091                            }
092                    }
093                    else if (dialect instanceof HSQLDialect) {
094                            db = HypersonicDB.getInstance();
095                    }
096                    else if (dialect instanceof InformixDialect) {
097                            db = InformixDB.getInstance();
098                    }
099                    else if (dialect instanceof IngresDialect) {
100                            db = IngresDB.getInstance();
101                    }
102                    else if (dialect instanceof InterbaseDialect) {
103                            if (dialect instanceof FirebirdDialect) {
104                                    db = FirebirdDB.getInstance();
105                            }
106                            else {
107                                    db = InterBaseDB.getInstance();
108                            }
109                    }
110                    else if (dialect instanceof JDataStoreDialect) {
111                            db = JDataStoreDB.getInstance();
112                    }
113                    else if (dialect instanceof MySQLDialect) {
114                            db = MySQLDB.getInstance();
115                    }
116                    else if (dialect instanceof Oracle8iDialect ||
117                                     dialect instanceof Oracle9Dialect) {
118    
119                            db = OracleDB.getInstance();
120                    }
121                    else if (dialect instanceof PostgreSQLDialect) {
122                            db = PostgreSQLDB.getInstance();
123                    }
124                    else if (dialect instanceof SAPDBDialect) {
125                            db = SAPDB.getInstance();
126                    }
127                    else if (dialect instanceof SQLServerDialect) {
128                            db = SQLServerDB.getInstance();
129                    }
130                    else if (dialect instanceof SybaseDialect ||
131                                     dialect instanceof Sybase11Dialect ||
132                                     dialect instanceof SybaseAnywhereDialect ||
133                                     dialect instanceof SybaseASE15Dialect) {
134    
135                            db = SybaseDB.getInstance();
136                    }
137    
138                    return db;
139            }
140    
141            @Override
142            public DB getDB(String type) {
143                    DB db = null;
144    
145                    if (type.equals(DB.TYPE_DB2)) {
146                            db = DB2DB.getInstance();
147                    }
148                    else if (type.equals(DB.TYPE_DERBY)) {
149                            db = DerbyDB.getInstance();
150                    }
151                    else if (type.equals(DB.TYPE_FIREBIRD)) {
152                            db = FirebirdDB.getInstance();
153                    }
154                    else if (type.equals(DB.TYPE_HYPERSONIC)) {
155                            db = HypersonicDB.getInstance();
156                    }
157                    else if (type.equals(DB.TYPE_INFORMIX)) {
158                            db = InformixDB.getInstance();
159                    }
160                    else if (type.equals(DB.TYPE_INGRES)) {
161                            db = IngresDB.getInstance();
162                    }
163                    else if (type.equals(DB.TYPE_INTERBASE)) {
164                            db = InterBaseDB.getInstance();
165                    }
166                    else if (type.equals(DB.TYPE_JDATASTORE)) {
167                            db = JDataStoreDB.getInstance();
168                    }
169                    else if (type.equals(DB.TYPE_MYSQL)) {
170                            db = MySQLDB.getInstance();
171                    }
172                    else if (type.equals(DB.TYPE_ORACLE)) {
173                            db = OracleDB.getInstance();
174                    }
175                    else if (type.equals(DB.TYPE_POSTGRESQL)) {
176                            db = PostgreSQLDB.getInstance();
177                    }
178                    else if (type.equals(DB.TYPE_SAP)) {
179                            db = SAPDB.getInstance();
180                    }
181                    else if (type.equals(DB.TYPE_SQLSERVER)) {
182                            db = SQLServerDB.getInstance();
183                    }
184                    else if (type.equals(DB.TYPE_SYBASE)) {
185                            db = SybaseDB.getInstance();
186                    }
187    
188                    return db;
189            }
190    
191            @Override
192            public void setDB(Object dialect) {
193                    _db = getDB(dialect);
194    
195                    if (_db == null) {
196                            Class<?> clazz = dialect.getClass();
197    
198                            _log.error("No DB implementation exists for " + clazz.getName());
199                    }
200                    else {
201                            if (_log.isDebugEnabled()) {
202                                    Class<?> dbClazz = _db.getClass();
203                                    Class<?> dialectClazz = dialect.getClass();
204    
205                                    _log.debug(
206                                            "Using DB implementation " + dbClazz.getName() + " for " +
207                                                    dialectClazz.getName());
208                            }
209                    }
210            }
211    
212            @Override
213            public void setDB(String type) {
214                    if (_db != null) {
215                            return;
216                    }
217    
218                    _db = getDB(type);
219    
220                    if (_db == null) {
221                            _log.error("No DB implementation exists for " + type);
222                    }
223                    else {
224                            if (_log.isDebugEnabled()) {
225                                    Class<?> clazz = _db.getClass();
226    
227                                    _log.debug(
228                                            "Using DB implementation " + clazz.getName() + " for " +
229                                                    type);
230                            }
231                    }
232            }
233    
234            private static Log _log = LogFactoryUtil.getLog(DBFactoryImpl.class);
235    
236            private static DB _db;
237    
238    }