1 | 0 | |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
package pt.digitalis.dif.startup; |
7 | |
|
8 | |
import java.util.Collections; |
9 | |
import java.util.Comparator; |
10 | |
import java.util.Iterator; |
11 | |
import java.util.List; |
12 | |
import java.util.Stack; |
13 | |
|
14 | |
import pt.digitalis.dif.codegen.DIFCodeGenerator; |
15 | |
import pt.digitalis.dif.controller.interfaces.IModelManager; |
16 | |
import pt.digitalis.dif.controller.security.managers.IAuthenticationManager; |
17 | |
import pt.digitalis.dif.controller.security.managers.IIdentityManager; |
18 | |
import pt.digitalis.dif.controller.security.managers.impl.IdentityManagerStaticImpl; |
19 | |
import pt.digitalis.dif.dem.interfaces.IApplication; |
20 | |
import pt.digitalis.dif.dem.interfaces.IApplicationConfiguration; |
21 | |
import pt.digitalis.dif.dem.interfaces.IApplicationPrivate; |
22 | |
import pt.digitalis.dif.dem.interfaces.IDIFAPI; |
23 | |
import pt.digitalis.dif.dem.interfaces.IProvider; |
24 | |
import pt.digitalis.dif.dem.interfaces.IService; |
25 | |
import pt.digitalis.dif.dem.interfaces.IStage; |
26 | |
import pt.digitalis.dif.dem.managers.IDEMManager; |
27 | |
import pt.digitalis.dif.dem.managers.impl.UsageIssuesManagerImpl; |
28 | |
import pt.digitalis.dif.dem.objects.issues.UsageIssue; |
29 | |
import pt.digitalis.dif.exception.InternalFrameworkException; |
30 | |
import pt.digitalis.dif.exception.codegen.DIFCodeGenerationException; |
31 | |
import pt.digitalis.dif.ioc.DIFIoCRegistry; |
32 | |
import pt.digitalis.dif.utils.logging.DIFLogger; |
33 | |
import pt.digitalis.log.LogLevel; |
34 | |
import pt.digitalis.utils.bytecode.exceptions.CodeGenerationException; |
35 | |
import pt.digitalis.utils.inspection.exception.ResourceNotFoundException; |
36 | |
import pt.digitalis.utils.ioc.IIoCRegistry; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 0 | final public class DIFInitializer { |
47 | |
|
48 | |
|
49 | |
final static private String INDENTATION = " "; |
50 | |
|
51 | |
|
52 | 0 | static private boolean initialized = false; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
static private void addIssuesToBuffer(String location, StringBuffer buffer, String indentPrefix) |
65 | |
{ |
66 | |
|
67 | 0 | List<UsageIssue> issues = null; |
68 | |
|
69 | 0 | issues = UsageIssuesManagerImpl.getInstance().getIssues(location); |
70 | |
|
71 | 0 | for (UsageIssue usageIssue: issues) |
72 | 0 | buffer.append(indentPrefix + " => " + usageIssue.getIssueType() + ": " + usageIssue.getIssueDescription() |
73 | 0 | + "\n"); |
74 | 0 | } |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
static private void dumpDEM() |
80 | |
{ |
81 | |
|
82 | 0 | StringBuffer buffer = new StringBuffer(); |
83 | 0 | Stack<String> indentationsStack = new Stack<String>(); |
84 | 0 | String currentIndentation = null; |
85 | |
|
86 | 0 | buffer.append("\n\nDumping DEM structure:\n"); |
87 | 0 | buffer.append("-----------------------------------------\n"); |
88 | |
|
89 | 0 | IDEMManager dem = DIFIoCRegistry.getRegistry().getImplementation(IDEMManager.class); |
90 | |
|
91 | 0 | Iterator<IProvider> providers = dem.getProviders().values().iterator(); |
92 | |
|
93 | 0 | while (providers.hasNext()) |
94 | |
{ |
95 | 0 | IProvider provider = providers.next(); |
96 | |
|
97 | 0 | if (providers.hasNext()) |
98 | |
{ |
99 | 0 | currentIndentation = INDENTATION + "| "; |
100 | 0 | buffer.append(INDENTATION + "|-- Provider: " + provider.getName() + " (" + provider.getID() + ")\n"); |
101 | |
} |
102 | |
else |
103 | |
{ |
104 | 0 | currentIndentation = INDENTATION + " "; |
105 | 0 | buffer.append(INDENTATION + "`-- Provider: " + provider.getName() + " (" + provider.getID() + ")\n"); |
106 | |
} |
107 | |
|
108 | 0 | if (UsageIssuesManagerImpl.getInstance().hasIssues(provider.getOriginalClassName())) |
109 | 0 | addIssuesToBuffer(provider.getOriginalClassName(), buffer, currentIndentation); |
110 | |
|
111 | |
|
112 | 0 | Iterator<IApplication> applications = provider.getApplications().values().iterator(); |
113 | |
|
114 | 0 | while (applications.hasNext()) |
115 | |
{ |
116 | 0 | IApplication application = applications.next(); |
117 | 0 | indentationsStack.add(currentIndentation); |
118 | |
|
119 | 0 | if (applications.hasNext()) |
120 | |
{ |
121 | 0 | buffer.append(currentIndentation + INDENTATION + "|-- Application: " + application.getName() + " (" |
122 | 0 | + application.getID() + ")\n"); |
123 | 0 | currentIndentation += INDENTATION + "| "; |
124 | |
} |
125 | |
else |
126 | |
{ |
127 | 0 | buffer.append(currentIndentation + INDENTATION + "`-- Application: " + application.getName() + " (" |
128 | 0 | + application.getID() + ")\n"); |
129 | 0 | currentIndentation += INDENTATION + " "; |
130 | |
} |
131 | |
|
132 | 0 | if (UsageIssuesManagerImpl.getInstance().hasIssues(application.getOriginalClassName())) |
133 | 0 | addIssuesToBuffer(application.getOriginalClassName(), buffer, currentIndentation); |
134 | |
|
135 | |
|
136 | 0 | Iterator<IService> services = application.getServices().values().iterator(); |
137 | |
|
138 | 0 | while (services.hasNext()) |
139 | |
{ |
140 | 0 | IService service = services.next(); |
141 | 0 | indentationsStack.add(currentIndentation); |
142 | |
|
143 | 0 | if (services.hasNext()) |
144 | |
{ |
145 | 0 | buffer.append(currentIndentation + INDENTATION + "|-- Service: " + service.getName() + " (" |
146 | 0 | + service.getID() + ")\n"); |
147 | 0 | currentIndentation += INDENTATION + "| "; |
148 | |
} |
149 | |
else |
150 | |
{ |
151 | 0 | buffer.append(currentIndentation + INDENTATION + "`-- Service: " + service.getName() + " (" |
152 | 0 | + service.getID() + ")\n"); |
153 | 0 | currentIndentation += INDENTATION + " "; |
154 | |
} |
155 | |
|
156 | 0 | if (UsageIssuesManagerImpl.getInstance().hasIssues(service.getOriginalClassName())) |
157 | 0 | addIssuesToBuffer(service.getOriginalClassName(), buffer, currentIndentation); |
158 | |
|
159 | |
|
160 | 0 | Iterator<IStage> stages = service.getStages().values().iterator(); |
161 | |
|
162 | 0 | while (stages.hasNext()) |
163 | |
{ |
164 | 0 | IStage stage = stages.next(); |
165 | 0 | indentationsStack.add(currentIndentation); |
166 | |
|
167 | 0 | if (stages.hasNext()) |
168 | |
{ |
169 | 0 | buffer.append(currentIndentation + INDENTATION + "|-- " + stage.getName() + " (" |
170 | 0 | + stage.getID() + ")\n"); |
171 | 0 | currentIndentation += INDENTATION + "| "; |
172 | |
} |
173 | |
else |
174 | |
{ |
175 | 0 | buffer.append(currentIndentation + INDENTATION + "`-- " + stage.getName() + " (" |
176 | 0 | + stage.getID() + ")\n"); |
177 | 0 | currentIndentation += INDENTATION + " "; |
178 | |
} |
179 | |
|
180 | 0 | if (UsageIssuesManagerImpl.getInstance().hasIssues(stage.getOriginalClassName())) |
181 | 0 | addIssuesToBuffer(stage.getOriginalClassName(), buffer, currentIndentation); |
182 | |
|
183 | 0 | currentIndentation = indentationsStack.pop(); |
184 | |
} |
185 | |
|
186 | 0 | buffer.append(currentIndentation + "\n"); |
187 | 0 | currentIndentation = indentationsStack.pop(); |
188 | |
} |
189 | |
|
190 | 0 | currentIndentation = indentationsStack.pop(); |
191 | |
} |
192 | |
} |
193 | |
|
194 | 0 | buffer.append("-----------------------------------------\n\n"); |
195 | |
|
196 | 0 | if (DIFStartupConfiguration.getLogLevel().equals(LogLevel.DEBUG)) |
197 | 0 | DIFLogger.getLogger().debug(buffer.toString()); |
198 | |
else |
199 | 0 | DIFLogger.getLogger().info(buffer.toString()); |
200 | 0 | } |
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
synchronized static public boolean initialize(boolean initDEMInstances, boolean upgradeModelManagers) |
212 | |
{ |
213 | 0 | if (!isInitialized()) |
214 | |
{ |
215 | |
try |
216 | |
{ |
217 | |
|
218 | 0 | IIoCRegistry iocRegistry = DIFIoCRegistry.getRegistry(); |
219 | |
|
220 | |
|
221 | 0 | iocRegistry.getImplementation(IIdentityManager.class); |
222 | 0 | iocRegistry.getImplementation(IAuthenticationManager.class); |
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | 0 | if (upgradeModelManagers) |
231 | |
{ |
232 | |
|
233 | 0 | DIFLogger.getLogger().info("Initializing Model Manager upgrades..."); |
234 | 0 | DIFLogger.getLogger().increaseIndentation(); |
235 | 0 | for (IModelManager modelManager: DIFIoCRegistry.getRegistry().getImplementations( |
236 | 0 | IModelManager.class)) |
237 | |
{ |
238 | 0 | if (modelManager.isEnabled()) |
239 | |
{ |
240 | 0 | if (!modelManager.isUpToDate()) |
241 | |
{ |
242 | 0 | String currentVersion = modelManager.getCurrentVersion(); |
243 | 0 | DIFLogger.getLogger().info( |
244 | 0 | "Model '" + modelManager.getSchema() + "': Upgrading from " + currentVersion); |
245 | |
|
246 | 0 | modelManager.updateVersion(); |
247 | 0 | DIFLogger.getLogger().info( |
248 | 0 | "Model '" + modelManager.getSchema() + "': Upgraded to " |
249 | 0 | + modelManager.getCurrentVersion()); |
250 | |
} |
251 | |
else |
252 | |
{ |
253 | 0 | DIFLogger.getLogger().info("Model '" + modelManager.getSchema() + "': Up to date"); |
254 | |
} |
255 | |
} |
256 | |
} |
257 | 0 | DIFLogger.getLogger().decreaseIndentation(); |
258 | |
} |
259 | |
|
260 | |
|
261 | 0 | DIFCodeGenerator codeGen = new DIFCodeGenerator(); |
262 | 0 | iocRegistry.injectDependencies(codeGen); |
263 | |
|
264 | 0 | if (initDEMInstances) |
265 | |
{ |
266 | |
|
267 | 0 | codeGen.collectRegisteredPackages(); |
268 | |
|
269 | |
|
270 | 0 | codeGen.searchRegisteredPackages(); |
271 | |
|
272 | |
|
273 | 0 | codeGen.enhanceDEMClasses(); |
274 | |
} |
275 | |
|
276 | |
|
277 | 0 | if (DIFStartupConfiguration.getLogLevel().equals(LogLevel.DEBUG)) |
278 | 0 | UsageIssuesManagerImpl.getInstance().logAllIssuesByType(); |
279 | |
|
280 | |
|
281 | 0 | codeGen.cleanUp(); |
282 | |
|
283 | |
|
284 | 0 | initializeAPIs(); |
285 | |
|
286 | |
|
287 | 0 | initializeApplications(); |
288 | |
|
289 | |
|
290 | 0 | IIdentityManager identityManager = DIFIoCRegistry.getRegistry().getImplementation( |
291 | 0 | IIdentityManager.class); |
292 | 0 | if (identityManager instanceof IdentityManagerStaticImpl) |
293 | 0 | ((IdentityManagerStaticImpl) identityManager).initializeStaticCustomUsers(); |
294 | |
|
295 | 0 | initialized = true; |
296 | |
|
297 | |
|
298 | |
|
299 | 0 | if (DIFStartupConfiguration.getLogLevel().equals(LogLevel.DEBUG) |
300 | 0 | || UsageIssuesManagerImpl.getInstance().hasIssues() |
301 | 0 | || DIFStartupConfiguration.getDeveloperMode() || DIFStartupConfiguration.getTestingMode()) |
302 | |
{ |
303 | 0 | dumpDEM(); |
304 | |
} |
305 | |
|
306 | |
} |
307 | 0 | catch (ResourceNotFoundException resourceNotFoundException) |
308 | |
{ |
309 | 0 | resourceNotFoundException.printStackTrace(); |
310 | |
|
311 | |
} |
312 | 0 | catch (CodeGenerationException codeGenerationException) |
313 | |
{ |
314 | 0 | codeGenerationException.printStackTrace(); |
315 | |
} |
316 | 0 | catch (DIFCodeGenerationException e) |
317 | |
{ |
318 | 0 | e.printStackTrace(); |
319 | |
} |
320 | 0 | catch (InternalFrameworkException e) |
321 | |
{ |
322 | 0 | throw new RuntimeException(e); |
323 | |
} |
324 | |
|
325 | |
} |
326 | 0 | return initialized; |
327 | |
} |
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
private static void initializeAPIs() throws InternalFrameworkException |
335 | |
{ |
336 | 0 | List<IDIFAPI> list = DIFIoCRegistry.getRegistry().getImplementations(IDIFAPI.class); |
337 | |
|
338 | 0 | Collections.sort(list, new Comparator<IDIFAPI>() { |
339 | |
|
340 | |
public int compare(IDIFAPI a, IDIFAPI b) |
341 | |
{ |
342 | 0 | return a.order().compareTo(b.order()); |
343 | |
} |
344 | |
}); |
345 | |
|
346 | 0 | for (IDIFAPI difApi: list) |
347 | |
{ |
348 | 0 | difApi.initialize(); |
349 | |
} |
350 | |
|
351 | 0 | } |
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
synchronized static public void initializeApplications() |
357 | |
{ |
358 | 0 | List<IApplicationConfiguration> appConfImpls = DIFIoCRegistry.getRegistry().getImplementations( |
359 | 0 | IApplicationConfiguration.class); |
360 | 0 | if (appConfImpls.size() > 0) |
361 | |
{ |
362 | 0 | appConfImpls.get(0).processConfigurations(); |
363 | 0 | DIFLogger.getLogger() |
364 | 0 | .info("Initializing Applications Configurations using: " |
365 | 0 | + appConfImpls.get(0).getClass().getSimpleName()); |
366 | |
} |
367 | |
|
368 | 0 | IDEMManager demManager = DIFIoCRegistry.getRegistry().getImplementation(IDEMManager.class); |
369 | 0 | DIFLogger.getLogger().info("Initializing Applications..."); |
370 | |
|
371 | 0 | for (IProvider provider: demManager.getProviders().values()) |
372 | 0 | for (IApplication application: provider.getApplications().values()) |
373 | 0 | ((IApplicationPrivate) application).__CG__initialize(); |
374 | 0 | } |
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
synchronized static public boolean isInitialized() |
382 | |
{ |
383 | 0 | return initialized; |
384 | |
} |
385 | |
} |