/*
 * Copyright (c) 2011-2018, Meituan Dianping. All Rights Reserved.
 *
 * 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.dianping.zebra.single.jdbc;

import com.dianping.zebra.Constants;
import com.dianping.zebra.config.ServiceConfigBuilder;
import com.dianping.zebra.exception.ZebraException;
import com.dianping.zebra.filter.JdbcFilter;
import com.dianping.zebra.util.StringUtils;

import javax.sql.DataSource;

import java.io.PrintWriter;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

public abstract class AbstractDataSource implements DataSource {

	public static final String LOCAL = Constants.CONFIG_MANAGER_TYPE_LOCAL;

	protected String configManagerType = LOCAL;

	protected Map<String, Object> serviceConfigs = ServiceConfigBuilder.newInstance().getConfigs();

	protected volatile List<JdbcFilter> filters;

	private int loginTimeout = 0;

	private PrintWriter out = null;

	protected void close() throws SQLException {
	}

	@Override
	public PrintWriter getLogWriter() throws SQLException {
		return out;
	}

	@Override
	public void setLogWriter(PrintWriter out) throws SQLException {
		this.out = out;
	}

	@Override
	public int getLoginTimeout() throws SQLException {
		return this.loginTimeout;
	}

	@Override
	public void setLoginTimeout(int seconds) throws SQLException {
		this.loginTimeout = seconds;
	}

	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		throw new UnsupportedOperationException("getParentLogger");
	}

	protected void init() {
	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		return this.getClass().isAssignableFrom(iface);
	}

	public void setConfigManagerType(String configManagerType) {
		if (StringUtils.isBlank(configManagerType)) {
			throw new ZebraException("configManagerType must not be blank");
		}

		this.configManagerType = configManagerType;
	}

	@Override
	@SuppressWarnings("unchecked")
	public <T> T unwrap(Class<T> iface) throws SQLException {
		try {
			return (T) this;
		} catch (Exception e) {
			throw new SQLException(e);
		}
	}
}

@@ -2442,30 +2442,64 @@
 %7B%0A%09%09
-return this.getClass()
+if(iface == null) %7B%0A%09%09%09return false;%0A%09%09%7D%0A%0A%09%09return iface
 .isA
@@ -2512,21 +2512,31 @@
 bleFrom(
-iface
+this.getClass()
 );%0A%09%7D%0A%0A%09
@@ -2875,33 +2875,234 @@
 %7B%0A%09%09
-try %7B%0A%09%09%09return (T) this;
+if(iface == null) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%0A%09%09try %7B%0A%09%09%09if(iface.isAssignableFrom(this.getClass())) %7B%0A%09%09%09%09return (T) this;%0A%09%09%09%7D else %7B%0A%09%09%09%09throw new SQLException(getClass().getName() + %22 Can not unwrap to%22 + iface.getName());%0A%09%09%09%7D
 %0A%09%09%7D

support unwrap