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.init;
35
36 import info.magnolia.cms.core.Path;
37 import info.magnolia.init.properties.FileSystemPropertySource;
38 import info.magnolia.init.properties.ServletContextPropertySource;
39 import org.apache.commons.lang.StringUtils;
40
41 import javax.inject.Inject;
42 import javax.inject.Singleton;
43 import javax.servlet.ServletContext;
44 import java.io.FileNotFoundException;
45 import java.io.IOException;
46 import java.util.ArrayList;
47 import java.util.List;
48
49 import static info.magnolia.cms.beans.config.PropertiesInitializer.processPropertyFilesString;
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 @Singleton
95 public class DefaultMagnoliaPropertiesResolver implements MagnoliaPropertiesResolver {
96 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DefaultMagnoliaPropertiesResolver.class);
97
98
99
100
101
102 protected static final String MAGNOLIA_INITIALIZATION_FILE = "magnolia.initialization.file";
103
104
105
106
107 protected static final String DEFAULT_INITIALIZATION_PARAMETER =
108 "WEB-INF/config/${servername}/${contextPath}/magnolia.properties,"
109 + "WEB-INF/config/${servername}/${webapp}/magnolia.properties,"
110 + "WEB-INF/config/${servername}/magnolia.properties,"
111 + "WEB-INF/config/${contextPath}/magnolia.properties,"
112 + "WEB-INF/config/${webapp}/magnolia.properties,"
113 + "WEB-INF/config/default/magnolia.properties,"
114 + "WEB-INF/config/magnolia.properties";
115
116 private final ServletContext context;
117 private final List<String> locations;
118
119 @Inject
120 public DefaultMagnoliaPropertiesResolver(ServletContext context, MagnoliaInitPaths initPaths) {
121 this.context = context;
122 String propertiesFilesString = getInitParameter(context, MAGNOLIA_INITIALIZATION_FILE, DEFAULT_INITIALIZATION_PARAMETER);
123
124
125 String contextPath = initPaths.getContextPath();
126 if (contextPath.length() == 0) {
127 contextPath = "ROOT";
128 } else {
129 contextPath = contextPath.substring(1);
130 }
131
132 final String propertiesLocationString = processPropertyFilesString(context, initPaths.getServerName(), initPaths.getWebappFolderName(), propertiesFilesString, contextPath);
133 final String[] propertiesLocation = propertiesLocationString.trim().split("[,]+",0);
134 this.locations = new ArrayList<String>(propertiesLocation.length);
135
136
137 for (String loc : propertiesLocation) {
138 locations.add(loc.trim());
139 }
140 }
141
142 protected String getInitParameter(ServletContext ctx, String name, String defaultValue) {
143 final String propertiesFilesString = ctx.getInitParameter(name);
144 if (StringUtils.isEmpty(propertiesFilesString)) {
145 log.debug("{} value in web.xml is undefined, falling back to default: {}", name, defaultValue);
146 return defaultValue;
147 }
148 log.debug("{} value in web.xml is :'{}'", name, propertiesFilesString);
149 return propertiesFilesString;
150 }
151
152
153
154
155 protected List<String> getLocations() {
156 return locations;
157 }
158
159 @Override
160 public List<PropertySource> getSources() {
161 final List<PropertySource> sources = new ArrayList<PropertySource>();
162 boolean foundFiles = false;
163 for (String location : getLocations()) {
164 try {
165 if (Path.isAbsolute(location)) {
166 sources.add(new FileSystemPropertySource(location));
167 } else {
168 sources.add(new ServletContextPropertySource(context, location));
169 }
170 foundFiles = true;
171 } catch (FileNotFoundException e) {
172 log.debug("Configuration file not found with path [{}]", location);
173 } catch (IOException e) {
174 throw new RuntimeException(e);
175 }
176 }
177 if (!foundFiles) {
178 log.warn("No configuration files found using location list {}.", getLocations());
179 }
180
181 return sources;
182 }
183 }