KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > scheduler > jobs > CmsImageCacheCleanupJob


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/scheduler/jobs/CmsImageCacheCleanupJob.java,v $
3  * Date : $Date: 2006/03/27 14:52:42 $
4  * Version: $Revision: 1.2 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.scheduler.jobs;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.loader.CmsImageLoader;
36 import org.opencms.main.CmsLog;
37 import org.opencms.scheduler.I_CmsScheduledJob;
38
39 import java.io.File JavaDoc;
40 import java.util.Map JavaDoc;
41
42 import org.apache.commons.logging.Log;
43
44 /**
45  * A schedulable OpenCms job that clear the image cache for the scaled images created by the <code>{@link org.opencms.loader.CmsImageLoader}</code>.<p>
46  *
47  * Job parameters:<p>
48  * <dl>
49  * <dt><code>maxage={time in hours}</code></dt>
50  * <dd>Specifies the maximum age (in hours) images can be unused before they are removed from the cache.
51  * Any image in the image cache folder that has a RFS date of last modification older than this time is considered
52  * expired and is therefore deleted.</dd>
53  * </dl>
54  *
55  * @author Alexander Kandzior
56  *
57  * @version $Revision: 1.2 $
58  *
59  * @since 6.2.0
60  */

61 public class CmsImageCacheCleanupJob implements I_CmsScheduledJob {
62
63     /** Unlock parameter. */
64     public static final String JavaDoc PARAM_MAXAGE = "maxage";
65
66     /** The log object for this class. */
67     private static final Log LOG = CmsLog.getLog(CmsImageCacheCleanupJob.class);
68
69     /**
70      * Removes all expired image cache entries from the RFS cache.<p>
71      *
72      * Empty directories are removed as well.<p>
73      *
74      * @param maxAge the maximum age of the image cache files in hours (or fractions of hours)
75      *
76      * @return the total number of deleted resources
77      */

78     public static int cleanImageCache(float maxAge) {
79
80         // calculate oldest possible date for the cache files
81
long expireDate = System.currentTimeMillis() - (long)(maxAge * 60f * 60f * 1000f);
82         File JavaDoc basedir = new File JavaDoc(CmsImageLoader.getImageRepositoryPath());
83         // perform the cache cleanup
84
return cleanImageCache(expireDate, basedir);
85     }
86
87     /**
88      * Removes all expired image cache entries from the given RFS directory, including recursion to subdirectories.<p>
89      *
90      * @param maxAge the maximum age of the image cache files
91      * @param directory the directory to remove the cache files in
92      *
93      * @return the total number of deleted resources
94      */

95     private static int cleanImageCache(long maxAge, File JavaDoc directory) {
96
97         int count = 0;
98         if (directory.canRead() && directory.isDirectory()) {
99             File JavaDoc[] files = directory.listFiles();
100             for (int i = 0; i < files.length; i++) {
101                 File JavaDoc f = files[i];
102                 if (f.isDirectory()) {
103                     count += cleanImageCache(maxAge, f);
104                 }
105                 if (f.canWrite()) {
106                     if (f.lastModified() < maxAge) {
107                         try {
108                             f.delete();
109                             count++;
110                         } catch (Exception JavaDoc e) {
111                             LOG.error(Messages.get().getBundle().key(
112                                 Messages.LOG_IMAGE_CACHE_UNABLE_TO_DELETE_1,
113                                 f.getAbsolutePath()));
114                         }
115                     }
116                 }
117             }
118             if (directory.listFiles().length <= 0) {
119                 try {
120                     directory.delete();
121                     count++;
122                 } catch (Exception JavaDoc e) {
123                     LOG.error(Messages.get().getBundle().key(
124                         Messages.LOG_IMAGE_CACHE_UNABLE_TO_DELETE_1,
125                         directory.getAbsolutePath()));
126                 }
127             }
128         }
129         return count;
130     }
131
132     /**
133      * @see org.opencms.scheduler.I_CmsScheduledJob#launch(CmsObject, Map)
134      */

135     public String JavaDoc launch(CmsObject cms, Map JavaDoc parameters) throws Exception JavaDoc {
136
137         if (!CmsImageLoader.isEnabled() || (CmsImageLoader.getImageRepositoryPath() == null)) {
138             // scaling functions are not available
139
return Messages.get().getBundle().key(Messages.LOG_IMAGE_SCALING_DISABLED_0);
140         }
141
142         String JavaDoc maxAgeStr = (String JavaDoc)parameters.get(PARAM_MAXAGE);
143         float maxAge;
144         try {
145             maxAge = Float.parseFloat(maxAgeStr);
146         } catch (NumberFormatException JavaDoc e) {
147             // in case of an error, use maxage of one week
148
maxAge = 24f * 7f;
149             LOG.error(Messages.get().getBundle().key(
150                 Messages.LOG_IMAGE_CACHE_BAD_MAXAGE_2,
151                 maxAgeStr,
152                 new Float JavaDoc(maxAge)));
153         }
154
155         // now perform the image cache cleanup
156
int count = cleanImageCache(maxAge);
157
158         return Messages.get().getBundle().key(Messages.LOG_IMAGE_CACHE_CLEANUP_COUNT_1, new Integer JavaDoc(count));
159     }
160 }
Popular Tags