View Javadoc

1   /*
2    * $Id: VocabularyClassLoader.java,v 1.6 2004/09/20 03:51:50 jlerner Exp $
3    *
4    * Copyright (c) 1999-2004, BBN Technologies, LLC.
5    * All rights reserved.
6    * http://www.daml.org/legal/opensource/bbn_license.html
7    */
8   package com.bbn.swede.library.kazuki;
9   
10  import java.io.File;
11  import java.io.FileInputStream;
12  import java.io.FileNotFoundException;
13  import java.io.IOException;
14  import java.util.StringTokenizer;
15  
16  /***
17   * A custom class loader that looks for SchemaGen vocabularies in the 
18   * <code>vocab</code> package.  If a requested vocabulary class is not found 
19   * in that package, the standard class loading mechanism is invoked.
20   */
21  public class VocabularyClassLoader extends ClassLoader
22  {
23     private String _sRoot;
24     /***
25      * Creates a new vocabulary class loader that looks for vocabularies under
26      * a specified package root.  The current thread's context class loader is
27      * used as the parent.
28      * @param sPackageRoot The filesystem path of the folder containing the
29      *                     package structure to search for vocabulary classes
30      */
31     public VocabularyClassLoader(String sPackageRoot)
32     {
33        super(Thread.currentThread().getContextClassLoader());
34        _sRoot = sPackageRoot;
35     }
36  
37     /***
38      * Creates a new vocabulary class loader that looks for vocabularies under
39      * a specified package root and defers to a specified parent class loader
40      * if it fails.
41      * @param sPackageRoot The filesystem path of the folder containing the
42      *                     package structure to search for vocabulary classes
43      * @param parent The classloader to defer to if a vocabulary class is not
44      *               found under <code>sPackageRoot</code>
45      */
46     public VocabularyClassLoader(ClassLoader parent, String sPackageRoot)
47     {
48        super(parent);
49        _sRoot = sPackageRoot;
50     }
51  
52     /*
53      *  (non-Javadoc)
54      * @see java.lang.ClassLoader#findClass(java.lang.String)
55      */
56     protected Class findClass(String sClass) throws ClassNotFoundException
57     {
58        StringTokenizer st = new StringTokenizer(sClass, ".");
59        StringBuffer sPath = new StringBuffer(_sRoot);
60        while (st.hasMoreTokens())
61        {
62           sPath.append("/" + st.nextToken());
63        }
64        sPath.append(".class");
65        File f = new File(sPath.toString());
66        if (!f.exists())
67        {
68           throw new ClassNotFoundException();
69        }
70        try
71        {
72           FileInputStream fis = new FileInputStream(f);
73           byte[] b = new byte[(int) f.length()];
74           fis.read(b);
75           return defineClass(sClass, b, 0, b.length);
76        }
77        catch (FileNotFoundException e)
78        {
79           throw new ClassNotFoundException();
80        }
81        catch (IOException e)
82        {
83           throw new ClassNotFoundException();
84        }
85     }
86  
87  }