Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Managers |
|
| 1.5714285714285714;1.571 |
1 | package org.jtheque.core.managers; | |
2 | ||
3 | /* | |
4 | * This file is part of JTheque. | |
5 | * | |
6 | * JTheque is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License. | |
9 | * | |
10 | * JTheque is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with JTheque. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
19 | import org.jtheque.core.managers.core.Core; | |
20 | import org.jtheque.core.managers.core.ICore; | |
21 | import org.jtheque.core.managers.error.IErrorManager; | |
22 | import org.jtheque.core.managers.log.ILoggingManager; | |
23 | import org.jtheque.utils.collections.CollectionUtils; | |
24 | ||
25 | import java.util.LinkedHashMap; | |
26 | import java.util.Map; | |
27 | ||
28 | /** | |
29 | * This class provide access to the different managers of the application. | |
30 | * | |
31 | * @author Baptiste Wicht | |
32 | */ | |
33 | public final class Managers { | |
34 | 0 | private static final Map<Class<?>, IManager> MANAGERS = new LinkedHashMap<Class<?>, IManager>(10); |
35 | ||
36 | /** | |
37 | * This is an utility class, not instanciable. | |
38 | */ | |
39 | private Managers() { | |
40 | 0 | super(); |
41 | 0 | } |
42 | ||
43 | /** | |
44 | * Pre-init the different managers of the application. | |
45 | */ | |
46 | public static void preInitManagers() { | |
47 | 0 | for (IManager manager : MANAGERS.values()) { |
48 | 0 | manager.preInit(); |
49 | } | |
50 | 0 | } |
51 | ||
52 | /** | |
53 | * Init the different managers of the application. | |
54 | */ | |
55 | public static void initManagers() { | |
56 | 0 | for (IManager manager : MANAGERS.values()) { |
57 | try { | |
58 | 0 | manager.init(); |
59 | 0 | } catch (ManagerException e) { |
60 | 0 | Managers.getManager(ILoggingManager.class).getLogger(Managers.class).error(e); |
61 | 0 | Managers.getManager(IErrorManager.class).addInternationalizedError("error.loading.manager"); |
62 | 0 | } |
63 | } | |
64 | 0 | } |
65 | ||
66 | /** | |
67 | * Close the different managers of the application. | |
68 | */ | |
69 | public static void closeManagers() { | |
70 | 0 | CollectionUtils.reverse(MANAGERS); |
71 | ||
72 | 0 | for (IManager manager : MANAGERS.values()) { |
73 | 0 | manager.close(); |
74 | } | |
75 | 0 | } |
76 | ||
77 | /** | |
78 | * Return the core. | |
79 | * | |
80 | * @return The core. | |
81 | */ | |
82 | public static ICore getCore() { | |
83 | 0 | return Core.getInstance(); |
84 | } | |
85 | ||
86 | /** | |
87 | * Return the manager with the specified class. | |
88 | * | |
89 | * @param managerClass The class of the manager. | |
90 | * @param <T> The type of the manager. | |
91 | * @return The manager. | |
92 | */ | |
93 | public static <T> T getManager(Class<T> managerClass) { | |
94 | 0 | return (T) MANAGERS.get(managerClass); |
95 | } | |
96 | ||
97 | /** | |
98 | * Load the manager from the manager container. | |
99 | * | |
100 | * @param container The manager container. | |
101 | */ | |
102 | static void loadManagers(ManagerContainer container) { | |
103 | 0 | MANAGERS.putAll(container.getManagers()); |
104 | 0 | } |
105 | } |