Skip to content

Commit

Permalink
Add extra properties support
Browse files Browse the repository at this point in the history
  • Loading branch information
zigarn committed Feb 16, 2014
1 parent 8bbc810 commit 4ae9fbd
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/main/java/org/apache/catalina/startup/CatalinaProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ private static void loadProperties() {
properties=new Properties();
}

loadExtraProperties();

// Register the properties as system properties
Enumeration enumeration = properties.propertyNames();
while (enumeration.hasMoreElements()) {
Expand Down Expand Up @@ -169,4 +171,79 @@ private static String getConfigUrl() {
}


/**
* Load extra properties files.
*/
private static void loadExtraProperties() {
String extraPropertiesURLs = properties.getProperty("catalina.extra.properties.urls");
if (extraPropertiesURLs != null) {
for (String extraPropertiesURL : extraPropertiesURLs.split(",")) {
InputStream is = null;
try {
String configUrl = replace(extraPropertiesURL.trim());
if (configUrl != null) {
is = (new URL(configUrl)).openStream();
}
} catch (Throwable t) {
// Ignore
}
if (is != null) {
try {
properties.load(is);
is.close();
} catch (Throwable t) {
log.warn("Unable to load extra properties " + extraPropertiesURL);
}
} else {
log.warn("Unable to find extra properties " + extraPropertiesURL);
}
}
}
}


/**
* System property replacement in the given string.
*
* @param str The original string
* @return the modified string
*/
private static String replace(String str) {
// Copied from Bootstrap.replace() in 7.0.50
String result = str;
int pos_start = str.indexOf("${");
if (pos_start >= 0) {
StringBuilder builder = new StringBuilder();
int pos_end = -1;
while (pos_start >= 0) {
builder.append(str, pos_end + 1, pos_start);
pos_end = str.indexOf('}', pos_start + 2);
if (pos_end < 0) {
pos_end = pos_start - 1;
break;
}
String propName = str.substring(pos_start + 2, pos_end);
String replacement;
if (propName.length() == 0) {
replacement = null;
} else if ("catalina.home".equals(propName)) {
replacement = Bootstrap.getCatalinaHome();
} else if ("catalina.base".equals(propName)) {
replacement = Bootstrap.getCatalinaBase();
} else {
replacement = System.getProperty(propName);
}
if (replacement != null) {
builder.append(replacement);
} else {
builder.append(str, pos_start, pos_end + 1);
}
pos_start = str.indexOf("${", pos_end + 1);
}
builder.append(str, pos_end + 1, str.length());
result = builder.toString();
}
return result;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.apache.catalina.startup;

import static org.junit.Assert.assertEquals;

import java.net.URL;

import org.junit.BeforeClass;
import org.junit.Test;

public class CatalinaPropertiesTest {

@BeforeClass
public static void setUpClass() {
URL url = CatalinaPropertiesTest.class.getClassLoader().getResource("org/apache/catalina/startup/");
System.setProperty("catalina.home", url.getFile());
}

@Test
public void loadProperties() {
assertEquals("Catalina extra properties 1 not loaded", "true", CatalinaProperties.getProperty("catalina.extra.1.loaded"));
assertEquals("Catalina extra properties 2 not loaded", "true", CatalinaProperties.getProperty("catalina.extra.2.loaded"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
catalina.extra.1.loaded=true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
catalina.extra.2.loaded=true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
catalina.extra.properties.urls=file://${catalina.home}/catalina.extra.1.properties,file://${catalina.home}/catalina.extra.2.properties

0 comments on commit 4ae9fbd

Please sign in to comment.