]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - osgi/runtime/org.argeo.osgi.boot/src/main/java/org/argeo/osgi/boot/internal/springutil/SystemPropertyUtils.java
Rename packages in OSGi boot
[lgpl/argeo-commons.git] / osgi / runtime / org.argeo.osgi.boot / src / main / java / org / argeo / osgi / boot / internal / springutil / SystemPropertyUtils.java
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 (file)
index 0000000..f81adc2
--- /dev/null
@@ -0,0 +1,87 @@
+/*\r
+ * Copyright 2002-2008 the original author or authors.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.argeo.osgi.boot.internal.springutil;\r
+\r
+/**\r
+ * Helper class for resolving placeholders in texts. Usually applied to file paths.\r
+ *\r
+ * <p>A text may contain <code>${...}</code> placeholders, to be resolved as\r
+ * system properties: e.g. <code>${user.dir}</code>.\r
+ *\r
+ * @author Juergen Hoeller\r
+ * @since 1.2.5\r
+ * @see #PLACEHOLDER_PREFIX\r
+ * @see #PLACEHOLDER_SUFFIX\r
+ * @see System#getProperty(String)\r
+ */\r
+public abstract class SystemPropertyUtils {\r
+\r
+       /** Prefix for system property placeholders: "${" */\r
+       public static final String PLACEHOLDER_PREFIX = "${";\r
+\r
+       /** Suffix for system property placeholders: "}" */\r
+       public static final String PLACEHOLDER_SUFFIX = "}";\r
+\r
+\r
+       /**\r
+        * Resolve ${...} placeholders in the given text,\r
+        * replacing them with corresponding system property values.\r
+        * @param text the String to resolve\r
+        * @return the resolved String\r
+        * @see #PLACEHOLDER_PREFIX\r
+        * @see #PLACEHOLDER_SUFFIX\r
+        */\r
+       public static String resolvePlaceholders(String text) {\r
+               StringBuffer buf = new StringBuffer(text);\r
+\r
+               int startIndex = buf.indexOf(PLACEHOLDER_PREFIX);\r
+               while (startIndex != -1) {\r
+                       int endIndex = buf.indexOf(PLACEHOLDER_SUFFIX, startIndex + PLACEHOLDER_PREFIX.length());\r
+                       if (endIndex != -1) {\r
+                               String placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex);\r
+                               int nextIndex = endIndex + PLACEHOLDER_SUFFIX.length();\r
+                               try {\r
+                                       String propVal = System.getProperty(placeholder);\r
+                                       if (propVal == null) {\r
+                                               // Fall back to searching the system environment.\r
+                                               //propVal = System.getenv(placeholder);// mbaudier - 2009-07-26\r
+                                               throw new Error("getenv no longer supported, use properties and -D instead: " + placeholder);\r
+                                       }\r
+                                       if (propVal != null) {\r
+                                               buf.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), propVal);\r
+                                               nextIndex = startIndex + propVal.length();\r
+                                       }\r
+                                       else {\r
+                                               System.err.println("Could not resolve placeholder '" + placeholder + "' in [" + text +\r
+                                                               "] as system property: neither system property nor environment variable found");\r
+                                       }\r
+                               }\r
+                               catch (Throwable ex) {\r
+                                       System.err.println("Could not resolve placeholder '" + placeholder + "' in [" + text +\r
+                                                       "] as system property: " + ex);\r
+                               }\r
+                               startIndex = buf.indexOf(PLACEHOLDER_PREFIX, nextIndex);\r
+                       }\r
+                       else {\r
+                               startIndex = -1;\r
+                       }\r
+               }\r
+\r
+               return buf.toString();\r
+       }\r
+\r
+}\r