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