Update groupId to org.onap.ccsdk.sli
[ccsdk/sli/core.git] / dblib / provider / src / main / java / org / onap / ccsdk / sli / core / dblib / factory / DBConfigFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * onap
4  * ================================================================================
5  * Copyright (C) 2016 - 2017 ONAP
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.ccsdk.sli.core.dblib.factory;
22
23
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.Properties;
27
28 import org.onap.ccsdk.sli.core.dblib.config.BaseDBConfiguration;
29 import org.onap.ccsdk.sli.core.dblib.config.DbConfigPool;
30 import org.onap.ccsdk.sli.core.dblib.config.JDBCConfiguration;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * @version $Revision: 1.1 $
35  * Change Log
36  * Author         Date     Comments
37  * ============== ======== ====================================================
38  * Rich Tabedzki  01/17/08 Initial version
39  */
40 public class DBConfigFactory {
41
42         public static DbConfigPool createConfig(Properties resource) {
43                 return getConfigparams(resource);
44         }
45
46         static DbConfigPool getConfigparams(Properties properties){
47                 DbConfigPool xmlConfig = new DbConfigPool(properties);
48                 ArrayList<Properties> propertySets = new ArrayList<Properties>();
49
50                 if("JDBC".equalsIgnoreCase(xmlConfig.getType())) {
51                         String hosts = properties.getProperty(BaseDBConfiguration.DATABASE_HOSTS);
52                         if(hosts == null || hosts.isEmpty()) {
53                                 propertySets.add(properties);
54                         } else {
55                                 String[] newhost = hosts.split(",");
56                                 for(int i=0; i< newhost.length; i++) {
57                                         Properties localset = new Properties();
58                                         localset.putAll(properties);
59                                         String url = localset.getProperty(BaseDBConfiguration.DATABASE_URL);
60                                         if(url.contains("DBHOST"))
61                                                 url = url.replace("DBHOST", newhost[i]);
62                                         if(url.contains("dbhost"))
63                                                 url = url.replace("dbhost", newhost[i]);
64                                         localset.setProperty(BaseDBConfiguration.DATABASE_URL, url);
65                                         localset.setProperty(BaseDBConfiguration.CONNECTION_NAME, newhost[i]);
66                                         propertySets.add(localset);
67                                 }
68                         }
69                 } else {
70                         propertySets.add(properties);
71                 }
72                 try {
73                         Iterator<Properties>  it = propertySets.iterator();
74                         while(it.hasNext()) {
75                                 BaseDBConfiguration config = parse(it.next());
76                                 xmlConfig.addConfiguration(config);
77                         }
78
79                 } catch (Exception e) {
80                         LoggerFactory.getLogger(DBConfigFactory.class).warn("",e);
81                 }
82
83                 return xmlConfig;
84         }
85
86         public static BaseDBConfiguration parse(Properties props) throws Exception {
87
88                 String type = props.getProperty(BaseDBConfiguration.DATABASE_TYPE);
89
90                 BaseDBConfiguration config = null;
91
92                 if("JDBC".equalsIgnoreCase(type)) {
93                         config = new JDBCConfiguration(props);
94                 }
95
96                 return config;
97
98         }
99 }