001/* 002 * This library is part of OpenCms - 003 * The Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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 GmbH & Co. KG, please see the 018 * company website: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://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.loader.imagecache; 029 030import java.time.Instant; 031import java.util.ArrayList; 032import java.util.Collections; 033import java.util.HashSet; 034import java.util.List; 035import java.util.Objects; 036import java.util.Set; 037 038/** 039 * A backend-neutral image cache maintenance request.<p> 040 */ 041public final class CmsImageCacheMaintenanceRequest { 042 043 /** Maintenance operations. */ 044 public enum Operation { 045 046 /** Clears the complete cache. */ 047 CLEAR, 048 049 /** Deletes selected entries. */ 050 DELETE, 051 052 /** Renews selected entries. */ 053 RENEW 054 } 055 056 /** The selected entries. */ 057 private final List<CmsImageCacheEntry> m_entries; 058 059 /** The operation. */ 060 private final Operation m_operation; 061 062 /** The requested renewal timestamp. */ 063 private final Instant m_renewalTime; 064 065 /** 066 * Creates a request.<p> 067 * 068 * @param operation the operation 069 * @param entries the selected entries 070 * @param renewalTime the renewal timestamp 071 */ 072 private CmsImageCacheMaintenanceRequest( 073 Operation operation, 074 List<CmsImageCacheEntry> entries, 075 Instant renewalTime) { 076 077 m_operation = Objects.requireNonNull(operation, "operation"); 078 Objects.requireNonNull(entries, "entries"); 079 m_entries = Collections.unmodifiableList(new ArrayList<CmsImageCacheEntry>(entries)); 080 if ((operation == Operation.CLEAR) && !entries.isEmpty()) { 081 throw new IllegalArgumentException("A clear request must not contain cache entries."); 082 } 083 if ((operation == Operation.RENEW) && (renewalTime == null)) { 084 throw new IllegalArgumentException("A renewal request requires a renewal timestamp."); 085 } 086 Set<String> keys = new HashSet<String>(); 087 for (CmsImageCacheEntry entry : entries) { 088 if (!keys.add(entry.getKey())) { 089 throw new IllegalArgumentException("Duplicate image cache entry key: " + entry.getKey()); 090 } 091 } 092 m_renewalTime = renewalTime; 093 } 094 095 /** 096 * Creates a clear request.<p> 097 * 098 * @return the request 099 */ 100 public static CmsImageCacheMaintenanceRequest clear() { 101 102 return new CmsImageCacheMaintenanceRequest(Operation.CLEAR, Collections.emptyList(), null); 103 } 104 105 /** 106 * Creates a delete request.<p> 107 * 108 * @param entries the entries to delete 109 * @return the request 110 */ 111 public static CmsImageCacheMaintenanceRequest delete(List<CmsImageCacheEntry> entries) { 112 113 return new CmsImageCacheMaintenanceRequest(Operation.DELETE, entries, null); 114 } 115 116 /** 117 * Creates a renewal request.<p> 118 * 119 * @param entries the entries to renew 120 * @param renewalTime the timestamp to apply 121 * @return the request 122 */ 123 public static CmsImageCacheMaintenanceRequest renew(List<CmsImageCacheEntry> entries, Instant renewalTime) { 124 125 return new CmsImageCacheMaintenanceRequest(Operation.RENEW, entries, renewalTime); 126 } 127 128 /** 129 * Returns the selected entries.<p> 130 * 131 * @return the selected entries 132 */ 133 public List<CmsImageCacheEntry> getEntries() { 134 135 return m_entries; 136 } 137 138 /** 139 * Returns the operation.<p> 140 * 141 * @return the operation 142 */ 143 public Operation getOperation() { 144 145 return m_operation; 146 } 147 148 /** 149 * Returns the requested renewal timestamp.<p> 150 * 151 * @return the renewal timestamp, or {@code null} 152 */ 153 public Instant getRenewalTime() { 154 155 return m_renewalTime; 156 } 157}