FileDocCategorySizeDatePackage
Loader.javaAPI DocphoneME MR2 API (J2ME)5322Wed May 02 18:00:40 BST 2007com.sun.satsa.jcrmic.classfile

Loader

public class Loader extends Object
This class is a class loader.

Fields Summary
private Vector
classPath
Class path.
private Hashtable
classes
Loaded classes.
private com.sun.satsa.jcrmic.utils.Notifier
notifier
Errors/warnings notifier.
Constructors Summary
public Loader(com.sun.satsa.jcrmic.utils.Notifier notifier)
Constructor.

param
notifier errors/warnings notifier


        classPath = new Vector();
        classes = new Hashtable();
        this.notifier = notifier;
    
Methods Summary
public JClassgetClass(java.lang.String className)
Returns class definition loaded earlier.

param
className the class name
return
class definition


        return (JClass) classes.get(className.replace('.", '/"));
    
public booleanloadClass(java.lang.String className)
Loads the class.

param
className the class name
return
true if successfull


        className = className.replace('.", '/");

        // check to avoid duplicate loading

        if (classes.containsKey(className))
            return true;

        InputStream is = null;

        for (int i = 0; i < classPath.size(); i++) {

            if (classPath.elementAt(i) instanceof String) {

                String path = (String) classPath.elementAt(i);

                path += File.separator +
                        className.replace('/", File.separatorChar) +
                        ".class";

                File f = new File(path);

                if (!f.exists())
                    continue;

                try {
                    is = new FileInputStream(f);
                } catch (FileNotFoundException e) {
                    continue;
                }

            } else {

                ZipFile z = (ZipFile) classPath.elementAt(i);
                ZipEntry ze = z.getEntry(className + ".class");

                if (ze == null)
                    continue;

                try {
                    is = z.getInputStream(ze);
                } catch (IOException e) {
                    notifier.error("rmic.ioerror", z.getName());
                    return false;
                }
            }

            if (is != null)
                break;
        }

        if (is == null) {
            notifier.error("rmic.class.not.found", className);
            return false;
        }

        // class file is found, load it

        DataInputStream dis = new DataInputStream(
                new BufferedInputStream(is));

        JClass cl = new JClass(this);

        try {
            cl.parse(dis);
        } catch (Exception e) {
            notifier.error("rmic.parsing.error", className);
            return false;
        }

        // class is loaded

        classes.put(className, cl);

        // update the class, load all related classes

        if (!cl.update())
            return false;

        return true;
    
public booleansetClassPath(java.lang.String path)
Set path for class loading.

param
path path for class loading
return
true if successfull


        classPath.clear();

        StringTokenizer parser = new StringTokenizer(path, File.pathSeparator);

        while (parser.hasMoreTokens()) {

            String s = parser.nextToken();

            File f = new File(s);

            try {
                if (f.isDirectory()) {
                    // directory - add path to the vector
                    classPath.addElement(f.getCanonicalPath());
                } else {
                    // file - check that it exists
                    if (!f.exists()) {
                        notifier.error("rmic.file.not.found", s);
                        return false;
                    }

                    ZipFile z = new ZipFile(f);
                    classPath.addElement(z);
                }
            } catch (IOException e) {
                notifier.error("rmic.incorrect.classpath", s);
                return false;
            }
        }

        if (classPath.size() == 0) {
            return false;
        }

        return true;