1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2006 by ELCA Informatique SA, Av. de la Harpe 22-24,
5 * 1000 Lausanne, Switzerland, http://www.elca.ch
6 *
7 * EL4J is published under the GNU Lesser General Public License (LGPL)
8 * Version 2.1. See http://www.gnu.org/licenses/
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * For alternative licensing, please contact info@elca.ch
16 */
17 package ch.elca.el4j.util.logging.appenders;
18
19 import java.io.File;
20 import java.io.IOException;
21
22 import org.springframework.util.StringUtils;
23
24 /**
25 *
26 * This provides methods to check and construct a valid
27 * logfile path. Setting an absolute log file path that works in all
28 * environments is typically hard. This Appender-wrapper creates such
29 * an absolute log file path.
30 *
31 *
32 * @svnLink $Revision: 3884 $;$Date: 2009-08-04 15:48:31 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/util/logging/appenders/SmartFileLibrary.java $
33 *
34 * @author Rashid Waraich (RWA)
35 */
36 public class SmartFileLibrary {
37
38 /**
39 * Hide the default constructor as this is a utility class.
40 */
41 protected SmartFileLibrary() { }
42
43 /**
44 * This method tries to create a valid logfile path from the given input.
45 * @param fileName The fileName.
46 * @return A valid filePath.
47 * @throws IOException Throws IOException, if any input invalid.
48 */
49 public static String createSmartLogPath(String fileName)
50 throws IOException {
51
52 String finalFileName = fileName;
53
54 // if fileName is empty throw IOException
55 if (!StringUtils.hasText(finalFileName)) {
56 throw new IOException("The log-fileName can not be empty!");
57 }
58
59 File logFile = new File(finalFileName);
60
61 // if the fileName does not contain an absolute path,
62 // we need to assign a directory to that file.
63 if (!logFile.isAbsolute()) {
64 String logDir;
65 logDir = System.getProperty("el4j.log.dir");
66
67 // if the 'el4j.log.dir' system property exist, try
68 // to use it as the directory else try to use the tempdir
69 // of JRE
70 if (logDir != null) {
71 finalFileName = constructPath(logDir , finalFileName);
72 } else {
73 logDir = System.getProperty("java.io.tmpdir");
74 finalFileName = constructPath(logDir , finalFileName);
75 }
76 }
77 return finalFileName;
78 }
79
80 /**
81 * Tries to construct a valid filePath, for the given fileName
82 * and direcotry.
83 * @param directoryPath The directoryPath.
84 * @param fileName The fileName.
85 * @return An absolute filePath.
86 * @throws IOException
87 */
88 private static String constructPath(String directoryPath, String fileName)
89 throws IOException {
90
91 // if directoryPath is empty throw IOException
92 if (!StringUtils.hasText(directoryPath)) {
93 throw new IOException("The directoryPath can not be empty!");
94 }
95
96 // if fileName is empty throw IOException
97 if (!StringUtils.hasText(fileName)) {
98 throw new IOException("The fileName can not be empty!");
99 }
100
101 String d = directoryPath.replace('\\', '/');
102 String f = extractRelativePath(fileName.replace('\\', '/'));
103
104 File dFile = new File(d);
105 // if the directory does not exist, throw IOException
106 if (!dFile.isDirectory()) {
107 throw new IOException("The directory ["
108 + directoryPath + "] does not exist!");
109 }
110
111 // if directoryPath does not already contain
112 // any pathSeperator at the end, add a pathSeperator
113 if (directoryPath.endsWith("/")) {
114 return d + f;
115 } else {
116 return d + '/' + f;
117 }
118 }
119
120 /**
121 * Trim fileName and remove fileSeperator symbols
122 * at the begining of the fileName and check,
123 * if the resulting fileName is not empty.
124 * The resulted fileName is given back.
125 * @param fileName The input filename.
126 * @return A valid filename.
127 * @throws IOException Throws IOException, if fileName is not valid.
128 */
129 private static String extractRelativePath(String fileName)
130 throws IOException {
131 String result = fileName;
132
133 // remove all pathSeperator symbols infont of the
134 // fileName/relative-path
135 while (result.startsWith("/")) {
136 result = result.substring(1 , result.length());
137 }
138
139 result = result.trim();
140
141 // if the fileName only contained whitespace characters and
142 // pathSeperator symbols, then the fileName is not valid...
143 if (!StringUtils.hasLength(result)) {
144 throw new IOException("The fileName is not valid!");
145 }
146
147 return result;
148 }
149 }