001package co.codewizards.cloudstore.local;
002
003import static co.codewizards.cloudstore.core.util.StringUtil.*;
004import static co.codewizards.cloudstore.core.util.Util.*;
005
006import java.io.File;
007import java.sql.Connection;
008import java.sql.DriverManager;
009import java.sql.SQLException;
010import java.util.Map;
011
012import javax.jdo.PersistenceManagerFactory;
013
014import org.slf4j.Logger;
015import org.slf4j.LoggerFactory;
016
017/**
018 * Factory creating JDBC connections to the repository's derby database.
019 * <p>
020 * <b>Important: This is for maintenance only!</b> Ordinary operations work with a {@link PersistenceManagerFactory}
021 * which is managed by {@link LocalRepoManagerImpl}.
022 * @author Marco หงุ่ยตระกูล-Schulze - marco at codewizards dot co
023 */
024public class JdbcConnectionFactory {
025        private static final Logger logger = LoggerFactory.getLogger(JdbcConnectionFactory.class);
026
027        private final File localRoot;
028
029        private String connectionURL;
030
031        private String connectionDriverName;
032
033        private String connectionUserName;
034
035        private String connectionPassword;
036
037        public JdbcConnectionFactory(final File localRoot) {
038                this.localRoot = assertNotNull("localRoot", localRoot);
039                if (!localRoot.isDirectory())
040                        throw new IllegalArgumentException("The given localRoot is not an existing directory: " + localRoot.getAbsolutePath());
041
042                initProperties();
043                initDriverClass();
044        }
045
046        private void initProperties() {
047                Map<String, String> persistenceProperties = new PersistencePropertiesProvider(localRoot).getPersistenceProperties(false);
048                connectionDriverName = persistenceProperties.get(PersistencePropertiesEnum.CONNECTION_DRIVER_NAME.key);
049                connectionURL = persistenceProperties.get(PersistencePropertiesEnum.CONNECTION_URL.key);
050                connectionUserName = persistenceProperties.get(PersistencePropertiesEnum.CONNECTION_USER_NAME.key);
051                connectionPassword = persistenceProperties.get(PersistencePropertiesEnum.CONNECTION_PASSWORD.key);
052        };
053
054        private void initDriverClass() {
055                if (isEmpty(connectionDriverName))
056                        return;
057
058                try {
059                        Class.forName(connectionDriverName);
060                } catch (Throwable e) { // Might theoretically be a link error (i.e. a sub-class of Error instead of Exception) => catch Throwable
061                        logger.warn("initDriverClass" + e, e);
062                }
063        }
064
065        public Connection createConnection() throws SQLException {
066                if (isEmpty(connectionUserName) && isEmpty(connectionPassword))
067                        return DriverManager.getConnection(connectionURL);
068                else
069                        return DriverManager.getConnection(connectionURL, connectionUserName, connectionPassword);
070        }
071}