KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > wrapper > common > RelatifEnvironmentPathGetter


1 /**
2  * Copyright (C) 2001-2003 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * 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  */

18 package org.objectweb.util.monolog.wrapper.common;
19
20 import org.objectweb.util.monolog.Monolog;
21
22 /**
23  *
24  * this abstract class provides with the way to define a file real path with
25  * the value of environment variables value.<br/>
26  * to do that, the path in the the log properties file must include some variable
27  * name those environment variable must be put in braket like <code>${MY_VARIABLE}</code>
28  * <p><b>Example:</b> <code>${TOMCAT_HOME}/logs/${APPLICATION_NAME}_log_file.log</code></p>
29  * @author franck Milleville fmillevi@yahoo.com
30  */

31 public abstract class RelatifEnvironmentPathGetter {
32     static final String JavaDoc START = "${";
33     static final String JavaDoc END = "}";
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 JavaDoc getRealPath(final String JavaDoc path) {
40         if (0>path.indexOf(START)) {
41             // no variable name in the path
42
return path;
43         }
44         
45         String JavaDoc 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 JavaDoc("Bad file path conformance"));
55                     return path;
56                 }
57                 final String JavaDoc name = path.substring(pathIndex + START.length(), endIndex);
58                 final String JavaDoc value = System.getProperty(name);
59                 if (null==value) {
60                     Monolog.error("Error during getting the path '"
61                             + path + "'", new Exception JavaDoc("Unknown environment variable '" + name + "'"));
62                     return path;
63                 }
64                 realPath += value;
65                 i = endIndex;
66                 pathIndex = path.indexOf(START, i);
67             } else {
68                 realPath += path.substring(i);
69                 i = path.length();
70             }
71         }
72         return realPath;
73     }
74 }
75
Popular Tags