From f7e613752e168ad2de04a13fdd1a2ca8ff3a8fe3 Mon Sep 17 00:00:00 2001 From: Andrew Zeneski Date: Tue, 28 Apr 2009 17:32:16 +0000 Subject: [PATCH] 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 --- .../authentication/AuthInterfaceResolver.java | 137 +----------------- 1 file changed, 8 insertions(+), 129 deletions(-) diff --git a/framework/common/src/org/ofbiz/common/authentication/AuthInterfaceResolver.java b/framework/common/src/org/ofbiz/common/authentication/AuthInterfaceResolver.java index 162b5e5c35..a15b64ec6c 100644 --- a/framework/common/src/org/ofbiz/common/authentication/AuthInterfaceResolver.java +++ b/framework/common/src/org/ofbiz/common/authentication/AuthInterfaceResolver.java @@ -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 authenticators = new ArrayList(); - protected ClassLoader loader; + public AuthInterfaceResolver() { - loader = getContextClassLoader(); + super(); } + @SuppressWarnings("unchecked") public List getImplementations() { find("org.ofbiz"); return authenticators; } - protected void find(String packageName) { - packageName = packageName.replace('.', '/'); - Enumeration 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() { - 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) {