View Javadoc
1   package org.apache.commons.jcs3.auxiliary.disk;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  
24  import org.apache.commons.jcs3.auxiliary.AbstractAuxiliaryCacheAttributes;
25  import org.apache.commons.jcs3.auxiliary.disk.behavior.IDiskCacheAttributes;
26  import org.apache.commons.jcs3.log.Log;
27  import org.apache.commons.jcs3.log.LogManager;
28  
29  /**
30   * This has common attributes that any conceivable disk cache would need.
31   */
32  public abstract class AbstractDiskCacheAttributes extends AbstractAuxiliaryCacheAttributes implements IDiskCacheAttributes
33  {
34      /** Don't change. */
35      private static final long serialVersionUID = 8306631920391711229L;
36  
37      /** The logger */
38      private static final Log log = LogManager.getLog(AbstractDiskCacheAttributes.class);
39  
40      /** path to disk */
41      private File diskPath;
42  
43      /** if this is false, we will not execute remove all */
44      private boolean allowRemoveAll = true;
45  
46      /** default to 5000 */
47      private int maxPurgatorySize = MAX_PURGATORY_SIZE_DEFAULT;
48  
49      /** Default amount of time to allow for key persistence on shutdown */
50      private static final int DEFAULT_shutdownSpoolTimeLimit = 60;
51  
52      /**
53       * This default determines how long the shutdown will wait for the key spool and data defrag to
54       * finish.
55       */
56      private int shutdownSpoolTimeLimit = DEFAULT_shutdownSpoolTimeLimit;
57  
58      /** Type of disk limit: SIZE or COUNT */
59      private DiskLimitType diskLimitType = DiskLimitType.COUNT;
60  
61      /**
62       * Sets the diskPath attribute of the DiskCacheAttributes object
63       * <p>
64       *
65       * @param path
66       *            The new diskPath value
67       */
68      @Override
69      public void setDiskPath(final String path)
70      {
71          setDiskPath(new File(path));
72      }
73  
74      /**
75       * Sets the diskPath attribute of the DiskCacheAttributes object
76       * <p>
77       *
78       * @param diskPath
79       *            The new diskPath value
80       */
81      public void setDiskPath(final File diskPath)
82      {
83          this.diskPath = diskPath;
84          boolean result = this.diskPath.isDirectory();
85  
86          if (!result)
87          {
88              result = this.diskPath.mkdirs();
89          }
90          if (!result)
91          {
92              log.error("Failed to create directory {0}", diskPath);
93          }
94      }
95  
96      /**
97       * Gets the diskPath attribute of the attributes object
98       * <p>
99       *
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(final 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(final 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(final 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         final 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(final DiskLimitType diskLimitType)
203     {
204         this.diskLimitType = diskLimitType;
205     }
206 
207     @Override
208     public void setDiskLimitTypeName(final 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 }