1 /**
2 * AUTOHIT 2003
3 * Copyright Erich P Gatejen (c) 1989,1997,2003,2004
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Additional license information can be found in the documentation.
19 * @author Erich P Gatejen
20 */
21
22 package autohit.universe;
23
24 import java.io.InputStream;
25 import java.util.Hashtable;
26
27 import autohit.universe.service.UniverseLocal;
28
29 /**
30 * Universe factory. This builds universe services. All the functions are
31 * thread-safe, however it will not keep one thread from destroying
32 * another thread's universe. This is *NOT* a singleton.
33 *
34 * There are four types of universes:
35 * UNI_LOCAL Local file system
36 * UNI_MASTER NOT IMPLIMENTED!
37 * UNI_MIRROR NOT IMPLIMENTED!
38 * UNI_REMOTE NOT IMPLIMENTED!
39
40 *
41 * Regardless of what kind of universe, there must be a local property file
42 * that describes the universe. The factory will use it to build a
43 * server for that universe.
44 *
45 * @author Erich P. Gatejen
46 * @version 1.0
47 * <i>Version History</i>
48 * <code>EPG - New - 23Apr03</code>
49 *
50 */
51 public class UniverseFactory {
52
53 /**
54 * Table of universe services
55 * @param name universe name
56 * @return the loaded object
57 */
58 Hashtable services;
59
60 /**
61 * Constructor. Not a singleton, so build the services.
62 */
63 public UniverseFactory() {
64 services = new Hashtable();
65 }
66
67 /**
68 * Returns true if the named universe service exists.
69 * It will only throw an Exception on a software detected fault.
70 * @param name universe name
71 * @return true if the universe service is valid
72 */
73 public synchronized boolean exists(String name) throws UniverseException {
74 try {
75 if (services.containsKey(name)) return true;
76 } catch (Exception e) {
77 // fall out to return false
78 }
79 return false;
80 }
81
82 /**
83 * Get a reference to a valid, working universe service.
84 * @param name universe name
85 * @return reference to a universe service
86 * @throws autohit.universe.UniverseException
87 */
88 public synchronized Universe reference(String name) throws UniverseException {
89
90 if (!services.containsKey(name)) {
91 throw new UniverseException("Named universe service does not exist for this factory.", UniverseException.UE_NAMED_UNIVERSE_SERVICE_DOESNT_EXIST);
92 }
93 return (Universe) services.get(name);
94 }
95
96 /**
97 * Destroy a universe server
98 * @param name universe name
99 * @throws autohit.universe.UniverseException
100 */
101 public synchronized void destroy(String name) throws UniverseException {
102
103 if (!services.containsKey(name)) {
104 throw new UniverseException("Named universe service does not exist for this factory.", UniverseException.UE_NAMED_UNIVERSE_SERVICE_DOESNT_EXIST);
105 }
106 Universe u = (Universe)services.remove(name);
107 u.close();
108 }
109
110 /**
111 * Create a valid, working universe service
112 * @param handle handle to the universe. non-unique handle will overwrite a refernce to an existing universe service.
113 * @param prop a path to the local universe property file
114 * @return reference to a universe service
115 * @throws autohit.universe.UniverseException
116 */
117 public synchronized Universe create(String handle, String prop) throws UniverseException {
118 UniverseProperties up;
119 Universe u;
120 try {
121 up= new UniverseProperties(prop);
122 } catch (Exception e) {
123 throw new UniverseException("Serious universe creation error: " + e.getMessage(), UniverseException.UE_DEFAULT, e);
124 }
125 u = completeCreate(up);
126 services.put(handle, u);
127 return u;
128 }
129
130 /**
131 * Create a valid, working universe service
132 * @param handle handle to the universe. non-unique handle will overwrite a refernce to an existing universe service.
133 * @param prop a path to the local universe property file
134 * @return reference to a universe service
135 * @throws autohit.universe.UniverseException
136 */
137 public synchronized Universe create(String handle, InputStream prop) throws UniverseException {
138 UniverseProperties up;
139 Universe u;
140 try {
141 up= new UniverseProperties(prop);
142 } catch (Exception e) { throw (UniverseException)e; }
143 u = completeCreate(up);
144 services.put(handle, u);
145 return u;
146 }
147
148 /**
149 * Private
150 */
151 private Universe completeCreate(UniverseProperties prop) throws UniverseException {
152
153 Universe uTemp;
154
155 switch (prop.getType()) {
156 case UniverseProperties.UNI_LOCAL :
157 uTemp = (Universe) new UniverseLocal();
158 uTemp.genesis(prop);
159 break;
160
161 case UniverseProperties.UNI_EXTENDED :
162 throw new UniverseException("Universe Factory does not support extended types--YET", UniverseException.UE_NOT_SUPPORTED);
163 //break;
164
165 default :
166 throw new UniverseException("Universe Factory does not support creating type=" + prop.getType(), UniverseException.UE_NOT_SUPPORTED);
167 }
168 return uTemp;
169 }
170
171 }
172
173
174
|