Skip to content

Commit

Permalink
SYMMETRICDS-417
Browse files Browse the repository at this point in the history
  • Loading branch information
chenson42 committed Apr 23, 2011
1 parent 8a7c9a4 commit 6c47338
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
Expand Up @@ -32,6 +32,7 @@
import org.jumpmind.symmetric.model.OutgoingBatch;
import org.jumpmind.symmetric.model.Router;
import org.jumpmind.symmetric.service.ITriggerRouterService;
import org.jumpmind.symmetric.util.AppUtils;
import org.jumpmind.symmetric.util.CsvUtils;

/**
Expand Down Expand Up @@ -110,12 +111,7 @@ protected void writeTable(Data data, String routerId, Writer out, DataExtractorC
protected String getTargetName(String name) {
String catalogName = name == null ? "" : name;
if (StringUtils.isNotBlank(catalogName)) {
String externalId = parameterService.getExternalId();
try {
catalogName = String.format(catalogName, Long.parseLong(externalId));
} catch (NumberFormatException ex) {
catalogName = String.format(catalogName, externalId);
}
catalogName = AppUtils.replaceTokens(catalogName, parameterService.getReplacementValues());
}
return catalogName;
}
Expand Down
Expand Up @@ -82,7 +82,9 @@ public interface IParameterService {
/**
* Get the external id for this instance
*/
public String getExternalId();
public String getExternalId();

public Map<String,String> getReplacementValues();

/**
* Provide the url used to register at to get initial configuration
Expand Down
Expand Up @@ -303,6 +303,13 @@ public String getRegistrationUrl() {
return url;
}

public Map<String, String> getReplacementValues() {
Map<String,String> replacementValues = new HashMap<String, String>(2);
replacementValues.put("externalId", getExternalId());
replacementValues.put("nodeGroupId", getNodeGroupId());
return replacementValues;
}

class DatabaseParameterMapper implements RowMapper<DatabaseParameter> {
public DatabaseParameter mapRow(ResultSet rs, int rowNum) throws SQLException {
return new DatabaseParameter(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));
Expand Down
Expand Up @@ -26,6 +26,8 @@
import java.util.Iterator;
import java.util.Map;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.sql.DataSource;

Expand Down Expand Up @@ -55,7 +57,9 @@ public class AppUtils {
private static String serverId;

private static FastDateFormat timezoneFormatter = FastDateFormat
.getInstance("Z");
.getInstance("Z");

private static Pattern pattern = Pattern.compile("\\$\\((.+?)\\)");

/**
* Get a unique identifier that represents the JVM instance this server is
Expand Down Expand Up @@ -109,17 +113,36 @@ public static String replace(String prop, String replaceWith,
String sourceString) {
return StringUtils
.replace(sourceString, "$(" + prop + ")", replaceWith);
}

public static String replaceTokens(String text, Map<String, String> replacements) {
Matcher matcher = pattern.matcher(text);
StringBuffer buffer = new StringBuffer();
while (matcher.find()) {
String[] match = matcher.group(1).split("\\|");
String replacement = replacements.get(match[0]);
if (replacement != null) {
matcher.appendReplacement(buffer, "");
if (match.length == 2) {
replacement = formatString(match[1], replacement);
}
buffer.append(replacement);
}
}
matcher.appendTail(buffer);
return buffer.toString();
}

public static String formatString(String format, String arg) {
if (format.indexOf("d") >= 0 || format.indexOf("u") >= 0 || format.indexOf("i") >= 0) {
return String.format(format, Long.parseLong(arg));
} else if (format.indexOf("e") >= 0 || format.indexOf("f") >= 0) {
return String.format(format, Double.valueOf(arg));
} else {
return String.format(format, arg);
}
}

public static String replaceTokens(String original, Map<String, String> replacementTokens) {
if (replacementTokens != null) {
for (Object key : replacementTokens.keySet()) {
original = original.replaceAll(key.toString(), replacementTokens.get(key));
}
}
return original;
}

/**
* This method will return the timezone in RFC822 format. </p> The format
* ("-+HH:MM") has advantages over the older timezone codes ("AAA"). The
Expand Down

0 comments on commit 6c47338

Please sign in to comment.