2
* Copyright 2003-2010 Terracotta, Inc.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package net.sf.ehcache.config;
22
import org.slf4j.Logger;
23
import org.slf4j.LoggerFactory;
26
* A class to represent DiskStore configuration
27
* e.g. <diskStore path="java.io.tmpdir" />
29
* @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
30
* @version $Id: DiskStoreConfiguration.java 2154 2010-04-06 02:45:52Z cdennis $
32
public final class DiskStoreConfiguration {
34
private static final Logger LOG = LoggerFactory.getLogger(DiskStoreConfiguration.class.getName());
37
* The path as specified in the config
39
private String originalPath;
43
* The path to the directory where .data and .index files will be created.
49
* A constants class for environment variables used in disk store paths
51
private static final class Env {
53
static final String USER_HOME = "user.home";
54
static final String USER_DIR = "user.dir";
55
static final String JAVA_IO_TMPDIR = "java.io.tmpdir";
56
static final String EHCACHE_DISK_STORE_DIR = "ehcache.disk.store.dir";
62
public final String getPath() {
67
* The diskStore default path, which is the system environment variable
68
* availablen on all Java virtual machines <code>java.io.tmpdir</code>
70
public static String getDefaultPath() {
71
return translatePath(Env.JAVA_IO_TMPDIR);
75
* Translates and sets the path.
77
* @param path If the path contains a Java System Property token it is replaced by
78
* its value in the running VM. Subdirectories can be specified below the property e.g. java.io.tmpdir/one.
79
* The following properties are translated:
81
* <li><code>user.home</code> - User's home directory
82
* <li><code>user.dir</code> - User's current working directory
83
* <li><code>java.io.tmpdir</code> - Default temp file path
84
* <li><code>ehcache.disk.store.di?r</code> - A system property you would normally specify on the command linecan specify with -DDefault temp file path
85
* e.g. <code>java -Dehcache.disk.store.dir=/u01/myapp/diskdir ...</code>
87
* Additional strings can be placed before and after tokens?
88
* e.g. <code>java.io/tmpdir/caches</code> might become <code>/tmp/caches</code>
90
public final void setPath(final String path) {
91
this.originalPath = path;
92
String translatedPath = translatePath(path);
93
this.path = translatedPath;
97
* @return the originalPath
99
public String getOriginalPath() {
103
private static String translatePath(String path) {
104
String translatedPath = replaceToken(Env.USER_HOME, System.getProperty(Env.USER_HOME), path);
105
translatedPath = replaceToken(Env.USER_DIR, System.getProperty(Env.USER_DIR), translatedPath);
106
translatedPath = replaceToken(Env.JAVA_IO_TMPDIR, System.getProperty(Env.JAVA_IO_TMPDIR), translatedPath);
107
translatedPath = replaceToken(Env.EHCACHE_DISK_STORE_DIR, System.getProperty(Env.EHCACHE_DISK_STORE_DIR), translatedPath);
108
//Remove duplicate separators: Windows and Solaris
109
translatedPath = replaceToken(File.separator + File.separator, File.separator, translatedPath);
110
LOG.debug("Disk Store Path: " + translatedPath);
111
return translatedPath;
115
* Replaces a token with replacement text.
120
* @return the String with replacement text applied
122
public static String replaceToken(final String token, final String replacement, final String source) {
123
int foundIndex = source.indexOf(token);
124
if (foundIndex == -1) {
127
String firstFragment = source.substring(0, foundIndex);
128
String lastFragment = source.substring(foundIndex + token.length(), source.length());
129
return new StringBuilder()
130
.append(firstFragment)
132
.append(lastFragment)