001    /**
002     * Copyright 2010-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.common.util.property.processor;
017    
018    import java.util.HashMap;
019    import java.util.Map;
020    import java.util.Properties;
021    
022    import org.kuali.common.util.Mode;
023    import org.kuali.common.util.PropertyUtils;
024    import org.kuali.common.util.property.Constants;
025    import org.slf4j.Logger;
026    import org.slf4j.LoggerFactory;
027    import org.springframework.util.Assert;
028    
029    public class JdbcUrlProcessor implements PropertyProcessor {
030            private static final Logger logger = LoggerFactory.getLogger(JdbcUrlProcessor.class);
031    
032            String jdbcUrlProperty = "jdbc.url";
033            String dbVendorProperty = "db.vendor";
034            Map<String, String> jdbcUrlFragments = getJdbcUrlFragments();
035            Mode propertyOverwriteMode = Constants.DEFAULT_PROPERTY_OVERWRITE_MODE;
036    
037            @Override
038            public void process(Properties properties) {
039                    Assert.notNull(jdbcUrlProperty);
040                    Assert.notNull(dbVendorProperty);
041                    String jdbcUrl = properties.getProperty(jdbcUrlProperty);
042                    if (jdbcUrl == null) {
043                            logger.info(jdbcUrlProperty + " is not set");
044                            return;
045                    }
046                    String databaseVendor = getMatch(jdbcUrl, jdbcUrlFragments);
047                    if (databaseVendor != null) {
048                            PropertyUtils.addOrOverrideProperty(properties, dbVendorProperty, databaseVendor, propertyOverwriteMode);
049                    } else {
050                            logger.info("Could not identify a database vendor from url - [{}]", jdbcUrl);
051                    }
052            }
053    
054            /**
055             * Loop through <code>fragments.keySet()</code> to see if <code>string</code> contains any of the <code>fragments</code>. If there is a
056             * match with a <code>fragment</code>, return the corresponding value for the <code>fragment</code> from the map.
057             */
058            protected String getMatch(String string, Map<String, String> fragments) {
059                    for (String fragment : fragments.keySet()) {
060                            if (string.contains(fragment)) {
061                                    return fragments.get(fragment);
062                            }
063                    }
064                    return null;
065            }
066    
067            protected Map<String, String> getJdbcUrlFragments() {
068                    Map<String, String> m = new HashMap<String, String>();
069                    m.put(":hsqldb:", "hsql");
070                    m.put(":derby:", "derby");
071                    m.put(":postgresql:", "postgresql");
072                    m.put(":db2:", "db2");
073                    m.put(":mysql:", "mysql");
074                    m.put(":oracle:", "oracle");
075                    m.put(":h2:", "h2");
076                    return m;
077            }
078    
079    }