KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > engine > ProjectorClassLoader


1 package org.apache.slide.projector.engine;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6
7 import org.apache.slide.projector.Context;
8 import org.apache.slide.projector.Projector;
9 import org.apache.slide.projector.SystemContext;
10 import org.apache.slide.projector.URI;
11 import org.apache.slide.projector.util.StreamHelper;
12 import org.apache.slide.projector.value.StreamableValue;
13 import org.apache.slide.projector.value.URIValue;
14
15 public class ProjectorClassLoader extends ClassLoader JavaDoc {
16     URI uri;
17
18     public ProjectorClassLoader(ClassLoader JavaDoc parent, URI uri) {
19         super(parent);
20         this.uri = uri;
21     }
22
23     public Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
24         Context context = new SystemContext();
25         try {
26             URI rendererUri = new URIValue(uri.toString() + name.replace('.', '/') + ".class");
27             StreamableValue resource = ((StreamableValue)Projector.getRepository().getResource(rendererUri, context.getCredentials()));
28             if ( resource == null ) {
29                 throw new ClassNotFoundException JavaDoc("Class " + name + " at URI '"+rendererUri+"' not found in collection '" + uri + "'!");
30             }
31             InputStream JavaDoc inputStream = resource.getInputStream();
32             ByteArrayOutputStream JavaDoc outputStream = new ByteArrayOutputStream JavaDoc(1024);
33             StreamHelper.copy(inputStream, outputStream);
34             byte[] b = outputStream.toByteArray();
35             return defineClass(name, b, 0, b.length);
36         } catch (IOException JavaDoc e) {
37             throw new ClassNotFoundException JavaDoc("Class " + name + " not found in collection " + uri + "!", e);
38         }
39     }
40 }
Popular Tags