/*
 *      Copyright (C) 2015 Noorq, Inc.
 *
 *   Licensed 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 casser.core;

import java.util.Map;

import casser.config.CasserSettings;
import casser.config.DefaultCasserSettings;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;



public final class Casser {
	
	private static volatile CasserSettings settings = new DefaultCasserSettings();
	
	private Casser() {
	}
	
	public static CasserSettings settings() {
		return settings;
	}

	public static CasserSettings configure(CasserSettings overrideSettings) {
		CasserSettings old = settings;
		settings = overrideSettings;
		return old;
	}

	public static SessionInitializer connect(Cluster cluster) {
		Session session = cluster.connect();
		return new SessionInitializer(session);
	}

	public static SessionInitializer connect(Cluster cluster, String keyspace) {
		Session session = cluster.connect(keyspace);
		return new SessionInitializer(session);
	}
	
	public static SessionInitializer init(Session session) {
		
		if (session == null) {
			throw new IllegalArgumentException("empty session");
		}
		
		return new SessionInitializer(session);
	}
	
	public static <E> E dsl(Class<E> iface) {
		return dsl(iface, iface.getClassLoader());
	}

	public static <E> E dsl(Class<E> iface, ClassLoader classLoader) {
		return settings.getDslInstantiator().instantiate(iface, classLoader);
	}
	
	public static <E> E wrap(Map<String, Object> map, Class<E> iface) {
		return wrap(map, iface, iface.getClassLoader());
	}

	public static <E> E wrap(Map<String, Object> map, Class<E> iface, ClassLoader classLoader) {
		return settings.getWrapperInstantiator().instantiate(map, iface, classLoader);
	}

}

@@ -657,16 +657,106 @@
 til.Map;
+%0Aimport java.util.concurrent.ConcurrentHashMap;%0Aimport java.util.concurrent.ConcurrentMap;
 %0A%0Aimport
@@ -1028,16 +1028,127 @@
 gs();%0A%09%0A
+%09private static final ConcurrentMap%3CClass%3C?%3E, Object%3E dslCache = new  ConcurrentHashMap%3CClass%3C?%3E, Object%3E();%0A%09%0A
 %09private
@@ -1893,32 +1893,95 @@
 (session);%0A%09%7D%0A%09%0A
+%09public static void clearDslCache() %7B%0A%09%09dslCache.clear();%0A%09%7D%0A%09%0A
 %09public static %3C
@@ -2130,76 +2130,287 @@
 %7B%0A%09%09
-return settings.getDslInstantiator().instantiate(iface, classLoader)
+%0A%09%09Object instance = dslCache.get(iface);%0A%09%09%0A%09%09if (instance == null) %7B%0A%09%09%0A%09%09%09instance = settings.getDslInstantiator().instantiate(iface, classLoader);%0A%09%09%09%0A%09%09%09Object c = dslCache.putIfAbsent(iface, instance);%0A%09%09%09if (c != null) %7B%0A%09%09%09%09instance = c;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A%09%09return (E) instance
 ;%0A%09%7D

revert back dsl cache