1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.objectfactory;
35
36
37
38
39
40
41
42
43
44
45 public class Components {
46
47 private static volatile ComponentProvider componentProvider = new NullComponentProvider();
48
49
50
51
52 public static void setComponentProvider(ComponentProvider provider) {
53 componentProvider = provider;
54 }
55
56
57
58
59 public static ComponentProvider getComponentProvider() {
60 return componentProvider;
61 }
62
63
64
65
66
67
68
69 public static <T> T getSingleton(Class<T> type) {
70 return getComponent(type);
71 }
72
73 public static <T> T newInstance(Class<T> type, Object... parameters) {
74 return getComponentProvider().newInstance(type, parameters);
75 }
76
77
78
79
80 public static <T> T getComponent(Class<T> type) {
81 return getComponentProvider().getComponent(type);
82 }
83
84 private static class NullComponentProvider implements ComponentProvider {
85
86 @Override
87 public <C> Class<? extends C> getImplementation(Class<C> type) throws ClassNotFoundException {
88 throw new IllegalStateException("No ComponentProvider has been set yet, something must have gone terribly wrong at startup.");
89 }
90
91 @Override
92 public <T> T getComponent(Class<T> type) {
93 throw new IllegalStateException("No ComponentProvider has been set yet, something must have gone terribly wrong at startup.");
94 }
95
96 @Override
97 public <T> T getSingleton(Class<T> type) {
98 throw new IllegalStateException("No ComponentProvider has been set yet, something must have gone terribly wrong at startup.");
99 }
100
101 public <T> T newInstance(Class<T> type) {
102 throw new IllegalStateException("No ComponentProvider has been set yet, something must have gone terribly wrong at startup.");
103 }
104
105 @Override
106 public <T> T newInstance(Class<T> type, Object... parameters) {
107 throw new IllegalStateException("No ComponentProvider has been set yet, something must have gone terribly wrong at startup.");
108 }
109
110 @Override
111 public <T> T newInstanceWithParameterResolvers(Class<T> type, ParameterResolver... parameters) {
112 throw new IllegalStateException("No ComponentProvider has been set yet, something must have gone terribly wrong at startup.");
113 }
114
115 @Override
116 public ComponentProvider getParent() {
117 throw new IllegalStateException("No ComponentProvider has been set yet, something must have gone terribly wrong at startup.");
118 }
119
120 }
121 }