summaryrefslogtreecommitdiffstats
path: root/appc-dispatcher/appc-dispatcher-common/appc-data-access-lib/src/main/java/org/onap/appc/dao/util/AppcDatabaseConnectionPool.java
blob: 219739755939a02325095c958374f330ded1677f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * ============LICENSE_END=========================================================
 */

package org.onap.appc.dao.util;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import org.onap.appc.configuration.Configuration;
import org.onap.appc.configuration.ConfigurationFactory;
import org.onap.appc.dao.util.api.DBConnectionPoolService;
import org.onap.appc.dao.util.dbcp.DBConnectionPool;
import org.onap.appc.dao.util.exception.DBConnectionPoolException;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

/**
 * This class implements
 *
 * @see org.onap.appc.dao.util.dbcp.DBConnectionPool
 * that provides concrete implemenation of accessing appc database which basic setup
 * data would be got from global configuration.
 * @see org.onap.appc.configuration.Configuration
 * <p>
 * The singleton instance of this class has been instantiated by blueprint.
 * An example is shown in the {@link DBConnectionPoolService}
 */
public class AppcDatabaseConnectionPool implements DBConnectionPoolService {
    enum PropertyPattern {
        DBURL("org.onap.appc.db.url.%s"),
        USERNAME("org.onap.appc.db.user.%s"),
        PASSWORD("org.onap.appc.db.pass.%s"),
        DRIVER("org.onap.appc.db.jdbc.driver");

        private String pattern;

        PropertyPattern(String pattern) {
            this.pattern = pattern;
        }

        public String getPattern() {
            return pattern;
        }
    }

    private final EELFLogger logger = EELFManager.getInstance().getLogger(AppcDatabaseConnectionPool.class);

    private DBConnectionPool dbConnectionPool;
    private String dbName;

    public AppcDatabaseConnectionPool() {
        // do nothing
    }

    public AppcDatabaseConnectionPool(String dbUrl, String userName, String password, String jdbcDriver) {
        dbConnectionPool = new DBConnectionPool(dbUrl, userName, password, jdbcDriver);
    }

    /**
     * Injected by blueprint
     *
     * @param dbName
     */
    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    /**
     * Bean init method used by blueprint
     */
    public void init() {
        Configuration configuration = ConfigurationFactory.getConfiguration();
        String dbUrl = getConnectionProperty(configuration, PropertyPattern.DBURL);
        String userName = getConnectionProperty(configuration, PropertyPattern.USERNAME);
        String password = getConnectionProperty(configuration, PropertyPattern.PASSWORD);
        String jdbcDriver = getJDBCDriver(configuration);

        dbConnectionPool = new DBConnectionPool(dbUrl, userName, password, jdbcDriver);

        // a simple health check
        Connection connection = null;
        try {
            connection = dbConnectionPool.getConnection();
        } catch (DBConnectionPoolException e) {
            logger.error("DB connection pool is created failed." +
                "Please make sure the provided information is correct.");
        }

        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                logger.error("DB connection cannot be closed:", e.getMessage());
            }
        }
    }

    /**
     * Bean destroy method used by blueprint
     */
    public void destroy() {
        if (dbConnectionPool != null) {
            dbConnectionPool.shutdown();
        }
    }

    /**
     * Get the connection from connection pool.
     *
     * @return Connection. If the provided db information is not correct,
     * the return value might be null.
     */
    @Override
    public Connection getConnection() throws DBConnectionPoolException {
        return dbConnectionPool.getConnection();
    }

    /**
     * Get dbcp status like active_status.
     * <p>
     * More details about status of DBConnectionPool,
     * go check {@link org.onap.appc.dao.util.dbcp.DBConnectionPool#getDataSourceStatus()}
     *
     * @return a map contains some dbcp information.
     */
    @Override
    public Map<String, Integer> getDataSourceStatus() {
        return dbConnectionPool.getDataSourceStatus();
    }

    private String getConnectionProperty(Configuration configuration, PropertyPattern propertyPattern) {
        String property = configuration.getProperty(String.format(propertyPattern.getPattern(), dbName), "");
        return property;
    }

    private String getJDBCDriver(Configuration configuration) {
        return configuration.getProperty(PropertyPattern.DRIVER.getPattern(), "");
    }
}