001package org.apache.commons.jcs.auxiliary.disk;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023
024import org.apache.commons.jcs.auxiliary.AbstractAuxiliaryCacheAttributes;
025import org.apache.commons.jcs.auxiliary.disk.behavior.IDiskCacheAttributes;
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028
029/**
030 * This has common attributes that any conceivable disk cache would need.
031 */
032public abstract class AbstractDiskCacheAttributes extends AbstractAuxiliaryCacheAttributes implements IDiskCacheAttributes
033{
034    /** Don't change. */
035    private static final long serialVersionUID = 8306631920391711229L;
036
037    /** The logger */
038    private static final Log log = LogFactory.getLog(AbstractDiskCacheAttributes.class);
039
040    /** path to disk */
041    private File diskPath;
042
043    /** if this is false, we will not execute remove all */
044    private boolean allowRemoveAll = true;
045
046    /** default to 5000 */
047    private int maxPurgatorySize = MAX_PURGATORY_SIZE_DEFAULT;
048
049    /** Default amount of time to allow for key persistence on shutdown */
050    private static final int DEFAULT_shutdownSpoolTimeLimit = 60;
051
052    /**
053     * This default determines how long the shutdown will wait for the key spool and data defrag to
054     * finish.
055     */
056    private int shutdownSpoolTimeLimit = DEFAULT_shutdownSpoolTimeLimit;
057
058    /** Type of disk limit: SIZE or COUNT */
059    private DiskLimitType diskLimitType = DiskLimitType.COUNT;
060
061    /**
062     * Sets the diskPath attribute of the DiskCacheAttributes object
063     * <p>
064     *
065     * @param path
066     *            The new diskPath value
067     */
068    @Override
069    public void setDiskPath(String path)
070    {
071        setDiskPath(new File(path));
072    }
073
074    /**
075     * Sets the diskPath attribute of the DiskCacheAttributes object
076     * <p>
077     *
078     * @param diskPath
079     *            The new diskPath value
080     */
081    public void setDiskPath(File diskPath)
082    {
083        this.diskPath = diskPath;
084        boolean result = this.diskPath.isDirectory();
085
086        if (!result)
087        {
088            result = this.diskPath.mkdirs();
089        }
090        if (!result)
091        {
092            log.error("Failed to create directory " + diskPath);
093        }
094    }
095
096    /**
097     * Gets the diskPath attribute of the attributes object
098     * <p>
099     *
100     * @return The diskPath value
101     */
102    @Override
103    public File getDiskPath()
104    {
105        return this.diskPath;
106    }
107
108    /**
109     * Gets the maxKeySize attribute of the DiskCacheAttributes object
110     * <p>
111     *
112     * @return The maxPurgatorySize value
113     */
114    @Override
115    public int getMaxPurgatorySize()
116    {
117        return maxPurgatorySize;
118    }
119
120    /**
121     * Sets the maxPurgatorySize attribute of the DiskCacheAttributes object
122     * <p>
123     *
124     * @param maxPurgatorySize
125     *            The new maxPurgatorySize value
126     */
127    @Override
128    public void setMaxPurgatorySize(int maxPurgatorySize)
129    {
130        this.maxPurgatorySize = maxPurgatorySize;
131    }
132
133    /**
134     * Get the amount of time in seconds we will wait for elements to move to disk during shutdown
135     * for a particular region.
136     * <p>
137     *
138     * @return the time in seconds.
139     */
140    @Override
141    public int getShutdownSpoolTimeLimit()
142    {
143        return this.shutdownSpoolTimeLimit;
144    }
145
146    /**
147     * Sets the amount of time in seconds we will wait for elements to move to disk during shutdown
148     * for a particular region.
149     * <p>
150     * This is how long we give the event queue to empty.
151     * <p>
152     * The default is 60 seconds.
153     * <p>
154     *
155     * @param shutdownSpoolTimeLimit
156     *            the time in seconds
157     */
158    @Override
159    public void setShutdownSpoolTimeLimit(int shutdownSpoolTimeLimit)
160    {
161        this.shutdownSpoolTimeLimit = shutdownSpoolTimeLimit;
162    }
163
164    /**
165     * @param allowRemoveAll
166     *            The allowRemoveAll to set.
167     */
168    @Override
169    public void setAllowRemoveAll(boolean allowRemoveAll)
170    {
171        this.allowRemoveAll = allowRemoveAll;
172    }
173
174    /**
175     * @return Returns the allowRemoveAll.
176     */
177    @Override
178    public boolean isAllowRemoveAll()
179    {
180        return allowRemoveAll;
181    }
182
183    /**
184     * Includes the common attributes for a debug message.
185     * <p>
186     *
187     * @return String
188     */
189    @Override
190    public String toString()
191    {
192        StringBuilder str = new StringBuilder();
193        str.append("AbstractDiskCacheAttributes ");
194        str.append("\n diskPath = " + getDiskPath());
195        str.append("\n maxPurgatorySize   = " + getMaxPurgatorySize());
196        str.append("\n allowRemoveAll   = " + isAllowRemoveAll());
197        str.append("\n ShutdownSpoolTimeLimit   = " + getShutdownSpoolTimeLimit());
198        return str.toString();
199    }
200
201    @Override
202    public void setDiskLimitType(DiskLimitType diskLimitType)
203    {
204        this.diskLimitType = diskLimitType;
205    }
206
207    @Override
208    public void setDiskLimitTypeName(String diskLimitTypeName)
209    {
210        if (diskLimitTypeName != null)
211        {
212            diskLimitType = DiskLimitType.valueOf(diskLimitTypeName.trim());
213        }
214    }
215
216    @Override
217    public DiskLimitType getDiskLimitType()
218    {
219        return diskLimitType;
220    }
221}