// Path: src/main/java/net/datenwerke/sandbox/SandboxedCallResult.java
// public interface SandboxedCallResult<V> extends Serializable {
// 	
// 	/**
// 	 * Returns the raw object 
// 	 * 
// 	 * @return
// 	 */
// 	Object getRaw();
// 	
// 	/**
// 	 * Returns the object in the scope of the classloader that has loaded this instance
// 	 * of {@link SandboxedCallResult}
// 	 * 
// 	 * @return
// 	 */
// 	V get();
// 	
// 	/**
// 	 * Returns the object in the scope of the given classloader.
// 	 * 
// 	 * @param  loader
// 	 * @return
// 	 */
// 	Object get(ClassLoader loader);
// 	
// 	/**
// 	 * Returns the wrapped object in the scope of the classloader that has loaded object obj.
// 	 * 
// 	 * @param obj
// 	 * @return
// 	 */
// 	Object get(Object obj);
// 
// 	
// }
// 
// Path: src/main/java/net/datenwerke/sandbox/SandboxedCallResultImpl.java
// public class SandboxedCallResultImpl<V> implements SandboxedCallResult<V> {
// 	
// 	/**
// 	 * 
// 	 */
// 	private static final long serialVersionUID = 2221868658839523140L;
// 	
// 	/**
// 	 * 
// 	 */
// 	public final Object raw;
// 	
// 	public SandboxedCallResultImpl(Object raw) {
// 		this.raw = raw;
// 	}
// 	
// 	@Override
// 	public Object getRaw() {
// 		return raw;
// 	}
// 	
// 	@Override
// 	public V get(){
// 		return (V) get(getClass().getClassLoader());
// 	}
// 	
// 	@Override
// 	public Object get(Object obj){
// 		return get(obj.getClass().getClassLoader());
// 	}
// 	
// 	@Override
// 	public Object get(ClassLoader loader){
// 		return SandboxServiceImpl.getInstance().bridge(raw, null != loader ? loader : ClassLoader.getSystemClassLoader());
// 	}
// 
// 	
// }
// 
// Path: src/main/java/net/datenwerke/sandbox/jvm/exceptions/JvmKilledUnsafeThreadException.java
// public class JvmKilledUnsafeThreadException extends RemoteException {
// 
// 	/**
// 	 * 
// 	 */
// 	private static final long serialVersionUID = -5420515201162587892L;
// 
// 
// 
// }
// 
// Path: src/main/java/net/datenwerke/sandbox/jvm/exceptions/JvmPoolInstantiationException.java
// public class JvmPoolInstantiationException extends RuntimeException {
// 
// 	/**
// 	 * 
// 	 */
// 	private static final long serialVersionUID = 1764438816801841440L;
// 
// 	public JvmPoolInstantiationException(Throwable e) {
// 		super(e);
// 	}
// }
// 
// Path: src/main/java/net/datenwerke/sandbox/jvm/exceptions/JvmServerDeadException.java
// public class JvmServerDeadException extends Exception {
// 
// 	/**
// 	 * 
// 	 */
// 	private static final long serialVersionUID = -8222817806431025268L;
// 
// 	
// 	public JvmServerDeadException(Throwable e){
// 		super(e);
// 	}
// }


import java.util.logging.Logger;
import net.datenwerke.sandbox.SandboxedCallResult;
import net.datenwerke.sandbox.SandboxedCallResultImpl;
import net.datenwerke.sandbox.jvm.exceptions.JvmKilledUnsafeThreadException;
import net.datenwerke.sandbox.jvm.exceptions.JvmPoolInstantiationException;
import net.datenwerke.sandbox.jvm.exceptions.JvmServerDeadException;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
/*
*  java-sandbox
*  Copyright (c) 2012 datenwerke Jan Albrecht
*  http://www.datenwerke.net
*  
*  This file is part of the java-sandbox: https://sourceforge.net/p/dw-sandbox/
*
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.

*  This program 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 General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*/

package net.datenwerke.sandbox.jvm;



/**
 * 
 * @author Arno Mittelbach
 *
 */
public class JvmPoolImpl implements JvmPool{

	private final Logger logger = Logger.getLogger(getClass().getName());
	
	private final int poolsize;
	private final int freelancerSize;
	private final BlockingDeque<JvmFuture> workQueue;
	private final JvmWorker[] jvms;
	
	private final JvmFreelancer[] freelancers;
	private final BlockingQueue<JvmFreelancer> freelancerQueue;
	
	private final JvmPoolConfig jvmConfig;
	private final String name;
	
	private boolean shutdown = false;

	private static int poolNumber = 1;
	
	public JvmPoolImpl(JvmPoolConfig jvmConfig){
		this.poolsize = jvmConfig.getPoolSize();
// Path: src/main/java/net/datenwerke/sandbox/SandboxedCallResult.java
// public interface SandboxedCallResult<V> extends Serializable {
// 	
// 	/**
// 	 * Returns the raw object 
// 	 * 
// 	 * @return
// 	 */
// 	Object getRaw();
// 	
// 	/**
// 	 * Returns the object in the scope of the classloader that has loaded this instance
// 	 * of {@link SandboxedCallResult}
// 	 * 
// 	 * @return
// 	 */
// 	V get();
// 	
// 	/**
// 	 * Returns the object in the scope of the given classloader.
// 	 * 
// 	 * @param  loader
// 	 * @return
// 	 */
// 	Object get(ClassLoader loader);
// 	
// 	/**
// 	 * Returns the wrapped object in the scope of the classloader that has loaded object obj.
// 	 * 
// 	 * @param obj
// 	 * @return
// 	 */
// 	Object get(Object obj);
// 
// 	
// }
// 
// Path: src/main/java/net/datenwerke/sandbox/SandboxedCallResultImpl.java
// public class SandboxedCallResultImpl<V> implements SandboxedCallResult<V> {
// 	
// 	/**
// 	 * 
// 	 */
// 	private static final long serialVersionUID = 2221868658839523140L;
// 	
// 	/**
// 	 * 
// 	 */
// 	public final Object raw;
// 	
// 	public SandboxedCallResultImpl(Object raw) {
// 		this.raw = raw;
// 	}
// 	
// 	@Override
// 	public Object getRaw() {
// 		return raw;
// 	}
// 	
// 	@Override
// 	public V get(){
// 		return (V) get(getClass().getClassLoader());
// 	}
// 	
// 	@Override
// 	public Object get(Object obj){
// 		return get(obj.getClass().getClassLoader());
// 	}
// 	
// 	@Override
// 	public Object get(ClassLoader loader){
// 		return SandboxServiceImpl.getInstance().bridge(raw, null != loader ? loader : ClassLoader.getSystemClassLoader());
// 	}
// 
// 	
// }
// 
// Path: src/main/java/net/datenwerke/sandbox/jvm/exceptions/JvmKilledUnsafeThreadException.java
// public class JvmKilledUnsafeThreadException extends RemoteException {
// 
// 	/**
// 	 * 
// 	 */
// 	private static final long serialVersionUID = -5420515201162587892L;
// 
// 
// 
// }
// 
// Path: src/main/java/net/datenwerke/sandbox/jvm/exceptions/JvmPoolInstantiationException.java
// public class JvmPoolInstantiationException extends RuntimeException {
// 
// 	/**
// 	 * 
// 	 */
// 	private static final long serialVersionUID = 1764438816801841440L;
// 
// 	public JvmPoolInstantiationException(Throwable e) {
// 		super(e);
// 	}
// }
// 
// Path: src/main/java/net/datenwerke/sandbox/jvm/exceptions/JvmServerDeadException.java
// public class JvmServerDeadException extends Exception {
// 
// 	/**
// 	 * 
// 	 */
// 	private static final long serialVersionUID = -8222817806431025268L;
// 
// 	
// 	public JvmServerDeadException(Throwable e){
// 		super(e);
// 	}
// }


// Path: src/main/java/net/datenwerke/sandbox/jvm/JvmPoolImpl.java
import java.util.logging.Logger;
import net.datenwerke.sandbox.SandboxedCallResult;
import net.datenwerke.sandbox.SandboxedCallResultImpl;
import net.datenwerke.sandbox.jvm.exceptions.JvmKilledUnsafeThreadException;
import net.datenwerke.sandbox.jvm.exceptions.JvmPoolInstantiationException;
import net.datenwerke.sandbox.jvm.exceptions.JvmServerDeadException;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

/*
*  java-sandbox
*  Copyright (c) 2012 datenwerke Jan Albrecht
*  http://www.datenwerke.net
*  
*  This file is part of the java-sandbox: https://sourceforge.net/p/dw-sandbox/
*
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.

*  This program 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 General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*/

package net.datenwerke.sandbox.jvm;



/**
 * 
 * @author Arno Mittelbach
 *
 */
public class JvmPoolImpl implements JvmPool{

	private final Logger logger = Logger.getLogger(getClass().getName());
	
	private final int poolsize;
	private final int freelancerSize;
	private final BlockingDeque<JvmFuture> workQueue;
	private final JvmWorker[] jvms;
	
	private final JvmFreelancer[] freelancers;
	private final BlockingQueue<JvmFreelancer> freelancerQueue;
	
	private final JvmPoolConfig jvmConfig;
	private final String name;
	
	private boolean shutdown = false;

	private static int poolNumber = 1;
	
	public JvmPoolImpl(JvmPoolConfig jvmConfig){
		this.poolsize = jvmConfig.getPoolSize();
		this.freelancerSize = jvmConfig.getFreelancerSize();