refactored Authorization resolver to use the AbstractResolver superclass

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@769475 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andrew Zeneski
2009-04-28 17:32:16 +00:00
parent d37f55dba6
commit f7e613752e
@@ -19,20 +19,10 @@
package org.ofbiz.common.authentication;
import java.util.List;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.net.URL;
import java.net.URLDecoder;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.List;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.AbstractResolver;
import org.ofbiz.common.authentication.api.Authenticator;
/**
@@ -40,134 +30,23 @@ import org.ofbiz.common.authentication.api.Authenticator;
*
* Discovers implementations of Authenticator on the class path (implementations must be in org.ofbiz.* package)
*/
public class AuthInterfaceResolver {
public class AuthInterfaceResolver extends AbstractResolver {
private static final String module = AuthInterfaceResolver.class.getName();
@SuppressWarnings("unchecked")
protected List<Class> authenticators = new ArrayList<Class>();
protected ClassLoader loader;
public AuthInterfaceResolver() {
loader = getContextClassLoader();
super();
}
@SuppressWarnings("unchecked")
public List<Class> getImplementations() {
find("org.ofbiz");
return authenticators;
}
protected void find(String packageName) {
packageName = packageName.replace('.', '/');
Enumeration<URL> urls;
try {
urls = loader.getResources(packageName);
}
catch (IOException io) {
Debug.logWarning(io, "Could not read package: " + packageName, module);
return;
}
while (urls.hasMoreElements()) {
try {
String urlPath = urls.nextElement().getFile();
urlPath = URLDecoder.decode(urlPath, "UTF-8");
if (Debug.verboseOn())
Debug.logVerbose("Found library file [" + urlPath + "]", module);
if (urlPath.startsWith("file:")) {
urlPath = urlPath.substring(5);
}
if (urlPath.indexOf('!') > 0) {
urlPath = urlPath.substring(0, urlPath.indexOf('!'));
}
if (Debug.verboseOn())
Debug.logVerbose("Scanning for classes in [" + urlPath + "]", module);
File file = new File(urlPath);
if (file.isDirectory()) {
readDirectory(packageName, file);
} else {
readJar(packageName, file);
}
}
catch (IOException io) {
Debug.logError(io, "Could not read resource entries", module);
}
}
}
protected void readDirectory(String parent, File location) {
File[] files = location.listFiles();
StringBuffer buf;
if (files == null) {
Debug.logWarning("Could not list directory " + location.getAbsolutePath() + " when looking for component classes", module);
return;
}
for (File file : files) {
buf = new StringBuffer();
buf.append(parent);
if (buf.length() > 0)
buf.append("/");
buf.append(file.getName());
String packageOrClass = (parent == null ? file.getName() : buf.toString());
if (file.isDirectory()) {
readDirectory(packageOrClass, file);
} else if (file.getName().endsWith(".class")) {
checkFile(packageOrClass);
}
}
}
protected void readJar(String parent, File jarfile) {
try {
JarEntry entry;
JarInputStream jarStream = new JarInputStream(new FileInputStream(jarfile));
while ((entry = jarStream.getNextJarEntry()) != null) {
String name = entry.getName();
if (!entry.isDirectory() && name.startsWith(parent) && name.endsWith(".class")) {
checkFile(name);
}
}
}
catch (IOException io) {
Debug.logError(io, "Could not search jar file [" + jarfile + "]", module);
}
}
protected void checkFile(String name) {
String externalName = name.substring(0, name.indexOf('.')).replace('/', '.');
if (Debug.verboseOn())
Debug.logVerbose("Converted file value [" + name + "] to class [" + externalName + "]", module);
try {
resolveClass(loader.loadClass(externalName));
} catch (ClassNotFoundException e) {
Debug.logWarning("No class found - " + externalName, module);
}
}
protected ClassLoader getContextClassLoader() {
return AccessController.doPrivileged(
new PrivilegedAction<ClassLoader>() {
public ClassLoader run() {
ClassLoader cl = null;
try {
cl = Thread.currentThread().getContextClassLoader();
} catch (SecurityException e) {
Debug.logError(e, e.getMessage(), module);
}
return cl;
}
});
}
@SuppressWarnings("unchecked")
public void resolveClass(Class clazz) {
Class[] ifaces = clazz.getInterfaces();
for (Class iface : ifaces) {