view src/org/dancres/blitz/BootContext.java @ 27:511648fa4d64 Version 2.1

Version to 2.1
author Dan Creswell <dan.creswell@gmail.com>
date Mon, 04 Jan 2010 13:00:40 +0000
parents 3dc0c5604566
children
line wrap: on
line source

package org.dancres.blitz;

import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;

/**
   Various components within Blitz require access to certain specific bits
   of information concerning Boot state.  BootContext is responsible for
   holding all these bits of information and making them available to the
   components.
 */
public class BootContext {
    private static List theContext = new LinkedList();

    public static void add(BootInfo anInfo) {
        synchronized(theContext) {
            theContext.add(anInfo);
        }
    }

    public static BootInfo get(Class aClass) {
        validate(aClass);

        synchronized(theContext) {
            Iterator myInfos = theContext.iterator();

            while (myInfos.hasNext()) {
                BootInfo myInfo = (BootInfo) myInfos.next();

                if (aClass.isInstance(myInfo))
                    return myInfo;
            }
        }

        return null;
    }

    private static void validate(Class aClass) {
        Class[] myInterfaces = aClass.getInterfaces();

        for (int i = 0 ; i < myInterfaces.length; i++) {
            if (myInterfaces[i].equals(BootInfo.class))
                return;
        }

        throw new RuntimeException("Attempt to store object which is not a BootInfo");
    }
}