1 /**
2 * Copyright 2003-2007 Greg Luck
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package net.sf.ehcache.config;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import java.io.File;
23
24 /**
25 * A class to represent DiskStore configuration
26 * e.g. <diskStore path="java.io.tmpdir" />
27 * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
28 * @version $Id: DiskStoreConfiguration.java 512 2007-07-10 09:18:45Z gregluck $
29 */
30 public final class DiskStoreConfiguration {
31 private static final Log LOG = LogFactory.getLog(DiskStoreConfiguration.class.getName());
32
33 private String path;
34
35 /**
36 * The diskStore path
37 */
38 public final String getPath() {
39 return path;
40 }
41
42 /**
43 * Translates and sets the path.
44 *
45 * @param path If the path contains a Java System Property it is replaced by
46 * its value in the running VM. Subdirectories can be specified below the property e.g. java.io.tmpdir/one The following properties are translated:
47 * <ul>
48 * <li><code>user.home</code> - User's home directory
49 * <li><code>user.dir</code> - User's current working directory
50 * <li><code>java.io.tmpdir</code> - Default temp file path
51 * </ul>
52 * e.g. <code>java.io/tmpdir/caches</code> might become <code>/tmp/caches</code>
53 */
54 public final void setPath(final String path) {
55 /** A constants class with method scope */
56 final class Env {
57 static final String USER_HOME = "user.home";
58 static final String USER_DIR = "user.dir";
59 static final String JAVA_IO_TMPDIR = "java.io.tmpdir";
60 }
61
62 String translatedPath = replaceToken(Env.USER_HOME, System.getProperty(Env.USER_HOME), path);
63 translatedPath = replaceToken(Env.USER_DIR, System.getProperty(Env.USER_DIR), translatedPath);
64 translatedPath = replaceToken(Env.JAVA_IO_TMPDIR, System.getProperty(Env.JAVA_IO_TMPDIR), translatedPath);
65 String separator = File.separator;
66 //Remove duplicate separators: Windows and Solaris
67 translatedPath = replaceToken(File.separator + File.separator, File.separator, translatedPath);
68
69
70 if (LOG.isDebugEnabled()) {
71 LOG.debug("Disk Store Path: " + translatedPath);
72 }
73 this.path = translatedPath;
74 }
75
76 /**
77 * Replaces a token with replacement text.
78 * @param token
79 * @param replacement
80 * @param source
81 * @return the String with replacement text applied
82 */
83 public static String replaceToken(final String token, final String replacement, final String source) {
84 int foundIndex = source.indexOf(token);
85 if (foundIndex == -1) {
86 return source;
87 } else {
88 String firstFragment = source.substring(0, foundIndex);
89 String lastFragment = source.substring(foundIndex + token.length(), source.length());
90 return new StringBuffer()
91 .append(firstFragment)
92 .append(replacement)
93 .append(lastFragment)
94 .toString();
95 }
96 }
97
98 }