View Javadoc
1   /**
2    * Copyright 2010-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.property.processor;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  import java.util.Properties;
21  
22  import org.kuali.common.util.Mode;
23  import org.kuali.common.util.PropertyUtils;
24  import org.kuali.common.util.property.Constants;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  import org.springframework.util.Assert;
28  
29  public class JdbcUrlProcessor implements PropertyProcessor {
30  	private static final Logger logger = LoggerFactory.getLogger(JdbcUrlProcessor.class);
31  
32  	String jdbcUrlProperty = "jdbc.url";
33  	String dbVendorProperty = "db.vendor";
34  	Map<String, String> jdbcUrlFragments = getJdbcUrlFragments();
35  	Mode propertyOverwriteMode = Constants.DEFAULT_PROPERTY_OVERWRITE_MODE;
36  
37  	@Override
38  	public void process(Properties properties) {
39  		Assert.notNull(jdbcUrlProperty);
40  		Assert.notNull(dbVendorProperty);
41  		String jdbcUrl = properties.getProperty(jdbcUrlProperty);
42  		if (jdbcUrl == null) {
43  			logger.info(jdbcUrlProperty + " is not set");
44  			return;
45  		}
46  		String databaseVendor = getMatch(jdbcUrl, jdbcUrlFragments);
47  		if (databaseVendor != null) {
48  			PropertyUtils.addOrOverrideProperty(properties, dbVendorProperty, databaseVendor, propertyOverwriteMode);
49  		} else {
50  			logger.info("Could not identify a database vendor from url - [{}]", jdbcUrl);
51  		}
52  	}
53  
54  	/**
55  	 * Loop through <code>fragments.keySet()</code> to see if <code>string</code> contains any of the <code>fragments</code>. If there is a
56  	 * match with a <code>fragment</code>, return the corresponding value for the <code>fragment</code> from the map.
57  	 */
58  	protected String getMatch(String string, Map<String, String> fragments) {
59  		for (String fragment : fragments.keySet()) {
60  			if (string.contains(fragment)) {
61  				return fragments.get(fragment);
62  			}
63  		}
64  		return null;
65  	}
66  
67  	protected Map<String, String> getJdbcUrlFragments() {
68  		Map<String, String> m = new HashMap<String, String>();
69  		m.put(":hsqldb:", "hsql");
70  		m.put(":derby:", "derby");
71  		m.put(":postgresql:", "postgresql");
72  		m.put(":db2:", "db2");
73  		m.put(":mysql:", "mysql");
74  		m.put(":oracle:", "oracle");
75  		m.put(":h2:", "h2");
76  		return m;
77  	}
78  
79  }