01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.components.classloader;
18:
19: import org.apache.avalon.framework.activity.Disposable;
20: import org.apache.avalon.framework.thread.ThreadSafe;
21:
22: import java.io.File;
23: import java.io.IOException;
24: import java.util.Collections;
25: import java.util.HashSet;
26: import java.util.Set;
27:
28: /**
29: * A singleton-like implementation of <code>ClassLoaderManager</code>
30: *
31: * @author <a href="mailto:ricardo@apache.org">Ricardo Rocha</a>
32: * @version $Id: ClassLoaderManagerImpl.java 433543 2006-08-22 06:22:54Z crossley $
33: */
34: public class ClassLoaderManagerImpl implements ClassLoaderManager,
35: ThreadSafe, Disposable {
36:
37: /**
38: * Set of class directories
39: */
40: protected final Set fileSet = Collections
41: .synchronizedSet(new HashSet());
42:
43: /**
44: * The class loader instance
45: */
46: private RepositoryClassLoader instance;
47:
48: /**
49: * A constructor that ensures only a single class loader instance exists
50: */
51: public ClassLoaderManagerImpl() {
52: reinstantiate();
53: }
54:
55: public void dispose() {
56: this .fileSet.clear();
57: reinstantiate();
58: }
59:
60: /**
61: * Add a directory to the proxied class loader
62: *
63: * @param directoryName The repository name
64: * @throws IOException If the directory is invalid
65: */
66: public void addDirectory(File directoryName) throws IOException {
67: if (this .fileSet.add(directoryName)) {
68: this .instance.addDirectory(directoryName);
69: }
70: }
71:
72: /**
73: * Load a class through the proxied class loader
74: *
75: * @param className The name of the class to be loaded
76: * @return The loaded class
77: * @throws ClassNotFoundException If the class is not found
78: */
79: public Class loadClass(String className)
80: throws ClassNotFoundException {
81: return this .instance.loadClass(className);
82: }
83:
84: /**
85: * Reinstantiate the proxied class loader to allow for class reloading
86: */
87: public void reinstantiate() {
88: if (this .fileSet.isEmpty()) {
89: this .instance = new RepositoryClassLoader();
90: } else {
91: this .instance = new RepositoryClassLoader(this.fileSet);
92: }
93: }
94: }
|