Skip to content
This repository was archived by the owner on Feb 21, 2025. It is now read-only.

Commit 4ae9fbd

Browse files
committed
Add extra properties support
1 parent 8bbc810 commit 4ae9fbd

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

src/main/java/org/apache/catalina/startup/CatalinaProperties.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ private static void loadProperties() {
131131
properties=new Properties();
132132
}
133133

134+
loadExtraProperties();
135+
134136
// Register the properties as system properties
135137
Enumeration enumeration = properties.propertyNames();
136138
while (enumeration.hasMoreElements()) {
@@ -169,4 +171,79 @@ private static String getConfigUrl() {
169171
}
170172

171173

174+
/**
175+
* Load extra properties files.
176+
*/
177+
private static void loadExtraProperties() {
178+
String extraPropertiesURLs = properties.getProperty("catalina.extra.properties.urls");
179+
if (extraPropertiesURLs != null) {
180+
for (String extraPropertiesURL : extraPropertiesURLs.split(",")) {
181+
InputStream is = null;
182+
try {
183+
String configUrl = replace(extraPropertiesURL.trim());
184+
if (configUrl != null) {
185+
is = (new URL(configUrl)).openStream();
186+
}
187+
} catch (Throwable t) {
188+
// Ignore
189+
}
190+
if (is != null) {
191+
try {
192+
properties.load(is);
193+
is.close();
194+
} catch (Throwable t) {
195+
log.warn("Unable to load extra properties " + extraPropertiesURL);
196+
}
197+
} else {
198+
log.warn("Unable to find extra properties " + extraPropertiesURL);
199+
}
200+
}
201+
}
202+
}
203+
204+
205+
/**
206+
* System property replacement in the given string.
207+
*
208+
* @param str The original string
209+
* @return the modified string
210+
*/
211+
private static String replace(String str) {
212+
// Copied from Bootstrap.replace() in 7.0.50
213+
String result = str;
214+
int pos_start = str.indexOf("${");
215+
if (pos_start >= 0) {
216+
StringBuilder builder = new StringBuilder();
217+
int pos_end = -1;
218+
while (pos_start >= 0) {
219+
builder.append(str, pos_end + 1, pos_start);
220+
pos_end = str.indexOf('}', pos_start + 2);
221+
if (pos_end < 0) {
222+
pos_end = pos_start - 1;
223+
break;
224+
}
225+
String propName = str.substring(pos_start + 2, pos_end);
226+
String replacement;
227+
if (propName.length() == 0) {
228+
replacement = null;
229+
} else if ("catalina.home".equals(propName)) {
230+
replacement = Bootstrap.getCatalinaHome();
231+
} else if ("catalina.base".equals(propName)) {
232+
replacement = Bootstrap.getCatalinaBase();
233+
} else {
234+
replacement = System.getProperty(propName);
235+
}
236+
if (replacement != null) {
237+
builder.append(replacement);
238+
} else {
239+
builder.append(str, pos_start, pos_end + 1);
240+
}
241+
pos_start = str.indexOf("${", pos_end + 1);
242+
}
243+
builder.append(str, pos_end + 1, str.length());
244+
result = builder.toString();
245+
}
246+
return result;
247+
}
248+
172249
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.apache.catalina.startup;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.net.URL;
6+
7+
import org.junit.BeforeClass;
8+
import org.junit.Test;
9+
10+
public class CatalinaPropertiesTest {
11+
12+
@BeforeClass
13+
public static void setUpClass() {
14+
URL url = CatalinaPropertiesTest.class.getClassLoader().getResource("org/apache/catalina/startup/");
15+
System.setProperty("catalina.home", url.getFile());
16+
}
17+
18+
@Test
19+
public void loadProperties() {
20+
assertEquals("Catalina extra properties 1 not loaded", "true", CatalinaProperties.getProperty("catalina.extra.1.loaded"));
21+
assertEquals("Catalina extra properties 2 not loaded", "true", CatalinaProperties.getProperty("catalina.extra.2.loaded"));
22+
}
23+
24+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
catalina.extra.1.loaded=true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
catalina.extra.2.loaded=true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
catalina.extra.properties.urls=file://${catalina.home}/catalina.extra.1.properties,file://${catalina.home}/catalina.extra.2.properties

0 commit comments

Comments
 (0)