1 package de.fhdw.wtf.context.core;
2
3 import java.io.IOException;
4 import java.lang.reflect.Modifier;
5
6 import de.fhdw.wtf.context.exception.DatabaseManagerFactoryNotInstantiableException;
7 import de.fhdw.wtf.context.exception.FrameworkException;
8 import de.fhdw.wtf.context.model.collections.PersistentList;
9 import de.fhdw.wtf.context.model.collections.PersistentListFactory;
10 import de.fhdw.wtf.context.model.collections.PersistentMap;
11 import de.fhdw.wtf.context.model.collections.PersistentMapFactory;
12 import de.fhdw.wtf.persistence.exception.ClassFacadeUninitializedException;
13 import de.fhdw.wtf.persistence.exception.PersistenceException;
14 import de.fhdw.wtf.persistence.exception.TypeOrAssociationNotFoundException;
15 import de.fhdw.wtf.persistence.facade.ClassFacade;
16 import de.fhdw.wtf.persistence.facade.DatabaseManager;
17 import de.fhdw.wtf.persistence.facade.DatabaseManagerFactory;
18 import de.fhdw.wtf.persistence.facade.TypeManager;
19 import de.fhdw.wtf.persistence.utils.PropertiesReader;
20 import de.fhdw.wtf.persistence.utils.PropertiesReaderFactory;
21 import de.fhdw.wtf.persistence.utils.PropertiesReaderFile;
22
23
24
25
26
27
28
29 public abstract class ApplicationStarter {
30
31
32
33
34 private static final String PACKAGE_DELIMITER = ".";
35
36
37
38
39 private static final String SOURCE_PACKAGE_OF_DATABASE_MANAGER_FACTORIES = "de" + PACKAGE_DELIMITER + "fhdw"
40 + PACKAGE_DELIMITER + "wtf" + PACKAGE_DELIMITER + "persistence" + PACKAGE_DELIMITER + "facade";
41
42
43
44
45 public static final Object ORACLE_DATABASE_MANAGER_FACTORY_NAME = "OracleDatabaseManagerFactory";
46
47
48
49
50
51 public void startStandalone() {
52 this.start(this.getResourcesPathJava());
53 }
54
55
56
57
58
59 public void startServer() {
60 this.start(this.getResourcesPathServer());
61 }
62
63
64
65
66
67
68
69 public void start(final String resourcesPath) {
70 PropertiesReader prop;
71 try {
72 prop = new PropertiesReaderFile();
73 prop.initialize(resourcesPath + "/" + this.getApplicationConfigFileName());
74
75 } catch (final Exception e) {
76 throw new FrameworkException(e.getMessage());
77 }
78 try {
79
80
81 this.initializeLogger();
82 ApplicationContainer.getInstance().setAppName(prop.getProperty("application-name"));
83 ApplicationContainer.getInstance().setUsedDatabaseManagerFactoryName(prop.getProperty("database"));
84 this.initializeDatabase(resourcesPath, ApplicationContainer.getInstance()
85 .getUsedDatabaseManagerFactoryName());
86 this.initializeRuntimePersistence();
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 this.initializeFactories();
109 this.registerActivities();
110 } catch (final PersistenceException e) {
111 throw new FrameworkException(e.getMessage());
112 } catch (final IOException e) {
113 throw new FrameworkException(e.getMessage());
114 } finally {
115 }
116 }
117
118
119
120
121
122 protected abstract void registerActivities();
123
124
125
126
127 public void stop() {
128 try {
129 ApplicationContainer.getInstance().getDatabaseManager().disconnect();
130 } catch (final PersistenceException e) {
131 throw new FrameworkException(e.getMessage());
132 }
133 }
134
135
136
137
138
139
140
141
142
143 private void initializeFactories() throws ClassFacadeUninitializedException, TypeOrAssociationNotFoundException {
144 final ObjectFactoryProvider factoryProvider = ObjectFactoryProvider.instance();
145 ApplicationContainer.getInstance().setFactoryProvider(factoryProvider);
146 final TypeManager typeManager =
147 ApplicationContainer.getInstance().getDatabaseManager().getClassFacade().getTypeManager();
148 try {
149 factoryProvider.registerTypeFactory(
150 typeManager,
151 PersistentList.class.getName(),
152 new PersistentListFactory());
153 } catch (final TypeOrAssociationNotFoundException e) {
154
155 }
156 try {
157 factoryProvider.registerTypeFactory(typeManager, PersistentMap.class.getName(), new PersistentMapFactory());
158 } catch (final TypeOrAssociationNotFoundException e) {
159
160 }
161 this.registerTypeFactories(factoryProvider, typeManager);
162 }
163
164
165
166
167
168
169
170
171
172
173
174 protected abstract void registerTypeFactories(ObjectFactoryProvider factoryProvider, TypeManager typeManager)
175 throws TypeOrAssociationNotFoundException;
176
177
178
179
180
181
182
183 private void initializeRuntimePersistence() throws PersistenceException {
184 final ClassFacade classFacade = ApplicationContainer.getInstance().getDatabaseManager().getClassFacade();
185 classFacade.initializeForRuntime();
186 }
187
188
189
190
191 private void initializeLogger() {
192 ApplicationContainer.getInstance().setLogger(Logger.getInstance());
193 }
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208 private void initializeDatabase(final String resourcesPath, final String databaseManagerFactoryClassName)
209 throws IOException, PersistenceException {
210 Class<?> newClass;
211 try {
212 newClass =
213 Class.forName(ApplicationStarter.SOURCE_PACKAGE_OF_DATABASE_MANAGER_FACTORIES + PACKAGE_DELIMITER
214 + databaseManagerFactoryClassName);
215 } catch (final ClassNotFoundException e) {
216 throw new DatabaseManagerFactoryNotInstantiableException(e.getMessage());
217 }
218 if (Modifier.isAbstract(newClass.getModifiers()) || !DatabaseManagerFactory.class.isAssignableFrom(newClass)) {
219 throw new DatabaseManagerFactoryNotInstantiableException(
220 DatabaseManagerFactoryNotInstantiableException.CLASS_IS_A_CONCRETE_DATABASE_MANAGER_FACTORY_REASON);
221 }
222 DatabaseManager database;
223 try {
224 database = ((DatabaseManagerFactory) newClass.newInstance()).getInstance();
225 } catch (final InstantiationException | IllegalAccessException e) {
226 throw new DatabaseManagerFactoryNotInstantiableException(e.getMessage());
227 }
228
229 database.setConnectionConstantsFromFile(PropertiesReaderFactory.getInstance().getPropertiesReaderFile(
230 resourcesPath + "/" + this.getDatabaseConfigFileName()));
231 database.connect();
232 ApplicationContainer.getInstance().setDatabaseManager(database);
233
234 }
235
236
237
238
239
240
241
242 protected abstract String getResourcesPathJava();
243
244
245
246
247
248
249
250 protected abstract String getResourcesPathServer();
251
252
253
254
255
256
257
258 protected abstract String getApplicationConfigFileName();
259
260
261
262
263
264
265 protected abstract String getDatabaseConfigFileName();
266
267
268
269
270
271
272
273 protected abstract String getModelPrefix();
274
275 }