ProblemArticles
package org.jos;
import java.lang.reflect.*;
import java.util.*;
import org.jos.util.Version;
import org.jos.api.JosErrorTracker;
/** Registry keeps track of the JOS API implementation factories.
*
* @author StefanReich
* @version 31-Mar-98
*/
public class Registry {
private static Vector/*<APIImplementationFactory>*/ apiImplementationFactories
= new Vector();
public static interface APIImplementationFactory {
public API getImplementation(Class api, Version minVersion);
}
public static void registerAPIImplementationFactory(APIImplementationFactory factory) {
apiImplementationFactories.addElement(factory);
}
/** returns the implementation for a given API part. to be called by
* org.jos.api classes only.
*
* @param api the interface
* @param minVersion the (minimally) required version
* @exception IllegalArgumentException if <code>api</code> is not in
* org.jos.api or is not an interface
* @return an implementation of the interface <code>api</code> or null
* if no implementation is available
*/
public static API getAPIImplementation(Class api, Version minVersion) {
check(api);
for (int i = 0; i < apiImplementationFactories.size(); i++) {
APIImplementationFactory factory = (APIImplementationFactory)
apiImplementationFactories.elementAt(i);
API implementation = factory.getImplementation(api, minVersion);
if (implementation != null &&
implementation.getImplementedVersion()
.isNewerThanOrEqualTo(minVersion))
return implementation;
}
return null;
}
private static void check(Class api) throws IllegalArgumentException {
if (!api.isInterface())
throw new IllegalArgumentException(
api.getName()+" is a class, not an interface");
if (!api.getName().startsWith("org.jos.api."))
throw new IllegalArgumentException(
api.getName()+" is not in org.jos.api");
}
private static Version readVersion(Class api) throws IllegalArgumentException {
try {
Field versionField = api.getDeclaredField("API_VERSION");
if ((versionField.getModifiers() & Modifier.STATIC) == 0)
throw new IllegalArgumentException(
api.getName()+".API_VERSION is not static");
Object o = versionField.get(null);
if (!(o instanceof Version))
throw new IllegalArgumentException(
api.getName()+".API_VERSION should contain an instance of org.jos.util.Version");
return (Version) o;
} catch (NoSuchFieldException e) {
throw new IllegalArgumentException(
api.getName()+" doesn't have a field named API_VERSION");
} catch (SecurityException e) {
throw new IllegalArgumentException(
api.getName()+".API_VERSION can't be accessed");
} catch (IllegalAccessException e) {
throw new IllegalArgumentException(
api.getName()+".API_VERSION is not public");
}
}
public static class SimpleAPIImplementationFactory implements APIImplementationFactory {
private Vector/*<Entry>*/ entries = new Vector();
static class Entry {
String api, implementation;
}
public void addEntry(String api, String implementation) {
Entry entry = new Entry();
entry.api = api;
entry.implementation = implementation;
entries.addElement(entry);
}
public API getImplementation(Class api, Version minVersion) {
String name = api.getName();
for (int i = 0; i < entries.size(); i++) {
Entry entry = (Entry) entries.elementAt(i);
if (name.equals(entry.api)) {
try {
return (API) Class.forName(entry.api).newInstance();
} catch (Exception e) {
}
}
}
return null;
}
}
}
Content of these pages are owned and copyrighted by the poster.
|
Hosted by:
|
|