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

Commit baeb699

Browse files
committed
Add extra properties support
1 parent 8883e9e commit baeb699

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.util.Enumeration;
2424
import java.util.Properties;
2525

26+
import org.apache.catalina.Globals;
27+
2628

2729
/**
2830
* Utility class to read the bootstrap Catalina configuration.
@@ -105,6 +107,8 @@ private static void loadProperties() {
105107
properties=new Properties();
106108
}
107109

110+
loadExtraProperties();
111+
108112
// Register the properties as system properties
109113
Enumeration<?> enumeration = properties.propertyNames();
110114
while (enumeration.hasMoreElements()) {
@@ -125,6 +129,83 @@ private static String getConfigUrl() {
125129
}
126130

127131

132+
/**
133+
* Load extra properties files.
134+
*/
135+
private static void loadExtraProperties() {
136+
String extraPropertiesURLs = properties.getProperty("catalina.extra.properties.urls");
137+
if (extraPropertiesURLs != null) {
138+
for (String extraPropertiesURL : extraPropertiesURLs.split(",")) {
139+
InputStream is = null;
140+
try {
141+
String configUrl = replace(extraPropertiesURL.trim());
142+
if (configUrl != null) {
143+
is = (new URL(configUrl)).openStream();
144+
}
145+
} catch (Throwable t) {
146+
handleThrowable(t);
147+
}
148+
if (is != null) {
149+
try {
150+
properties.load(is);
151+
is.close();
152+
} catch (Throwable t) {
153+
handleThrowable(t);
154+
log.warn("Unable to load extra properties " + extraPropertiesURL);
155+
}
156+
} else {
157+
log.warn("Unable to find extra properties " + extraPropertiesURL);
158+
}
159+
}
160+
}
161+
}
162+
163+
164+
/**
165+
* System property replacement in the given string.
166+
*
167+
* @param str The original string
168+
* @return the modified string
169+
*/
170+
private static String replace(String str) {
171+
// Copied from Bootstrap.replace().
172+
String result = str;
173+
int pos_start = str.indexOf("${");
174+
if (pos_start >= 0) {
175+
StringBuilder builder = new StringBuilder();
176+
int pos_end = -1;
177+
while (pos_start >= 0) {
178+
builder.append(str, pos_end + 1, pos_start);
179+
pos_end = str.indexOf('}', pos_start + 2);
180+
if (pos_end < 0) {
181+
pos_end = pos_start - 1;
182+
break;
183+
}
184+
String propName = str.substring(pos_start + 2, pos_end);
185+
String replacement;
186+
if (propName.length() == 0) {
187+
replacement = null;
188+
} else if (Globals.CATALINA_HOME_PROP.equals(propName)) {
189+
replacement = Bootstrap.getCatalinaHome();
190+
} else if (Globals.CATALINA_BASE_PROP.equals(propName)) {
191+
replacement = Bootstrap.getCatalinaBase();
192+
} else {
193+
replacement = System.getProperty(propName);
194+
}
195+
if (replacement != null) {
196+
builder.append(replacement);
197+
} else {
198+
builder.append(str, pos_start, pos_end + 1);
199+
}
200+
pos_start = str.indexOf("${", pos_end + 1);
201+
}
202+
builder.append(str, pos_end + 1, str.length());
203+
result = builder.toString();
204+
}
205+
return result;
206+
}
207+
208+
128209
// Copied from ExceptionUtils since that class is not visible during start
129210
private static void handleThrowable(Throwable t) {
130211
if (t instanceof ThreadDeath) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.apache.catalina.startup;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.net.URL;
6+
7+
import org.apache.catalina.Globals;
8+
import org.junit.BeforeClass;
9+
import org.junit.Test;
10+
11+
public class CatalinaPropertiesTest {
12+
13+
@BeforeClass
14+
public static void setUpClass() {
15+
URL url = CatalinaPropertiesTest.class.getClassLoader().getResource("org/apache/catalina/startup/");
16+
System.setProperty(Globals.CATALINA_HOME_PROP, url.getFile());
17+
}
18+
19+
@Test
20+
public void loadProperties() {
21+
assertEquals("Catalina extra properties 1 not loaded", "true", CatalinaProperties.getProperty("catalina.extra.1.loaded"));
22+
assertEquals("Catalina extra properties 2 not loaded", "true", CatalinaProperties.getProperty("catalina.extra.2.loaded"));
23+
}
24+
25+
}
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)