com.wesleyhome.johksoftware.resource.FileResourceLoader 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.File;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import com.wesleyhome.johksoftware.resource.api.BaseClassResourceLoader;
import com.wesleyhome.johksoftware.resource.api.ClassFilter;
/**
* @author Justin Wesley
* @since 1.0.0
*
*/
class FileResourceLoader extends BaseClassResourceLoader {
public static final String FILE_PROTOCOL = "file";
protected String baseClassPath;
/**
* @param baseClassPath
*/
FileResourceLoader(final URL baseClassPathURL) {
super();
try {
String file = baseClassPathURL.getFile();
String basePath1 = "";
baseClassPath = URLDecoder.decode(file, "UTF-8");
while (!baseClassPath.equals(basePath1)) {
basePath1 = baseClassPath;
baseClassPath = URLDecoder.decode(baseClassPath, "UTF-8");
}
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
@Override
protected Set> getClasses(final String packageName, final Class collectionTypeClass,
final ClassFilter classFilter, final boolean returnFirst) {
Set> classes = new HashSet>();
File directory = null;
String packageFileName = "";
try {
packageFileName = baseClassPath + '/' + packageName.replace('.', '/');
directory = new File(packageFileName);
} catch (NullPointerException x) {
throw new RuntimeException(packageFileName + " does not appear to be a valid package", x);
}
if (directory.exists()) {
// Get the list of the files contained in the package
File[] files = directory.listFiles();
for (File file : files) {
String fileName = file.getName();
if (file.isDirectory()) {
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 extends E> 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");
}
return classes;
}
/* (non-Javadoc)
* @see com.johksoftware.utilities.reflection.ClassResourceLoader#getClass(java.lang.String, java.lang.Class, com.johksoftware.utilities.reflection.ClassFilter)
*/
@Override
public Class extends E> getClass(final String packageName, final Class collectionTypeClass, final ClassFilter classFilter) {
Set> classes = getClasses(packageName, collectionTypeClass, classFilter, false);
return classes.isEmpty() ? null : classes.iterator().next();
}
}