Main Page Cookbook/Overview ImageMeister the jcprops file Licensing Binary Installation & Configuration [ Win · Mac · Nix · OSX ] Changes Public API Source Code Main Page Java [ Common · Win · Mac · Nix ] Native Code [ Common · Win · Mac · Nix ] Manifest Native Code Overviews [ Common · Win · Mac · Nix · Strings ] Macros [ General · Native Macros ] Walkthroughs [ Java only · Java and native ] Building [ Win · Mac · Nix · OSX ] Distribution Issues

# Walkthrough: FileRegistry.initialize()

This file traces the steps when you call the FileRegistry.initialize() method.

Let's assume that we're running on Win95 with the SunJDK, and that the 'jcfactrz.txt' file contains the following:

    com.jconfig.mac.FileRegistryFactoryMac
    com.jconfig.win.FileRegistryFactoryWin

This example will trace through the following files (the com.jconfig part of the package name is omitted):

    FileRegistry.java
        FileRegistryFactory.java
            win.FileRegistryFactoryWin.java
                win.FileRegistryMSVM.java

## Step 1.

The FileRegistry.initialize() method looks like:

        public static void initialize( File curDir, int creator ) {
            FileRegistryFactory     factory;

            factory = new FileRegistryFactory();

            try {
                delegate = factory.createFileRegistry( curDir, creator );
            }
            catch ( Exception e ) {
                System.out.println( "FileRegistry.initialize, can't make delegate " + e );
                delegate = null;
            }
            catch ( Error ee ) {
                System.out.println( "FileRegistry.initialize, can't make delegate " + ee );
                delegate = null;
            }
        }

from FileRegistry.java

The 'delegate' is an object which implements the FileRegistryI interface, and most all other calls of the FileRegistry will be passed to this object. If it couldn't be created, 'delegate' is set to null. The other FileRegistry method check whether 'delegate' is null; if it is, the method returns an error code (such as null or -1)

## Step 2.

If this is the first time that the FileRegistryFactory.createFileRegistry() method has been called, it calls its 'initialize' method to create a FileRegistryI object. Otherwise, the previously created object is returned. Note however that if an object was not created the first time, obj will be null.

        public FileRegistryI createFileRegistry( File curDir, int creator ) {
            if ( !bBeenInited )
                initialize( curDir, creator );

            return obj;
        }

from FileRegistryFactory.java

## Step 3.

The 'initialize()' method looks like:

        private void initialize( File curDir, int creator ) {
            bBeenInited = true;

            tryCreatePlatformObject( curDir, creator );

            if ( obj == null )
                tryCreatePlainObject( curDir, creator );
        }

from FileRegistryFactory.java

This first tries to create a platform specific object, and, if that fails, it tries to create a 'plain' object (i.e., FileRegistryPlain, the fallback object).

## Step 4.

The tryCreatePlatformObject() method, with error checking removed, looks like:

        private void tryCreatePlatformObject( File curDir, int creator ) {
            FileRegistryFactoryI        factory;
            FileRegistryI               tempObj;
            Class                       factoryClass;
            File                        factoryFile;
            String                      classNames[];
            int                         i;

            factoryFile = new File( curDir, kFactoryFileName );
            classNames = JUtils.fileToStringArray( factoryFile );

            for ( i = 0; i < classNames.length; i++ ) {
                factoryClass = Class.forName( classNames[ i ] );
                factory = (FileRegistryFactoryI) factoryClass.newInstance();
                tempObj = factory.createFileRegistry( curDir, creator );
                if ( tempObj != null ) {
                    obj = tempObj;
                    return;
                }
            }
        }

from FileRegistryFactory.java

This opens the 'jcfactrz.txt' file, and converts the lines of this file into an array of Strings (the classNames array). So, in this example, classNames is:

    { "com.jconfig.win.FileRegistryFactoryWin", "com.jconfig.mac.FileRegistryFactoryMac" };

For each element of classNames, we try to create an object of that class. This object must implement the FileRegistryFactoryI interface. Then, that object's 'createFileRegistry()' method is called. If this method returns a non-null FileRegistryI object, FileRegistryFactory's 'obj' variable is set to that object, and we return: we're done.

## Step 5.

In this case, this method will call the 'FileRegistryFactoryWin.createFileRegistry()' method, which looks like:

        public FileRegistryI createFileRegistry( File curDir, int creator ) {
            if ( !bBeenInited )
                initialize( curDir, creator );

            return obj;
        }

from FileRegistryFactoryWin.java

This is similar to the FileRegistryFactory.createFileRegistry() method (see step 2.)

## Step 6.

The 'FileRegistryFactoryWin.initialize()' method looks like:

        private void initialize( File curDir, int creator ) {
            bBeenInited = true;

            figureOutPlatform();
            tryCreatePlatformSpecific( curDir, creator );
        }

from FileRegistryFactoryWin.java

The 'figureOutPlatform()' method uses Java's environment strings to determine what platform we're running on, and sets some boolean variables appropriately. In this case, the variables are:

        bIsPlatformMSVM15
        bIsPlatformMSVM20
        bIsPlatformSun11
        bIsPlatformMSVM15W
        bIsPlatformMSVM20W
        bIsPlatformSun11W

Currently, the Windows code can run on three different VMs: MSVM1.5, MSVM2.0+, and SunJDK. The ones ending in 'W' are set if we're on NT.

The 'tryCreatePlatformSpecific()' method modifies the 'obj' variable, which is returned from the 'FileRegistryFactoryWin.createFileRegistry()' method described in Step 5.

## Step 7.

The tryCreatePlatformSpecific() method has the following form:

        private void tryCreatePlatformSpecific( File curDir, int creator ) {
            if ( bIsPlatformMSVM15 )
                obj = new FileRegistryMSVM( FileRegistryMSVM.kMSVM1, curDir, creator );
            else if ( bIsPlatformMSVM20 )
                    obj = new FileRegistryMSVM( FileRegistryMSVM.kMSVM2, curDir, creator );
            //	repeat for the other four possibilities
        }

from FileRegistryFactoryWin.java

## Step 8.

The 'FileRegistryMSVM' class implements the FileRegistryI interface. This first argument to its constructor is a number indicating which version of the native code it should load. This class' constructor looks like:

        FileRegistryMSVM( int whichLibrary, File curDir, int creator ) throws ConfigException {
            if ( whichLibrary == kMSVM1 )
                libName = kMSVM1LibName;
            else if ( whichLibrary == kMSVM2 )
                libName = kMSVM2LibName;
            //	repeat for the other four possibilities

            Runtime.getRuntime().loadLibrary( libName );
        }

from FileRegistryMSVM.java

This object loads the native library, and initialization is done: the FileRegistryFactoryWin.createFileRegistry() method will return the FileRegistryMSVM object to the FileRegistryFactory object, which will return it to the FileRegistry.

So, all future calls to the FileRegistry will be delegated to the FileRegistryMSVM object.

If instead of running on the JDK, we were running on the MSVM, the FileRegistryFactoryWin.tryCreatePlatformSpecific() method would have created a FileRegistryMSVM object as above, except it would have told FileRegistryMSVM to load one of the MSVM DLLs instead of one of the Sun DLLs.

If instead of running on Windows, we were running on a PowerMac, the FileRegistryFactoryWin.createFileRegistry() method would have returned null. In this case, the FileRegistryFactory.tryCreatePlatformObject() method would have created an instance of FileRegistryFactoryMac, and given it a chance to create the FileRegistryI object.

If an error occured when trying to load one of the native DLLs or shared libraries, or if we were running on a platform other than Windows or a PowerMac, when the FileRegistryFactory.tryCreatePlatformObject() method returns, 'obj' would still be null. In this case, the FileRegistryFactory would call its 'tryCreatePlainObject' method, which would have created a FileRegistryPlain object.

Main Page · JConfig · ImageMeister · System Properties Repository · WordMeister · Free Samples · Java Freeware · Contact Us · FAQ · Links

Copyright (c) 1997-2002 Samizdat Productions. All Rights Reserved.
WarpMovie, TileMovie, JConfig, ImageMeister and MovieShredder are Trademarks of Samizdat Productions.