/*
 * Copyright (c) 2016 MCPhoton <http://mcphoton.org> and contributors.
 *
 * This file is part of the Photon Server Implementation <https://github.com/mcphoton/Photon-Server>.
 *
 * The Photon Server Implementation is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * The Photon Server Implementation is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.mcphoton.impl.plugin;

import java.net.URL;
import java.net.URLClassLoader;
import org.mcphoton.plugin.ClassSharer;
import org.mcphoton.plugin.SharedClassLoader;

public final class PluginClassLoader extends URLClassLoader implements SharedClassLoader {

	private final ClassSharer sharer;

	public PluginClassLoader(URL[] urls, ClassSharer sharer) {
		super(urls);
		this.sharer = sharer;
	}

	public PluginClassLoader(URL url, ClassSharer sharer) {
		super(new URL[] {url});
		this.sharer = sharer;
	}

	@Override
	protected Class<?> findClass(String name) throws ClassNotFoundException {
		return findClass(name, true);
	}

	@Override
	public Class<?> findClass(String name, boolean checkShared) throws ClassNotFoundException {
		if (checkShared) {
			Class<?> c = sharer.getClass(name);
			if (c != null) {
				return c;
			}
		}
		return super.findClass(name);

	}

	@Override
	public ClassSharer getSharer() {
		return sharer;
	}

}

@@ -968,24 +968,74 @@
 lassLoader;%0A
+import java.util.concurrent.atomic.AtomicInteger;%0A
 import org.m
@@ -1058,24 +1058,24 @@
 lassSharer;%0A
-
 import org.m
@@ -1235,16 +1235,77 @@
  sharer;
+%0A%09private final AtomicInteger useCount = new AtomicInteger();
 %0A%0A%09publi
@@ -1508,24 +1508,109 @@
 sharer;%0A%09%7D%0A%0A
+%09@Override%0A%09public int decreaseUseCount() %7B%0A%09%09return useCount.getAndDecrement();%0A%09%7D%0A%0A
 %09@Override%0A%09
@@ -2004,24 +2004,24 @@
 tSharer() %7B%0A
-
 %09%09return sha
@@ -2029,10 +2029,163 @@
 er;%0A%09%7D%0A%0A
+%09@Override%0A%09public int getUseCount() %7B%0A%09%09return useCount.get();%0A%09%7D%0A%0A%09@Override%0A%09public int increaseUseCount() %7B%0A%09%09return useCount.getAndIncrement();%0A%09%7D%0A%0A
 %7D%0A

Implement use count