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