<commit_before>/*******************************************************************************
* Copyright (C) 2016 <NAME> <<EMAIL>>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     <NAME> <<EMAIL>> - initial API and implementation
 ******************************************************************************/
package com.quakearts.tools.classloaders;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.concurrent.ConcurrentHashMap;
import java.util.zip.ZipInputStream;
import com.quakearts.tools.classloaders.hibernate.JarFileEntry;
import com.quakearts.tools.classloaders.utils.UtilityMethods;
import com.quakearts.webapp.orm.DataStore;
import com.quakearts.webapp.orm.DataStoreFactory;
import com.quakearts.webapp.orm.exception.DataStoreException;

public class DBJarClassLoader extends ClassLoader {//TODO Implement with URL class loader

	private static final ConcurrentHashMap<Long, byte[]> CACHED_JARS = new ConcurrentHashMap<Long, byte[]>();
	private String domain;
	
	public DBJarClassLoader() {
		super(Thread.currentThread().getContextClassLoader());
	}
	
	public DBJarClassLoader(String domain) {
		super(Thread.currentThread().getContextClassLoader());
		this.domain = domain;
	}
	
	public Class<?> getDBJarClass(String name) throws ClassNotFoundException{
		return loadClass(name, true);
	}
	
	@Override
	protected Class<?> findClass(String className) throws ClassNotFoundException {
		try{
			JarFileEntry classObject = loadEntry(className.replace(".", "/") +".class");
			if(classObject==null)
				throw new ClassNotFoundException("Class "+className+" does not exist in database");
			else
				return defineClass(classObject,className);
		}catch (DataStoreException e) {
			throw new ClassNotFoundException("Exception of type " + e.getClass().getName()
					+ " was thrown. Message is " + e.getMessage()
					+ ". Exception occured whiles finding class in database.",e);
		} catch (IOException e) {
			throw new ClassNotFoundException("Exception of type " + e.getClass().getName()
					+ " was thrown. Message is " + e.getMessage()
					+ ". Exception occured whiles loading class from jar stream.",e);
		}
	}

	private JarFileEntry loadEntry(String entryName) throws DataStoreException, IOException {
		DataStore store;
		if(domain==null)
			store = DataStoreFactory.getInstance().getDataStore();
		else
			store = DataStoreFactory.getInstance().getDataStore(domain);
		return (JarFileEntry)store.get(JarFileEntry.class,entryName);
	}

	private byte[] getBytes(JarFileEntry jarFileEntry) throws IOException{
		ByteArrayInputStream in;
		byte[] cachedJar;
		if((cachedJar = CACHED_JARS.get(jarFileEntry.getJarId()))!=null)
			in = new ByteArrayInputStream(cachedJar);
		else
			in = new ByteArrayInputStream(jarFileEntry.getJarFile().getJarData());

		ZipInputStream jarStream = new ZipInputStream(in);
		String entryFileName = jarFileEntry.getId();
		if(UtilityMethods.findZipEntry(entryFileName, jarStream)==null)
			throw new IOException("No such jar entry in jarfile: "+entryFileName);
		else{
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			for(int c=jarStream.read();c!=-1;c=jarStream.read())
				bos.write(c);
			
			return bos.toByteArray();
		}
	}

	private Class<?> defineClass(JarFileEntry classObject, String className) throws IOException {
		byte[] classBytes = getBytes(classObject);
		return defineClass(className, classBytes, 0, classBytes.length);
	}

	@Override
	protected URL findResource(String name) {<commit_msg>Remove unnecessary cast<commit_after>/*******************************************************************************
* Copyright (C) 2016 <NAME> <<EMAIL>>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     <NAME> <<EMAIL>> - initial API and implementation
 ******************************************************************************/
package com.quakearts.tools.classloaders;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.concurrent.ConcurrentHashMap;
import java.util.zip.ZipInputStream;
import com.quakearts.tools.classloaders.hibernate.JarFileEntry;
import com.quakearts.tools.classloaders.utils.UtilityMethods;
import com.quakearts.webapp.orm.DataStore;
import com.quakearts.webapp.orm.DataStoreFactory;
import com.quakearts.webapp.orm.exception.DataStoreException;

public class DBJarClassLoader extends ClassLoader {

	private static final ConcurrentHashMap<Long, byte[]> CACHED_JARS = new ConcurrentHashMap<Long, byte[]>();
	private String domain;
	
	public DBJarClassLoader() {
		super(Thread.currentThread().getContextClassLoader());
	}
	
	public DBJarClassLoader(String domain) {
		super(Thread.currentThread().getContextClassLoader());
		this.domain = domain;
	}
	
	public Class<?> getDBJarClass(String name) throws ClassNotFoundException{
		return loadClass(name, true);
	}
	
	@Override
	protected Class<?> findClass(String className) throws ClassNotFoundException {
		try{
			JarFileEntry classObject = loadEntry(className.replace(".", "/") +".class");
			if(classObject==null)
				throw new ClassNotFoundException("Class "+className+" does not exist in database");
			else
				return defineClass(classObject,className);
		}catch (DataStoreException e) {
			throw new ClassNotFoundException("Exception of type " + e.getClass().getName()
					+ " was thrown. Message is " + e.getMessage()
					+ ". Exception occured whiles finding class in database.",e);
		} catch (IOException e) {
			throw new ClassNotFoundException("Exception of type " + e.getClass().getName()
					+ " was thrown. Message is " + e.getMessage()
					+ ". Exception occured whiles loading class from jar stream.",e);
		}
	}

	private JarFileEntry loadEntry(String entryName) throws DataStoreException, IOException {
		DataStore store;
		if(domain==null)
			store = DataStoreFactory.getInstance().getDataStore();
		else
			store = DataStoreFactory.getInstance().getDataStore(domain);
		return store.get(JarFileEntry.class, entryName);
	}

	private byte[] getBytes(JarFileEntry jarFileEntry) throws IOException{
		ByteArrayInputStream in;
		byte[] cachedJar;
		if((cachedJar = CACHED_JARS.get(jarFileEntry.getJarId()))!=null)
			in = new ByteArrayInputStream(cachedJar);
		else
			in = new ByteArrayInputStream(jarFileEntry.getJarFile().getJarData());

		ZipInputStream jarStream = new ZipInputStream(in);
		String entryFileName = jarFileEntry.getId();
		if(UtilityMethods.findZipEntry(entryFileName, jarStream)==null)
			throw new IOException("No such jar entry in jarfile: "+entryFileName);
		else{
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			for(int c=jarStream.read();c!=-1;c=jarStream.read())
				bos.write(c);
			
			return bos.toByteArray();
		}
	}

	private Class<?> defineClass(JarFileEntry classObject, String className) throws IOException {
		byte[] classBytes = getBytes(classObject);
		return defineClass(className, classBytes, 0, classBytes.length);
	}

	@Override
	protected URL findResource(String name) {
{'fraction_non_alphanumeric': 0.14772580018141765, 'fraction_numerical': 0.002591680704937152, 'mean_word_length': 5.524091293322063, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1922073', 'n_tokens_mistral': 2345, 'n_tokens_neox': 2147, 'n_words': 531}