001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package mx4j.server;
010:
011: import java.util.ArrayList;
012: import javax.management.loading.MLet;
013:
014: /**
015: * Default implementation of a ClassLoaderRepository
016: *
017: * @version $Revision: 1.8 $
018: */
019: public class DefaultClassLoaderRepository extends
020: ModifiableClassLoaderRepository {
021: private static final int WITHOUT = 1;
022: private static final int BEFORE = 2;
023:
024: private ArrayList classLoaders = new ArrayList();
025:
026: public Class loadClass(String className)
027: throws ClassNotFoundException {
028: return loadClassWithout(null, className);
029: }
030:
031: public Class loadClassWithout(ClassLoader loader, String className)
032: throws ClassNotFoundException {
033: return loadClassFromRepository(loader, className, WITHOUT);
034: }
035:
036: public Class loadClassBefore(ClassLoader loader, String className)
037: throws ClassNotFoundException {
038: return loadClassFromRepository(loader, className, BEFORE);
039: }
040:
041: protected void addClassLoader(ClassLoader cl) {
042: if (cl == null)
043: return;
044:
045: ArrayList loaders = getClassLoaders();
046: synchronized (loaders) {
047: if (!loaders.contains(cl))
048: loaders.add(cl);
049: }
050: }
051:
052: protected void removeClassLoader(ClassLoader cl) {
053: if (cl == null)
054: return;
055:
056: ArrayList loaders = getClassLoaders();
057: synchronized (loaders) {
058: loaders.remove(cl);
059: }
060: }
061:
062: protected ArrayList cloneClassLoaders() {
063: ArrayList loaders = getClassLoaders();
064: synchronized (loaders) {
065: return (ArrayList) loaders.clone();
066: }
067: }
068:
069: protected ArrayList getClassLoaders() {
070: return classLoaders;
071: }
072:
073: private Class loadClassFromRepository(ClassLoader loader,
074: String className, int algorithm)
075: throws ClassNotFoundException {
076: ArrayList copy = cloneClassLoaders();
077: for (int i = 0; i < copy.size(); ++i) {
078: try {
079: ClassLoader cl = (ClassLoader) copy.get(i);
080: if (cl.equals(loader)) {
081: if (algorithm == BEFORE)
082: break;
083: else
084: continue;
085: }
086:
087: return loadClass(cl, className);
088: } catch (ClassNotFoundException ignored) {
089: }
090: }
091: throw new ClassNotFoundException(className);
092: }
093:
094: private Class loadClass(ClassLoader loader, String className)
095: throws ClassNotFoundException {
096: // This is an optimization: if the classloader is an MLet (and not a subclass)
097: // then the method MLet.loadClass(String, ClassLoaderRepository) is used.
098: if (loader.getClass() == MLet.class)
099: return ((MLet) loader).loadClass(className, null);
100: return loader.loadClass(className);
101: }
102:
103: private int getSize() {
104: ArrayList loaders = getClassLoaders();
105: synchronized (loaders) {
106: return loaders.size();
107: }
108: }
109: }
|