KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > FileClassLoadingService


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.modeler;
57
58 import java.io.File JavaDoc;
59 import java.net.MalformedURLException JavaDoc;
60 import java.net.URL JavaDoc;
61 import java.net.URLClassLoader JavaDoc;
62 import java.util.ArrayList JavaDoc;
63 import java.util.Collection JavaDoc;
64 import java.util.Collections JavaDoc;
65 import java.util.Iterator JavaDoc;
66 import java.util.List JavaDoc;
67
68 import org.apache.log4j.Logger;
69
70 /**
71  * A default implementation of ClassLoadingService used in CayenneModeler.
72  *
73  * @since 1.1
74  * @author Andrei Adamchik
75  */

76 public class FileClassLoadingService implements ClassLoadingService {
77
78     private static Logger logObj = Logger.getLogger(FileClassLoadingService.class);
79
80     private FileClassLoader classLoader;
81     protected List JavaDoc pathFiles;
82
83     public FileClassLoadingService() {
84         this.pathFiles = new ArrayList JavaDoc(15);
85     }
86
87     /**
88      * Returns class for a given name, loading it if needed from configured locations.
89      */

90     public synchronized Class JavaDoc loadClass(String JavaDoc className) throws ClassNotFoundException JavaDoc {
91         return nonNullClassLoader().loadClass(className);
92     }
93
94     /**
95      * Returns a ClassLoader based on the current configured CLASSPATH settings.
96      */

97     public ClassLoader JavaDoc getClassLoader() {
98         return nonNullClassLoader();
99     }
100
101     /**
102      * Returns an unmodifiable list of configured CLASSPATH locations.
103      */

104     public synchronized List JavaDoc getPathFiles() {
105         return Collections.unmodifiableList(pathFiles);
106     }
107
108     public synchronized void setPathFiles(Collection JavaDoc files) {
109
110         pathFiles.clear();
111         classLoader = null;
112
113         Iterator JavaDoc it = files.iterator();
114         while (it.hasNext()) {
115             addFile((File JavaDoc) it.next());
116         }
117     }
118
119     /**
120      * Adds a new location to the list of configured locations.
121      */

122     private void addFile(File JavaDoc file) {
123         file = file.getAbsoluteFile();
124
125         if (pathFiles.contains(file)) {
126             return;
127         }
128
129         if (classLoader != null) {
130             try {
131                 classLoader.addURL(file.toURL());
132             }
133             catch (MalformedURLException JavaDoc ex) {
134                 logObj.warn("Invalid classpath entry, ignoring: " + file);
135                 return;
136             }
137         }
138
139         pathFiles.add(file);
140         logObj.debug("Added CLASSPATH entry...: " + file.getAbsolutePath());
141     }
142
143     private synchronized FileClassLoader nonNullClassLoader() {
144         // init class loader on demand
145
if (classLoader == null) {
146             classLoader = new FileClassLoader(getClass().getClassLoader(), pathFiles);
147         }
148
149         return classLoader;
150     }
151
152     // URLClassLoader with addURL method exposed.
153
static class FileClassLoader extends URLClassLoader JavaDoc {
154
155         FileClassLoader(ClassLoader JavaDoc parent) {
156             super(new URL JavaDoc[0], parent);
157         }
158
159         FileClassLoader(ClassLoader JavaDoc parent, List JavaDoc files) {
160             this(parent);
161
162             Iterator JavaDoc it = files.iterator();
163             while (it.hasNext()) {
164                 File JavaDoc file = (File JavaDoc) it.next();
165
166                 // I guess here we have to quetly ignore invalid URLs...
167
try {
168                     addURL(file.toURL());
169                 }
170                 catch (MalformedURLException JavaDoc ex) {
171                 }
172             }
173         }
174
175         public void addURL(URL JavaDoc url) {
176             super.addURL(url);
177         }
178     }
179 }
Popular Tags