1 31 32 package org.opencms.staticexport; 33 34 import org.opencms.db.CmsPublishedResource; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsResource; 37 import org.opencms.file.CmsResourceFilter; 38 import org.opencms.file.CmsVfsResourceNotFoundException; 39 import org.opencms.main.CmsException; 40 import org.opencms.main.CmsLog; 41 import org.opencms.main.OpenCms; 42 import org.opencms.report.I_CmsReport; 43 import org.opencms.security.CmsSecurityException; 44 import org.opencms.util.CmsFileUtil; 45 import org.opencms.util.CmsStringUtil; 46 import org.opencms.util.CmsUUID; 47 48 import java.io.File ; 49 import java.io.FileFilter ; 50 import java.util.ArrayList ; 51 import java.util.HashSet ; 52 import java.util.Iterator ; 53 import java.util.List ; 54 import java.util.Set ; 55 56 import org.apache.commons.logging.Log; 57 58 72 public abstract class A_CmsStaticExportHandler implements I_CmsStaticExportHandler { 73 74 77 private class PrefixFileFilter implements FileFilter { 78 79 80 private String m_baseExtension; 81 82 83 private String m_baseName; 84 85 90 public PrefixFileFilter(File baseFile) { 91 92 String fileName = baseFile.getName(); 93 m_baseExtension = CmsFileUtil.getFileExtension(fileName); 94 m_baseName = fileName + "_"; 95 } 96 97 103 public boolean accept(File f) { 104 105 return f.getName().startsWith(m_baseName) && f.getName().endsWith(m_baseExtension); 106 } 107 } 108 109 110 private static final Log LOG = CmsLog.getLog(A_CmsStaticExportHandler.class); 111 112 113 protected boolean m_busy; 114 115 118 public boolean isBusy() { 119 120 return m_busy; 121 } 122 123 126 public abstract void performEventPublishProject(CmsUUID publishHistoryId, I_CmsReport report); 127 128 134 public void scrubExportFolders(CmsUUID publishHistoryId) { 135 136 if (LOG.isDebugEnabled()) { 137 LOG.debug(Messages.get().getBundle().key(Messages.LOG_SCRUBBING_EXPORT_FOLDERS_1, publishHistoryId)); 138 } 139 140 Set scrubedFolders = new HashSet (); 141 Set scrubedFiles = new HashSet (); 142 143 CmsObject cms; 145 try { 146 cms = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserExport()); 147 } catch (CmsException e) { 148 LOG.error(Messages.get().getBundle().key(Messages.LOG_INIT_FAILED_0), e); 150 return; 151 } 152 153 List publishedResources; 154 try { 155 publishedResources = cms.readPublishedResources(publishHistoryId); 156 } catch (CmsException e) { 157 158 LOG.error( 159 Messages.get().getBundle().key(Messages.LOG_READING_CHANGED_RESOURCES_FAILED_1, publishHistoryId), 160 e); 161 return; 162 } 163 164 Iterator itPubRes = publishedResources.iterator(); 165 while (itPubRes.hasNext()) { 166 CmsPublishedResource res = (CmsPublishedResource)itPubRes.next(); 167 if (res.isUnChanged() || !res.isVfsResource()) { 168 continue; 170 } 171 172 String resPath = cms.getRequestContext().removeSiteRoot(res.getRootPath()); 174 List siblings = getSiblingsList(cms, resPath); 175 176 Iterator itSibs = siblings.iterator(); 177 while (itSibs.hasNext()) { 178 String vfsName = (String )itSibs.next(); 179 180 String rfsName = OpenCms.getStaticExportManager().getRfsName(cms, vfsName); 182 if (LOG.isDebugEnabled()) { 183 LOG.debug(Messages.get().getBundle().key(Messages.LOG_CHECKING_STATIC_EXPORT_2, vfsName, rfsName)); 184 } 185 if (rfsName.startsWith(OpenCms.getStaticExportManager().getRfsPrefix(vfsName)) 186 && (!scrubedFiles.contains(rfsName)) 187 && (!scrubedFolders.contains(CmsResource.getFolderPath(rfsName)))) { 188 189 if (res.isFolder()) { 190 if (res.isDeleted()) { 191 String exportFolderName = CmsFileUtil.normalizePath(OpenCms.getStaticExportManager().getExportPath( 192 vfsName) 193 + rfsName.substring(OpenCms.getStaticExportManager().getRfsPrefix(vfsName).length())); 194 try { 195 File exportFolder = new File (exportFolderName); 196 if (exportFolder.exists() && exportFolder.canWrite()) { 198 CmsFileUtil.purgeDirectory(exportFolder); 199 if (LOG.isInfoEnabled()) { 201 LOG.info(Messages.get().getBundle().key( 202 Messages.LOG_FOLDER_DELETED_1, 203 exportFolderName)); 204 } 205 scrubedFolders.add(rfsName); 206 continue; 207 } 208 } catch (Throwable t) { 209 if (LOG.isWarnEnabled()) { 211 LOG.warn(Messages.get().getBundle().key( 212 Messages.LOG_FOLDER_DELETION_FAILED_2, 213 vfsName, 214 exportFolderName)); 215 } 216 } 217 } 218 rfsName += CmsStaticExportManager.EXPORT_DEFAULT_FILE; 220 if (LOG.isDebugEnabled()) { 221 LOG.debug(Messages.get().getBundle().key(Messages.LOG_FOLDER_1, rfsName)); 222 } 223 224 } 225 226 String rfsExportFileName = CmsFileUtil.normalizePath(OpenCms.getStaticExportManager().getExportPath( 227 vfsName) 228 + rfsName.substring(OpenCms.getStaticExportManager().getRfsPrefix(vfsName).length())); 229 230 purgeFile(rfsExportFileName, vfsName); 231 scrubedFiles.add(rfsName); 232 233 List fileList = getRelatedFilesToPurge(rfsExportFileName, vfsName); 234 Iterator iter = fileList.iterator(); 235 while (iter.hasNext()) { 236 File file = (File )iter.next(); 237 purgeFile(file.getAbsolutePath(), vfsName); 238 rfsName = CmsFileUtil.normalizePath(OpenCms.getStaticExportManager().getRfsPrefix(vfsName) 239 + "/" 240 + file.getAbsolutePath().substring( 241 OpenCms.getStaticExportManager().getExportPath(vfsName).length())); 242 rfsName = CmsStringUtil.substitute(rfsName, new String (new char[] {File.separatorChar}), "/"); 243 scrubedFiles.add(rfsName); 244 } 245 } 246 247 } 248 } 249 } 250 251 259 protected abstract List getRelatedFilesToPurge(String exportFileName, String vfsName); 260 261 268 protected List getSiblingsList(CmsObject cms, String resPath) { 269 270 List siblings = new ArrayList (); 271 try { 272 List li = cms.readSiblings(resPath, CmsResourceFilter.ALL); 273 for (int i = 0, l = li.size(); i < l; i++) { 274 String vfsName = ((CmsResource)li.get(i)).getRootPath(); 275 siblings.add(vfsName); 276 } 277 } catch (CmsVfsResourceNotFoundException e) { 278 } catch (CmsSecurityException e) { 280 } catch (CmsException e) { 282 if (LOG.isWarnEnabled()) { 284 LOG.warn(Messages.get().getBundle().key(Messages.LOG_FETCHING_SIBLINGS_FAILED_1, resPath), e); 285 } 286 } 287 if (!siblings.contains(resPath)) { 288 siblings.add(resPath); 291 } 292 return siblings; 293 } 294 295 302 protected void purgeFile(String rfsFilePath, String vfsName) { 303 304 File rfsFile = new File (rfsFilePath); 305 306 deleteFile(rfsFile, vfsName); 308 309 File parent = rfsFile.getParentFile(); 312 if (parent != null) { 313 File [] paramVariants = parent.listFiles(new PrefixFileFilter(rfsFile)); 315 if (paramVariants != null) { 316 for (int v = 0; v < paramVariants.length; v++) { 317 deleteFile(paramVariants[v], vfsName); 318 } 319 } 320 } 321 } 322 323 329 private void deleteFile(File file, String vfsName) { 330 331 try { 332 if (file.exists() && file.canWrite()) { 333 file.delete(); 334 if (LOG.isInfoEnabled()) { 336 LOG.info(Messages.get().getBundle().key(Messages.LOG_FILE_DELETED_1, getRfsName(file, vfsName))); 337 } 338 } 339 } catch (Throwable t) { 340 if (LOG.isWarnEnabled()) { 342 LOG.warn( 343 Messages.get().getBundle().key(Messages.LOG_FILE_DELETION_FAILED_1, getRfsName(file, vfsName)), 344 t); 345 } 346 } 347 } 348 349 357 private String getRfsName(File file, String vfsName) { 358 359 String filePath = file.getAbsolutePath(); 360 String result = CmsFileUtil.normalizePath(OpenCms.getStaticExportManager().getRfsPrefix(vfsName) 361 + filePath.substring(OpenCms.getStaticExportManager().getExportPath(vfsName).length())); 362 return CmsStringUtil.substitute(result, new String (new char[] {File.separatorChar}), "/"); 363 } 364 } | Popular Tags |