KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > core > AbstractFactory


1 package com.sslexplorer.core;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5
6 /**
7  * Abstract Factory
8  *
9  * @author James D Robinson <a HREF="mailto:james@3sp.com">&lt;james@3sp.com&gt;</a> *
10  * @author Brett Smith <a HREF="mailto:brett@3sp.com">&lt;brett@3sp.com&gt;</a> *
11  *
12  */

13 public class AbstractFactory {
14     static Log log = LogFactory.getLog(AbstractFactory.class);
15
16     static AbstractFactory instance;
17     static Class JavaDoc factoryImpl = null;
18     private static boolean locked = false;
19
20     /**
21      * Return the created factory. Implementations will probably want to
22      * provide their own get instance that provides a correctly type
23      * instance.
24      *
25      * @return An instance of the factory.
26      */

27     public static AbstractFactory getAbstractInstance() {
28         try {
29             if(instance == null) {
30                 if(factoryImpl == null) {
31                     throw new Exception JavaDoc("Not factory implementation class has been set.");
32                 }
33                 instance = (AbstractFactory) factoryImpl.newInstance();
34             }
35             return instance;
36         } catch (Exception JavaDoc e) {
37             log.error("Could not create instance of class " + factoryImpl.getCanonicalName(), e);
38             return instance == null ? instance = new AbstractFactory() : instance;
39         }
40     }
41
42     /**
43      * Set the class the use for the factory implementation. This must
44      * be called before the first {@link #getAbstractInstance()} call.
45      *
46      * @param factoryImpl the class of the factory implementation
47      * @param lock whether to lock the policy database after setting it.
48      * @throws IllegalStateException if factory implementation has already been locked
49      */

50     public static void setFactoryImpl(Class JavaDoc factoryImpl, boolean lock) throws IllegalStateException JavaDoc {
51         if (locked) {
52             throw new IllegalStateException JavaDoc("Factory has been locked by someone else.");
53         }
54         AbstractFactory.factoryImpl = factoryImpl;
55         locked = lock;
56     }
57 }
58
Popular Tags