/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

package com.glaf.core.jdbc.datasource;

import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import com.glaf.core.config.Environment;
import com.glaf.core.config.SystemProperties;
import com.glaf.core.jdbc.connection.ConnectionProvider;
import com.glaf.core.jdbc.connection.ConnectionProviderFactory;
import com.glaf.core.util.PropertiesUtils;

public class DynamicDataSource extends AbstractRoutingDataSource {
	protected final static Log logger = LogFactory
			.getLog(DynamicDataSource.class);

	private static ConcurrentMap<Object, Object> targetDataSources = new ConcurrentHashMap<Object, Object>();

	private static Object defaultTargetDataSource;

	public DynamicDataSource() {

	}

	@Override
	public void afterPropertiesSet() {
		String filename = SystemProperties.getAppPath() + "/WEB-INF/conf/jdbc/"
				+ Environment.getCurrentSystemName() + ".jdbc.properties";
		Properties props = PropertiesUtils.loadFilePathResource(filename);
		if (props != null) {
			try {
				ConnectionProvider provider = ConnectionProviderFactory
						.createProvider(Environment.getCurrentSystemName(),
								props);
				targetDataSources.put(Environment.getCurrentSystemName(),
						provider.getDataSource());
				defaultTargetDataSource = provider.getDataSource();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		setDefaultTargetDataSource(defaultTargetDataSource);
		setTargetDataSources(targetDataSources);
		super.afterPropertiesSet();
	}

	@Override
	protected synchronized Object determineCurrentLookupKey() {
		return Environment.getCurrentSystemName();
	}

}
@@ -1767,16 +1767,25 @@
 Set() %7B%0A
+%09%09try %7B%0A%09
 %09%09String
@@ -1825,17 +1825,22 @@
 ppPath()
- 
+%0A%09%09%09%09%09
 + %22/WEB-
@@ -1855,16 +1855,17 @@
 /jdbc/%22%0A
+%09
 %09%09%09%09+ En
@@ -1919,16 +1919,17 @@
 rties%22;%0A
+%09
 %09%09Proper
@@ -1991,16 +1991,17 @@
 ame);%0A%09%09
+%09
 if (prop
@@ -2017,17 +2017,8 @@
 ) %7B%0A
-%09%09%09try %7B%0A
 %09%09%09%09
@@ -2302,16 +2302,20 @@
 ();%0A%09%09%09%7D
+%0A%09%09%7D
  catch (
@@ -2330,17 +2330,16 @@
 n ex) %7B%0A
-%09
 %09%09%09ex.pr
@@ -2354,25 +2354,41 @@
 Trace();%0A%09%09%09
-%7D
+logger.error(ex);
 %0A%09%09%7D%0A%09%09setDe

update DynamicDataSource