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

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

Go to download

This project will load resources from version 2 of the JBoss virtual file system. Version 2 is found in JBoss Community AS 6 and JBoss EAP 5.1.

There is a newer version: 2.0.0
Show newest version
/**
 * 
 */
package com.wesleyhome.johksoftware.resource;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.jboss.virtual.VirtualFile;
import com.wesleyhome.johksoftware.resource.api.BaseClassResourceLoader;
import com.wesleyhome.johksoftware.resource.api.ClassFilter;

/**
 * @author Justin Wesley
 * @since 1.0
 *
 */
class VFS2ResourceLoader extends BaseClassResourceLoader {

	public static final String VFS_PROTOCOL = "vfsfile";
	protected VirtualFile baseVF;

	/**
	 * @param url
	 */
	VFS2ResourceLoader(final VirtualFile child) {
		super();
		baseVF = child;
	}

	private VirtualFile getChild(final VirtualFile base, final String packageName) throws IOException {
		String[] split = StringUtils.split(packageName, ".", 2);
		if (split.length == 0) {
			return base;
		}
		VirtualFile file = base.getChild(split[0]);
		if (split.length > 1) {
			return getChild(file, split[1]);
		}
		return file;
	}

	@Override
	protected  Set> getClasses(final String packageName, final Class collectionTypeClass,
		final ClassFilter classFilter, final boolean returnFirst) {
		Set> classes = new HashSet>();
		VirtualFile directory = null;
		String packageFileName = "";
		try {
			directory = getChild(baseVF, packageName);
			if (directory.exists()) {
				//	 Get the list of the files contained in the package
				List children = directory.getChildren();
				for (VirtualFile virtualFile : children) {
					String fileName = virtualFile.getName();
					if (!virtualFile.isLeaf()) {
						if (!classFilter.evaluatePackage(packageName)) {
							continue;
						}
						if (!classFilter.evaluateDirectoryName(fileName)) {
							continue;
						}
						if (!StringUtils.isEmpty(packageName)) {
							fileName = "." + fileName;
						}
						String newPackageName = packageName + fileName;
						classes.addAll(getClasses(newPackageName, collectionTypeClass, classFilter, false));
						if (returnFirst) {
							return classes;
						}
					} else {
						if (!fileName.endsWith(".class")) {
							continue;
						}
						String className = fileName.substring(0, fileName.length() - 6);
						String fullyQualifiedClassName = packageName + "." + className;
						if (classFilter.evaluateClassName(fullyQualifiedClassName)) {
							Class cls = getImplementedClass(fullyQualifiedClassName, collectionTypeClass);
							if (cls != null && classFilter.evaluateClass(cls)) {
								classes.add(cls);
							}
							if (returnFirst) {
								return classes;
							}
						}
					}
				}
			} else {
				throw new RuntimeException(packageName + " does not appear to be a valid package");
			}
		} catch (NullPointerException x) {
			throw new RuntimeException(packageFileName + " does not appear to be a valid package", x);
		} catch (IOException e) {
			throw new RuntimeException(packageFileName + " does not appear to be a valid package", e);
		}
		return classes;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy