All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wesleyhome.johksoftware.resource.JarResourceLoader Maven / Gradle / Ivy

/*
 * Copyright 2011 JOHK Software
 * 
 * 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 com.wesleyhome.johksoftware.resource;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import com.wesleyhome.johksoftware.resource.api.ClassFilter;
import com.wesleyhome.johksoftware.resource.api.ClassFilterAdapter;

/**
 * The JarResourceLoader class is a 
 * 
 * @author Justin Wesley
 * @since 1.0.0
 */
class JarResourceLoader extends FileResourceLoader {

	public static final String JAR_PROTOCOL = "jar";
	private String jarFilePath;

	JarResourceLoader(final URL jarURL) {
		super(jarURL);
		jarFilePath = jarURL.getFile();
		String regex = "file:";
		jarFilePath = jarFilePath.replaceFirst(regex, "");
		if (jarFilePath.endsWith("!")) {
			jarFilePath = jarFilePath.substring(0, jarFilePath.length() - 1);
		}
	}

	/* (non-Javadoc)
	 * @see com.johksoftware.utilities.reflection.FileResourceLoader#getClasses(java.lang.String)
	 */
	@Override
	public Set> getClasses(final String packageName) {
		ClassFilter filter = new ClassFilterAdapter() {

			/* (non-Javadoc)
			 * @see com.johksoftware.utilities.reflection.ClassFilterAdapter#evaluatePackage(java.lang.String)
			 */
			@Override
			public boolean evaluatePackage(final String pkgName) {
				return pkgName.startsWith(packageName);
			}
		};
		return super.getClasses(packageName, filter);
	}

	@Override
	protected  Set> getClasses(final String packageName, final Class collectionTypeClass,
		final ClassFilter classFilter, final boolean returnFirst) {
		JarFile jarFile;
		try {
			String truePath = URLDecoder.decode(jarFilePath, "UTF-8");
			jarFile = new JarFile(truePath);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		List list = Collections.list(jarFile.entries());
		Set> classes = new HashSet>();
		for (JarEntry jarEntry : list) {
			boolean isDirectory = jarEntry.isDirectory();
			if (!isDirectory) {
				String fileName = jarEntry.getName();
				boolean isClassFile = fileName.endsWith(".class");
				if (isClassFile) {
					int lastSlash = fileName.lastIndexOf("/");
					String newPackageName = fileName.substring(0, lastSlash).replace("/", ".");
					String className = fileName.substring(lastSlash + 1, fileName.length() - 6);
					String fullyQualifiedClassName = newPackageName + "." + className;
					if (classFilter.evaluatePackage(newPackageName)) {
						if (classFilter.evaluateClassName(fullyQualifiedClassName)) {
							Class cls = getImplementedClass(fullyQualifiedClassName, collectionTypeClass);
							if (cls != null && classFilter.evaluateClass(cls)) {
								classes.add(cls);
							}
							if (returnFirst) {
								return classes;
							}
						}
					}
				}
			}
		}
		return classes;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy