1 /** 2 * Copyright 2003-2006 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 /** 23 * A class to represent DiskStore configuration 24 * e.g. <diskStore path="java.io.tmpdir" /> 25 * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a> 26 * @version $Id: DiskStoreConfiguration.java 52 2006-04-24 14:50:03Z gregluck $ 27 */ 28 public final class DiskStoreConfiguration { 29 private static final Log LOG = LogFactory.getLog(DiskStoreConfiguration.class.getName()); 30 31 private String path; 32 33 /** 34 * The diskStore path 35 */ 36 public final String getPath() { 37 return path; 38 } 39 40 /** 41 * Translates and sets the path. 42 * 43 * @param path If the path contains a Java System Property it is replaced by 44 * its value in the running VM. Subdirectories can be specified below the property e.g. java.io.tmpdir/one The following properties are translated: 45 * <ul> 46 * <li><code>user.home</code> - User's home directory 47 * <li><code>user.dir</code> - User's current working directory 48 * <li><code>java.io.tmpdir</code> - Default temp file path 49 * </ul> 50 * e.g. <code>java.io/tmpdir/caches</code> might become <code>/tmp/caches</code> 51 */ 52 public final void setPath(final String path) { 53 /** A constants class with method scope */ 54 final class Env { 55 static final String USER_HOME = "user.home"; 56 static final String USER_DIR = "user.dir"; 57 static final String JAVA_IO_TMPDIR = "java.io.tmpdir"; 58 } 59 60 String translatedPath = replaceToken(Env.USER_HOME, System.getProperty(Env.USER_HOME), path); 61 translatedPath = replaceToken(Env.USER_DIR, System.getProperty(Env.USER_DIR), translatedPath); 62 translatedPath = replaceToken(Env.JAVA_IO_TMPDIR, System.getProperty(Env.JAVA_IO_TMPDIR), translatedPath); 63 64 if (LOG.isDebugEnabled()) { 65 LOG.debug("Disk Store Path: " + translatedPath); 66 } 67 this.path = translatedPath; 68 } 69 70 private static String replaceToken(final String token, final String replacement, final String source) { 71 int foundIndex = source.indexOf(token); 72 if (foundIndex == -1) { 73 return source; 74 } else { 75 String firstFragment = source.substring(0, foundIndex); 76 String lastFragment = source.substring(foundIndex + token.length(), source.length()); 77 return new StringBuffer() 78 .append(firstFragment) 79 .append(replacement) 80 .append(lastFragment) 81 .toString(); 82 } 83 } 84 85 }