X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=osgi%2Fruntime%2Forg.argeo.osgi.boot%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fosgi%2Fboot%2Finternal%2Fspringutil%2FSystemPropertyUtils.java;fp=osgi%2Fruntime%2Forg.argeo.osgi.boot%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fosgi%2Fboot%2Finternal%2Fspringutil%2FSystemPropertyUtils.java;h=f81adc25b67135f5c593e01edb29ab3406dff11a;hb=1b452a434924d7628c7b2eace3c5df8e62cdea1c;hp=0000000000000000000000000000000000000000;hpb=4432ffb9f9b4e4d23b2515bde9872910ff8e4e36;p=lgpl%2Fargeo-commons.git diff --git a/osgi/runtime/org.argeo.osgi.boot/src/main/java/org/argeo/osgi/boot/internal/springutil/SystemPropertyUtils.java b/osgi/runtime/org.argeo.osgi.boot/src/main/java/org/argeo/osgi/boot/internal/springutil/SystemPropertyUtils.java new file mode 100644 index 000000000..f81adc25b --- /dev/null +++ b/osgi/runtime/org.argeo.osgi.boot/src/main/java/org/argeo/osgi/boot/internal/springutil/SystemPropertyUtils.java @@ -0,0 +1,87 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.argeo.osgi.boot.internal.springutil; + +/** + * Helper class for resolving placeholders in texts. Usually applied to file paths. + * + *

A text may contain ${...} placeholders, to be resolved as + * system properties: e.g. ${user.dir}. + * + * @author Juergen Hoeller + * @since 1.2.5 + * @see #PLACEHOLDER_PREFIX + * @see #PLACEHOLDER_SUFFIX + * @see System#getProperty(String) + */ +public abstract class SystemPropertyUtils { + + /** Prefix for system property placeholders: "${" */ + public static final String PLACEHOLDER_PREFIX = "${"; + + /** Suffix for system property placeholders: "}" */ + public static final String PLACEHOLDER_SUFFIX = "}"; + + + /** + * Resolve ${...} placeholders in the given text, + * replacing them with corresponding system property values. + * @param text the String to resolve + * @return the resolved String + * @see #PLACEHOLDER_PREFIX + * @see #PLACEHOLDER_SUFFIX + */ + public static String resolvePlaceholders(String text) { + StringBuffer buf = new StringBuffer(text); + + int startIndex = buf.indexOf(PLACEHOLDER_PREFIX); + while (startIndex != -1) { + int endIndex = buf.indexOf(PLACEHOLDER_SUFFIX, startIndex + PLACEHOLDER_PREFIX.length()); + if (endIndex != -1) { + String placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex); + int nextIndex = endIndex + PLACEHOLDER_SUFFIX.length(); + try { + String propVal = System.getProperty(placeholder); + if (propVal == null) { + // Fall back to searching the system environment. + //propVal = System.getenv(placeholder);// mbaudier - 2009-07-26 + throw new Error("getenv no longer supported, use properties and -D instead: " + placeholder); + } + if (propVal != null) { + buf.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), propVal); + nextIndex = startIndex + propVal.length(); + } + else { + System.err.println("Could not resolve placeholder '" + placeholder + "' in [" + text + + "] as system property: neither system property nor environment variable found"); + } + } + catch (Throwable ex) { + System.err.println("Could not resolve placeholder '" + placeholder + "' in [" + text + + "] as system property: " + ex); + } + startIndex = buf.indexOf(PLACEHOLDER_PREFIX, nextIndex); + } + else { + startIndex = -1; + } + } + + return buf.toString(); + } + +}