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.configuration;
35
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import info.magnolia.init.MagnoliaConfigurationProperties;
40 import info.magnolia.module.model.ComponentDefinition;
41 import info.magnolia.objectfactory.Classes;
42 import info.magnolia.objectfactory.ComponentConfigurationPath;
43 import info.magnolia.objectfactory.ComponentFactory;
44 import info.magnolia.objectfactory.ComponentProvider;
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public class LegacyComponentsConfigurer implements ComponentConfigurer {
64
65 private final static Logger log = LoggerFactory.getLogger(LegacyComponentsConfigurer.class);
66
67 @Override
68 public void doWithConfiguration(ComponentProvider parentComponentProvider, ComponentProviderConfiguration configuration) {
69
70 MagnoliaConfigurationProperties configurationProperties = parentComponentProvider.getComponent(MagnoliaConfigurationProperties.class);
71
72 if (configurationProperties != null) {
73 for (String key : configurationProperties.getKeys()) {
74 addComponent(configuration, key, configurationProperties.getProperty(key));
75 }
76 }
77 }
78
79 protected <T> void addComponent(ComponentProviderConfiguration componentProviderConfiguration, String key, String value) {
80
81 final Class<T> type = (Class<T>) classForName(key);
82 if (type == null) {
83 log.debug("{} does not seem to resolve to a class. (property value: {})", key, value);
84 return;
85 }
86
87 if (ComponentConfigurationPath.isComponentConfigurationPath(value)) {
88 componentProviderConfiguration.addComponent(getObserved(type, value));
89 } else {
90 Class<? extends T> valueType = (Class<? extends T>) classForName(value);
91 if (valueType == null) {
92 log.debug("{} does not seem to resolve a class or a configuration path. (property key: {})", value, key);
93 } else {
94 if (ComponentFactory.class.isAssignableFrom(valueType)) {
95 componentProviderConfiguration.addComponent(getComponentFactory(type, (Class<? extends ComponentFactory<T>>) valueType));
96 } else {
97 componentProviderConfiguration.addComponent(getImplementation(type, valueType));
98 componentProviderConfiguration.addTypeMapping(type, valueType);
99 }
100 }
101 }
102 }
103
104 protected <T> ImplementationConfiguration getImplementation(Class<T> type, Class<? extends T> implementation) {
105 ImplementationConfiguration configuration = new ImplementationConfiguration<T>();
106 configuration.setType(type);
107 configuration.setImplementation(implementation);
108 configuration.setScope(ComponentDefinition.SCOPE_SINGLETON);
109 configuration.setLazy(true);
110 return configuration;
111 }
112
113 protected <T> ProviderConfiguration<T> getComponentFactory(Class<T> type, Class<? extends ComponentFactory<T>> factoryClass) {
114 ProviderConfiguration<T> configuration = new ProviderConfiguration<T>();
115 configuration.setType(type);
116 configuration.setProviderClass(factoryClass);
117 configuration.setScope(ComponentDefinition.SCOPE_SINGLETON);
118 configuration.setLazy(true);
119 return configuration;
120 }
121
122 protected <T> ConfiguredComponentConfiguration<T> getObserved(Class<T> type, String workspaceAndPath) {
123 ComponentConfigurationPath path = new ComponentConfigurationPath(workspaceAndPath);
124 ConfiguredComponentConfiguration<T> configuration = new ConfiguredComponentConfiguration<T>();
125 configuration.setType(type);
126 configuration.setWorkspace(path.getRepository());
127 configuration.setPath(path.getPath());
128 configuration.setObserved(true);
129 configuration.setScope(ComponentDefinition.SCOPE_SINGLETON);
130 configuration.setLazy(true);
131 return configuration;
132 }
133
134 protected Class<?> classForName(String value) {
135 try {
136 return Classes.getClassFactory().forName(value);
137 } catch (ClassNotFoundException e) {
138 return null;
139 }
140 }
141 }