KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > coldcore > coloradoftp > factory > ObjectFactory


1 package com.coldcore.coloradoftp.factory;
2
3 /**
4  * Object factory.
5  *
6  * Provides objects referred by name. Written as a static class this factory is available from all
7  * other objects in the same JVM. Configuration dictates what objects must be provided as singletons
8  * and wich must be not (every time a new object).
9  *
10  * This implementation delegates all calls to its internal factory which is responsible for
11  * finding requested objects.
12  *
13  *
14  * ColoradoFTP - The Open Source FTP Server (http://cftp.coldcore.com)
15  */

16 public class ObjectFactory {
17
18   protected static InternalFactory internalFactory;
19
20
21   private ObjectFactory() {}
22
23
24   /** Get internal factory
25    * @return Internal factory
26    */

27   public static InternalFactory getInternalFactory() {
28     return internalFactory;
29   }
30
31
32   /** Set new internal factory
33    * @param internalFactory Internal factory
34    */

35   public static void setInternalFactory(InternalFactory internalFactory) {
36     ObjectFactory.internalFactory = internalFactory;
37   }
38
39
40   /** Get object by name
41    * @param name Object name
42    * @return Requested object (never returns NULL)
43    */

44   public static Object JavaDoc getObject(String JavaDoc name) {
45     if (internalFactory == null) throw new IllegalStateException JavaDoc("Internal factory is not set");
46     Object JavaDoc o = internalFactory.getBean(name);
47     if (o == null) throw new IllegalArgumentException JavaDoc("Object "+name+" cannot be loaded");
48     return o;
49   }
50 }
51
Popular Tags