~ubuntu-branches/ubuntu/oneiric/ehcache/oneiric

« back to all changes in this revision

Viewing changes to src/main/java/net/sf/ehcache/config/DiskStoreConfiguration.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2010-06-23 10:35:31 UTC
  • mfrom: (1.1.5 upstream) (2.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100623103531-ra0qdpmotoz6ygct
Tags: 2.1.0-1
Merge changes from Thierry's PPA and upload to Debian.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 *  Copyright 2003-2010 Terracotta, Inc.
 
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
 
 
20
import java.io.File;
 
21
 
 
22
import org.slf4j.Logger;
 
23
import org.slf4j.LoggerFactory;
 
24
 
 
25
/**
 
26
 * A class to represent DiskStore configuration
 
27
 * e.g. <diskStore path="java.io.tmpdir" />
 
28
 *
 
29
 * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
 
30
 * @version $Id: DiskStoreConfiguration.java 2154 2010-04-06 02:45:52Z cdennis $
 
31
 */
 
32
public final class DiskStoreConfiguration {
 
33
 
 
34
    private static final Logger LOG = LoggerFactory.getLogger(DiskStoreConfiguration.class.getName());
 
35
    
 
36
    /**
 
37
     * The path as specified in the config
 
38
     */
 
39
    private String originalPath;
 
40
 
 
41
 
 
42
    /**
 
43
     * The path to the directory where .data and .index files will be created.
 
44
     */
 
45
    private String path;
 
46
 
 
47
 
 
48
    /**
 
49
     * A constants class for environment variables used in disk store paths
 
50
     */
 
51
    private static final class Env {
 
52
 
 
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";
 
57
    }
 
58
 
 
59
    /**
 
60
     * The diskStore path
 
61
     */
 
62
    public final String getPath() {
 
63
        return path;
 
64
    }
 
65
 
 
66
    /**
 
67
     * The diskStore default path, which is the system environment variable
 
68
     * availablen on all Java virtual machines <code>java.io.tmpdir</code>
 
69
     */
 
70
    public static String getDefaultPath() {
 
71
        return translatePath(Env.JAVA_IO_TMPDIR);
 
72
    }
 
73
 
 
74
    /**
 
75
     * Translates and sets the path.
 
76
     *
 
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:
 
80
     *             <ul>
 
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>
 
86
     *             </ul>
 
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>
 
89
     */
 
90
    public final void setPath(final String path) {
 
91
        this.originalPath = path;
 
92
        String translatedPath = translatePath(path);
 
93
        this.path = translatedPath;
 
94
    }
 
95
 
 
96
    /**
 
97
     * @return the originalPath
 
98
     */
 
99
    public String getOriginalPath() {
 
100
        return originalPath;
 
101
    }
 
102
 
 
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;
 
112
    }
 
113
 
 
114
    /**
 
115
     * Replaces a token with replacement text.
 
116
     *
 
117
     * @param token
 
118
     * @param replacement
 
119
     * @param source
 
120
     * @return the String with replacement text applied
 
121
     */
 
122
    public static String replaceToken(final String token, final String replacement, final String source) {
 
123
        int foundIndex = source.indexOf(token);
 
124
        if (foundIndex == -1) {
 
125
            return source;
 
126
        } else {
 
127
            String firstFragment = source.substring(0, foundIndex);
 
128
            String lastFragment = source.substring(foundIndex + token.length(), source.length());
 
129
            return new StringBuilder()
 
130
                    .append(firstFragment)
 
131
                    .append(replacement)
 
132
                    .append(lastFragment)
 
133
                    .toString();
 
134
        }
 
135
    }
 
136
 
 
137
}