KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > classloader > ClassLoaderManagerImpl


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.classloader;
17
18 import org.apache.avalon.framework.activity.Disposable;
19 import org.apache.avalon.framework.thread.ThreadSafe;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Set JavaDoc;
26
27 /**
28  * A singleton-like implementation of <code>ClassLoaderManager</code>
29  *
30  * @author <a HREF="mailto:ricardo@apache.org">Ricardo Rocha</a>
31  * @version $Id: ClassLoaderManagerImpl.java 161488 2005-04-15 15:54:05Z vgritsenko $
32  */

33 public class ClassLoaderManagerImpl implements ClassLoaderManager, ThreadSafe, Disposable {
34
35     /**
36      * Set of class directories
37      */

38     protected final Set JavaDoc fileSet = Collections.synchronizedSet(new HashSet JavaDoc());
39
40     /**
41      * The class loader instance
42      */

43     private RepositoryClassLoader instance;
44
45     /**
46      * A constructor that ensures only a single class loader instance exists
47      */

48     public ClassLoaderManagerImpl() {
49         reinstantiate();
50     }
51
52     public void dispose() {
53         this.fileSet.clear();
54         reinstantiate();
55     }
56
57     /**
58      * Add a directory to the proxied class loader
59      *
60      * @param directoryName The repository name
61      * @throws IOException If the directory is invalid
62      */

63     public void addDirectory(File JavaDoc directoryName) throws IOException JavaDoc {
64         if (this.fileSet.add(directoryName)) {
65             this.instance.addDirectory(directoryName);
66         }
67     }
68
69     /**
70      * Load a class through the proxied class loader
71      *
72      * @param className The name of the class to be loaded
73      * @return The loaded class
74      * @throws ClassNotFoundException If the class is not found
75      */

76     public Class JavaDoc loadClass(String JavaDoc className) throws ClassNotFoundException JavaDoc {
77         return this.instance.loadClass(className);
78     }
79
80     /**
81      * Reinstantiate the proxied class loader to allow for class reloading
82      */

83     public void reinstantiate() {
84         if (this.fileSet.isEmpty()) {
85             this.instance = new RepositoryClassLoader();
86         } else {
87             this.instance = new RepositoryClassLoader(this.fileSet);
88         }
89     }
90 }
91
Popular Tags