01: /**
02: * Copyright (C) 2001-2003 France Telecom R&D
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */package org.objectweb.util.monolog.wrapper.common;
18:
19: import org.objectweb.util.monolog.Monolog;
20:
21: /**
22: *
23: * this abstract class provides with the way to define a file real path with
24: * the value of environment variables value.<br/>
25: * to do that, the path in the the log properties file must include some variable
26: * name those environment variable must be put in braket like <code>${MY_VARIABLE}</code>
27: * <p><b>Example:</b> <code>${TOMCAT_HOME}/logs/${APPLICATION_NAME}_log_file.log</code></p>
28: * @author franck Milleville fmillevi@yahoo.com
29: */
30: public abstract class RelatifEnvironmentPathGetter {
31: static final String START = "${";
32: static final String END = "}";
33:
34: /**
35: * This method replace environment variables name by its value (when any)
36: * @param path the file path including variable name
37: * @return the file path with environment variable value
38: */
39: public static String getRealPath(final String path) {
40: if (0 > path.indexOf(START)) {
41: // no variable name in the path
42: return path;
43: }
44:
45: String realPath = "";
46: int pathIndex = path.indexOf(START);
47: for (int i = 0; i < path.length(); i++) {
48: if (pathIndex >= i) {
49: realPath += path.substring(i, pathIndex);
50: i = pathIndex;
51: int endIndex = path.indexOf(END, pathIndex);
52: if (0 > endIndex) {
53: Monolog.error("Error during getting the path '"
54: + path + "'", new Exception(
55: "Bad file path conformance"));
56: return path;
57: }
58: final String name = path.substring(pathIndex
59: + START.length(), endIndex);
60: final String value = System.getProperty(name);
61: if (null == value) {
62: Monolog.error("Error during getting the path '"
63: + path + "'", new Exception(
64: "Unknown environment variable '" + name
65: + "'"));
66: return path;
67: }
68: realPath += value;
69: i = endIndex;
70: pathIndex = path.indexOf(START, i);
71: } else {
72: realPath += path.substring(i);
73: i = path.length();
74: }
75: }
76: return realPath;
77: }
78: }
|