net.sf.ehcache.config.DiskStoreConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ehcache-core Show documentation
Show all versions of ehcache-core Show documentation
This is the ehcache core module. Pair it with other modules for added functionality.
/**
* Copyright 2003-2010 Terracotta, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.sf.ehcache.config;
import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A class to represent DiskStore configuration
* e.g.
*
* @author Greg Luck
* @version $Id: DiskStoreConfiguration.java 2522 2010-06-25 14:40:22Z gbevin $
*/
public final class DiskStoreConfiguration {
private static final Logger LOG = LoggerFactory.getLogger(DiskStoreConfiguration.class.getName());
/**
* The path as specified in the config
*/
private String originalPath;
/**
* The path to the directory where .data and .index files will be created.
*/
private String path;
/**
* A constants class for environment variables used in disk store paths
*/
private static final class Env {
static final String USER_HOME = "user.home";
static final String USER_DIR = "user.dir";
static final String JAVA_IO_TMPDIR = "java.io.tmpdir";
static final String EHCACHE_DISK_STORE_DIR = "ehcache.disk.store.dir";
}
/**
* The diskStore path
*/
public final String getPath() {
return path;
}
/**
* The diskStore default path, which is the system environment variable
* availablen on all Java virtual machines java.io.tmpdir
*/
public static String getDefaultPath() {
return translatePath(Env.JAVA_IO_TMPDIR);
}
/**
* Builder method to set the disk store path, see {@link #setPath(String)}
*
* @return this configuration instance
*/
public final DiskStoreConfiguration path(final String path) {
setPath(path);
return this;
}
/**
* Translates and sets the path.
*
* @param path
* If the path contains a Java System Property token it is replaced by
* its value in the running VM. Subdirectories can be specified below the property e.g. java.io.tmpdir/one.
* The following properties are translated:
*
* user.home
- User's home directory
* user.dir
- User's current working directory
* java.io.tmpdir
- Default temp file path
* ehcache.disk.store.di?r
- A system property you would normally specify on the command linecan specify
* with -DDefault temp file path e.g. java -Dehcache.disk.store.dir=/u01/myapp/diskdir ...
*
* Additional strings can be placed before and after tokens?
* e.g. java.io/tmpdir/caches
might become /tmp/caches
*/
public final void setPath(final String path) {
this.originalPath = path;
String translatedPath = translatePath(path);
this.path = translatedPath;
}
/**
* @return the originalPath
*/
public String getOriginalPath() {
return originalPath;
}
private static String translatePath(String path) {
String translatedPath = replaceToken(Env.USER_HOME, System.getProperty(Env.USER_HOME), path);
translatedPath = replaceToken(Env.USER_DIR, System.getProperty(Env.USER_DIR), translatedPath);
translatedPath = replaceToken(Env.JAVA_IO_TMPDIR, System.getProperty(Env.JAVA_IO_TMPDIR), translatedPath);
translatedPath = replaceToken(Env.EHCACHE_DISK_STORE_DIR, System.getProperty(Env.EHCACHE_DISK_STORE_DIR), translatedPath);
// Remove duplicate separators: Windows and Solaris
translatedPath = replaceToken(File.separator + File.separator, File.separator, translatedPath);
LOG.debug("Disk Store Path: " + translatedPath);
return translatedPath;
}
/**
* Replaces a token with replacement text.
*
* @param token
* @param replacement
* @param source
* @return the String with replacement text applied
*/
public static String replaceToken(final String token, final String replacement, final String source) {
int foundIndex = source.indexOf(token);
if (foundIndex == -1) {
return source;
} else {
String firstFragment = source.substring(0, foundIndex);
String lastFragment = source.substring(foundIndex + token.length(), source.length());
return new StringBuilder().append(firstFragment).append(replacement).append(lastFragment).toString();
}
}
}