KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > OzoneObjectFactory


1 /*
2 Normally a class like this would be generated by OPP. Unfortunately this cannot
3 (yet?) be the case for OzoneObject, as that class does not implement OzoneRemote.
4 */

5 package org.ozoneDB;
6
7 import org.ozoneDB.tools.OPP.OPP;
8
9 /**
10  * <p>Factory pattern class for creating ozone objects.</p>
11  * <p> You can use this class for retrieving objects from the database where
12  * the type is not known beforehand.</p>
13  * A factory has a bit of a schizophrenic nature: on the client-side it 'links'
14  * to an ExternalDatabase, while on the server-side it does so to the Database
15  * that holds the instances that the factory creates. Note however that a
16  * factory running inside an Ozone server can also be linked to an External
17  * database outside that server (userclient -> server A -> server B). In that
18  * case such a factory would be 'linked' to an ExternalDatabase.</p>
19  * <p>The idea behind factories is threefold:<ul>
20
21  * <li>provide an abstraction from Ozone specific object creation</li>
22  * <li>facilitate creating objects with non-default constructors</li>
23  * <li>provide (almost) the same interface on the server side and the client
24  * side on object creation</li></ul>
25  * The differences in client side and server side operation are:<ul>
26  * <li>on the server side <code>getDefault()</code> returns a factory that is
27  * creates its objects inside that same server database and on the client side
28  * it returns a factory that creates its objects in the default database</li>
29  * <li>In order to use <code>getDefault()<code> on the client side, you need to
30  * call <code>setDefaultDatabaseUrl(String)</code> to specify the default
31  * database.</li>
32  * In a typical real-world scenario where you connect to only one ozone database
33  * you would call <code>setDefaultDatabaseUrl(String)</code> on the client only
34  * once, and for the rest of the programs lifespan call
35  * <code>CarTypeImplFactory.getDefault().create()</code> methods both on the server and
36  * client sides to create objects.</p>
37  * <p>Note: if you do not have a clue what factories are and how they work, you
38  * should probably brush up on your knowledge of
39  * <a HREF="http://www.google.com/search?q=java+design+patterns+GoF">design patterns</a></p>.
40  * @author <a HREF="mailto://ozone-db.orgATmekenkampD0Tcom">Leo Mekenkamp</a>
41  * @since FIXME(since when?)
42  */

43
44 public final class OzoneObjectFactory extends AbstractFactory {
45
46     private static class Info implements org.ozoneDB.FactoryClassInfo {
47         public final void defaultDatabaseUrlChanged() {
48             defaultInstance = null;
49         }
50     }
51
52     static {
53         addFactoryClassInfo(new Info());
54     }
55
56     private static OzoneObjectFactory defaultInstance = null;
57
58     /**
59      * On the client side: returns a factory that is linked to a database
60      * specified by the url passed to <code>setDefaultDatabaseUrl</code>. On the
61      * server side: returns a factory that is linked to the server database.
62      * Note that multiple calls to this method return the same value over and
63      * over again, until <code>setDefaultDatabaseUrl</code> has been called.
64      */

65     public static synchronized OzoneObjectFactory getDefault() throws Exception JavaDoc {
66         if (defaultInstance == null) {
67             defaultInstance = new OzoneObjectFactory();
68         }
69         return defaultInstance;
70     }
71
72     /**
73      * <p>Default constructor: creates a factory that is linked to the default
74      * database.</p>
75      */

76     public OzoneObjectFactory() throws Exception JavaDoc {
77     }
78
79     /** <p>Creates a factory that creates its objects in the database specified by
80      * <code>url</code>. Note that this constructor can only be used if the database in
81      * question is not opened by this client. Use <code>CarTypeImplFactory()</code> or
82      * <code>CarTypeImplFactory(Factory)</code> to create a factory for an already opened
83      * database. This might seem strange, or even annoying at first, but it is very
84      * logical from an object oriented point of view: you probably want a factory that
85      * creates its objects in the default database, or in the same realm as another
86      * factory you already have created...</p>
87      * @param url defining the remote database (something like
88      * <code>ozone:remote://localhost:3333</code>)
89      */

90     public OzoneObjectFactory(String JavaDoc url) throws Exception JavaDoc {
91         super(url);
92     }
93
94     /** <p>Creates a factory that creates its objects in the same database as a specific
95      * other fatory does.</p>
96      * @param factory the factory that creates its objects in the same database as the new factory
97      * should
98      */

99     public OzoneObjectFactory(AbstractFactory factory) {
100         super(factory);
101     }
102
103     /**
104      * Gets called automatically to indicate that the default database has been
105      * closed. DO NOT CALL THIS METHOD YOURSELF.
106      */

107     protected void defaultClosed() {
108         defaultInstance = null;
109     }
110
111     /**
112      * <p>Retrieves an object through its handle. <emp>Do not use this function
113      * unless you are perfectly sure about what you are doing</emp>. See the
114      * examples for some hands-on information on when to use handles. Note that
115      * normally one would use names or just standard java object references.</p>
116      */

117     public OzoneRemote objectForHandle(String JavaDoc handle) throws Exception JavaDoc {
118         return (OzoneRemote) getDatabase().objectForHandle(handle);
119     }
120
121     /**
122      * <p>Retrieves an object from the database through its name.</p>
123      */

124     public OzoneRemote objectForName(String JavaDoc name) throws Exception JavaDoc {
125         return (OzoneRemote) getDatabase().objectForName(name);
126     }
127
128 }
129
Popular Tags