001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.ui.apps.cacheadmin; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsProject; 032import org.opencms.file.CmsResource; 033import org.opencms.file.CmsResourceFilter; 034import org.opencms.loader.CmsImageLoader; 035import org.opencms.main.CmsException; 036import org.opencms.main.OpenCms; 037import org.opencms.ui.A_CmsUI; 038import org.opencms.util.CmsFileUtil; 039import org.opencms.util.CmsStringUtil; 040 041import java.io.File; 042import java.io.FilenameFilter; 043import java.util.ArrayList; 044import java.util.Collections; 045import java.util.HashMap; 046import java.util.List; 047import java.util.Map; 048 049/** 050 * Helper class for getting information about cached images.<p> 051 */ 052public class CmsImageCacheHolder { 053 054 /**Maps Cache-file name to VFS name. */ 055 private static Map<String, String> PATH_TO_VFS_NAME = new HashMap<String, String>(); 056 057 /**File path map. */ 058 private Map m_filePaths = new HashMap(); 059 060 /** Lengths map. */ 061 private Map m_lengths = new HashMap(); 062 063 /** Sizes map. */ 064 private Map m_sizes = new HashMap(); 065 066 /** Variations map. */ 067 private Map m_variations = new HashMap(); 068 069 /**Filter for files (and dictionaries). */ 070 private FilenameFilter m_filter; 071 072 /**Cms Object. */ 073 protected CmsObject m_clonedCms; 074 075 /** 076 * public constructor.<p> 077 * @param search 078 */ 079 public CmsImageCacheHolder(final String search) { 080 081 try { 082 m_clonedCms = getClonedCmsObject(A_CmsUI.getCmsObject()); 083 } catch (CmsException e) { 084 m_clonedCms = A_CmsUI.getCmsObject(); 085 } 086 m_filter = new FilenameFilter() { 087 088 public boolean accept(File dir, String name) { 089 090 String spatt = search.replace("*", ""); 091 if (new File(dir, name).isDirectory()) { 092 return true; 093 } 094 095 String fullPath = dir.getAbsolutePath() + "/" + name; 096 097 fullPath = fullPath.substring(CmsImageLoader.getImageRepositoryPath().length() - 1); 098 return getVFSName(m_clonedCms, fullPath).contains(spatt); 099 100 } 101 }; 102 if (!search.startsWith("*") & search.startsWith("/")) { 103 String root = getRootFromPattern(search); 104 if (root.length() > 1) { 105 readAllImagesAndVariations(root.substring(1).replace("*", "")); 106 } else { 107 readAllImagesAndVariations(""); 108 } 109 } else { 110 readAllImagesAndVariations(""); 111 } 112 113 } 114 115 /** 116 * Returns all cached images.<p> 117 * 118 * @return a list of root paths 119 */ 120 public List<String> getAllCachedImages() { 121 122 List<String> ret = new ArrayList<String>(m_variations.keySet()); 123 Collections.sort(ret); 124 return ret; 125 } 126 127 /** 128 * Get variations of resource.<p> 129 * 130 * @param resource to get variations for 131 * @return list of CmsVariationBean 132 */ 133 public List<CmsVariationBean> getVariations(String resource) { 134 135 List<String> ret = (List<String>)(m_filePaths.get(resource)); 136 List<CmsVariationBean> res = new ArrayList<CmsVariationBean>(); 137 if (ret == null) { 138 return new ArrayList<CmsVariationBean>(); 139 } 140 for (String r : ret) { 141 res.add(new CmsVariationBean(r)); 142 } 143 return res; 144 } 145 146 /** 147 * Get the amount of variations for resource.<p> 148 * 149 * @param resource to get variations for 150 * @return amount of variations 151 */ 152 public int getVariationsCount(String resource) { 153 154 return ((List<String>)m_variations.get(resource)).size(); 155 } 156 157 /** 158 * Get name of image in the VFS.<p> 159 * 160 * @param cms CmsObject 161 * @param oName Name of cached image file 162 * @return vfs resource name (root path) 163 */ 164 String getVFSName(CmsObject cms, String oName) { 165 166 oName = CmsStringUtil.substitute(oName, "\\", "/"); 167 if (!oName.startsWith("/")) { 168 oName = "/" + oName; 169 } 170 171 if (PATH_TO_VFS_NAME.containsKey(oName)) { 172 return PATH_TO_VFS_NAME.get(oName); 173 } 174 175 String imgName = oName; 176 CmsResource res = null; 177 boolean found = false; 178 while (!found) { 179 String path = CmsResource.getParentFolder(imgName); 180 String name = imgName.substring(path.length()); 181 String ext = CmsFileUtil.getExtension(imgName); 182 String nameWoExt = name.substring(0, name.length() - ext.length()); 183 int pos = nameWoExt.lastIndexOf("_"); 184 String newName = path; 185 found = (pos < 0); 186 if (!found) { 187 newName += nameWoExt.substring(0, pos); 188 } else { 189 newName += nameWoExt; 190 } 191 newName += ext; 192 try { 193 res = cms.readResource(newName, CmsResourceFilter.ALL); 194 found = true; 195 } catch (Exception e) { 196 // it could be a variation 197 } 198 imgName = newName; 199 } 200 201 if (res != null) { 202 PATH_TO_VFS_NAME.put(oName, res.getRootPath()); 203 return res.getRootPath(); 204 } 205 return ""; 206 } 207 208 /** 209 * Clones a CmsObject.<p> 210 * 211 * @param cms the CmsObject to be cloned. 212 * @return a clones CmsObject 213 * @throws CmsException if something goes wrong 214 */ 215 private CmsObject getClonedCmsObject(CmsObject cms) throws CmsException { 216 217 CmsObject clonedCms = OpenCms.initCmsObject(cms); 218 // only online images get caches 219 clonedCms.getRequestContext().setCurrentProject(clonedCms.readProject(CmsProject.ONLINE_PROJECT_ID)); 220 // paths are always root path 221 clonedCms.getRequestContext().setSiteRoot(""); 222 223 return clonedCms; 224 } 225 226 private String getRootFromPattern(String pattern) { 227 228 String res = pattern.substring(0, pattern.lastIndexOf("/")); 229 230 return res; 231 } 232 233 /** 234 * Fille the list m_variations and m_filePaths.<p> 235 */ 236 private void readAllImagesAndVariations(String root) { 237 238 File basedir = new File(CmsImageLoader.getImageRepositoryPath() + root); 239 visitImages(m_clonedCms, basedir); 240 m_variations = Collections.unmodifiableMap(m_variations); 241 m_sizes = Collections.unmodifiableMap(m_sizes); 242 m_lengths = Collections.unmodifiableMap(m_lengths); 243 244 } 245 246 /** 247 * Visits a single image.<p> 248 * 249 * @param cms CmsObject 250 * @param f a File to be read out 251 */ 252 private void visitImage(CmsObject cms, File f) { 253 254 f.length(); 255 String oName = f.getAbsolutePath().substring(CmsImageLoader.getImageRepositoryPath().length()); 256 oName = getVFSName(cms, oName); 257 258 List files = (List)m_filePaths.get(oName); 259 if (files == null) { 260 files = new ArrayList(); 261 m_filePaths.put(oName, files); 262 } 263 files.add(f.getAbsolutePath()); 264 265 List variations = (List)m_variations.get(oName); 266 if (variations == null) { 267 variations = new ArrayList(); 268 m_variations.put(oName, variations); 269 270 } 271 oName += " ("; 272 oName += f.length() + " Bytes)"; 273 variations.add(oName); 274 } 275 276 /** 277 * Visits all cached images in the given directory.<p> 278 * 279 * @param cms the cms context 280 * @param directory the directory to visit 281 */ 282 private void visitImages(CmsObject cms, File directory) { 283 284 if (!directory.canRead() || !directory.isDirectory()) { 285 return; 286 } 287 File[] files = directory.listFiles(m_filter); 288 for (int i = 0; i < files.length; i++) { 289 File f = files[i]; 290 if (f.isDirectory()) { 291 visitImages(cms, f); 292 continue; 293 } 294 visitImage(cms, f); 295 } 296 } 297}