source: josm/trunk/src/org/openstreetmap/josm/data/cache/CacheEntryAttributes.java@ 12620

Last change on this file since 12620 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

  • Property svn:eol-style set to native
File size: 5.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.cache;
3
4import java.util.Arrays;
5import java.util.Collections;
6import java.util.HashSet;
7import java.util.Map;
8import java.util.Map.Entry;
9import java.util.Set;
10import java.util.concurrent.ConcurrentHashMap;
11
12import org.apache.commons.jcs.engine.ElementAttributes;
13import org.openstreetmap.josm.tools.Logging;
14
15/**
16 * Class that contains attributes for JCS cache entries. Parameters are used to properly handle HTTP caching,
17 * and metadata structures, that should be stored together with the cache entry
18 *
19 * @author Wiktor Niesiobędzki
20 * @since 8168
21 */
22public class CacheEntryAttributes extends ElementAttributes {
23 private static final long serialVersionUID = 1L; //version
24 private final Map<String, String> attrs = new ConcurrentHashMap<>(RESERVED_KEYS.size());
25 private static final String NO_TILE_AT_ZOOM = "noTileAtZoom";
26 private static final String ETAG = "Etag";
27 private static final String LAST_MODIFICATION = "lastModification";
28 private static final String EXPIRATION_TIME = "expirationTime";
29 private static final String HTTP_RESPONSE_CODE = "httpResponceCode";
30 private static final String ERROR_MESSAGE = "errorMessage";
31 // this contains all of the above
32 private static final Set<String> RESERVED_KEYS = new HashSet<>(Arrays.asList(
33 NO_TILE_AT_ZOOM,
34 ETAG,
35 LAST_MODIFICATION,
36 EXPIRATION_TIME,
37 HTTP_RESPONSE_CODE,
38 ERROR_MESSAGE
39 ));
40
41 /**
42 * Constructs a new {@code CacheEntryAttributes}.
43 */
44 public CacheEntryAttributes() {
45 super();
46 attrs.put(NO_TILE_AT_ZOOM, "false");
47 attrs.put(LAST_MODIFICATION, "0");
48 attrs.put(EXPIRATION_TIME, "0");
49 attrs.put(HTTP_RESPONSE_CODE, "200");
50 }
51
52 /**
53 * @return if the entry is marked as "no tile at this zoom level"
54 */
55 public boolean isNoTileAtZoom() {
56 return Boolean.toString(true).equals(attrs.get(NO_TILE_AT_ZOOM));
57 }
58
59 /**
60 * Sets the marker for "no tile at this zoom level"
61 * @param noTileAtZoom true if this entry is "no tile at this zoom level"
62 */
63 public void setNoTileAtZoom(boolean noTileAtZoom) {
64 attrs.put(NO_TILE_AT_ZOOM, Boolean.toString(noTileAtZoom));
65 }
66
67 /**
68 * @return ETag header value, that was returned for this entry.
69 */
70 public String getEtag() {
71 return attrs.get(ETAG);
72 }
73
74 /**
75 * Sets the ETag header that was set with this entry
76 * @param etag Etag header
77 */
78 public void setEtag(String etag) {
79 if (etag != null) {
80 attrs.put(ETAG, etag);
81 }
82 }
83
84 /**
85 * Utility for conversion from String to int, with default to 0, in case of any errors
86 *
87 * @param key - integer as string
88 * @return int value of the string
89 */
90 private long getLongAttr(String key) {
91 String val = attrs.get(key);
92 if (val == null) {
93 attrs.put(key, "0");
94 return 0;
95 }
96 try {
97 return Long.parseLong(val);
98 } catch (NumberFormatException e) {
99 attrs.put(key, "0");
100 return 0;
101 }
102 }
103
104 /**
105 * @return last modification of the object in cache in milliseconds from Epoch
106 */
107 public long getLastModification() {
108 return getLongAttr(LAST_MODIFICATION);
109 }
110
111 /**
112 * sets last modification of the object in cache
113 *
114 * @param lastModification time in format of milliseconds from Epoch
115 */
116 public void setLastModification(long lastModification) {
117 attrs.put(LAST_MODIFICATION, Long.toString(lastModification));
118 }
119
120 /**
121 * @return when the object expires in milliseconds from Epoch
122 */
123 public long getExpirationTime() {
124 return getLongAttr(EXPIRATION_TIME);
125 }
126
127 /**
128 * sets expiration time for the object in cache
129 *
130 * @param expirationTime in format of milliseconds from epoch
131 */
132 public void setExpirationTime(long expirationTime) {
133 attrs.put(EXPIRATION_TIME, Long.toString(expirationTime));
134 }
135
136 /**
137 * Sets the HTTP response code that was sent with the cache entry
138 *
139 * @param responseCode http status code
140 * @since 8389
141 */
142 public void setResponseCode(int responseCode) {
143 attrs.put(HTTP_RESPONSE_CODE, Integer.toString(responseCode));
144 }
145
146 /**
147 * @return http status code
148 * @since 8389
149 */
150 public int getResponseCode() {
151 return (int) getLongAttr(HTTP_RESPONSE_CODE);
152 }
153
154 /**
155 * Sets the metadata about cache entry. As it stores all data together, with other attributes
156 * in common map, some keys might not be stored.
157 *
158 * @param map metadata to save
159 * @since 8418
160 */
161 public void setMetadata(Map<String, String> map) {
162 for (Entry<String, String> e: map.entrySet()) {
163 if (RESERVED_KEYS.contains(e.getKey())) {
164 Logging.info("Metadata key configuration contains key {0} which is reserved for internal use");
165 } else {
166 attrs.put(e.getKey(), e.getValue());
167 }
168 }
169 }
170
171 /**
172 * Returns an unmodifiable Map containing all metadata. Unmodifiable prevents access to metadata within attributes.
173 *
174 * @return unmodifiable Map with cache element metadata
175 * @since 8418
176 */
177 public Map<String, String> getMetadata() {
178 return Collections.unmodifiableMap(attrs);
179 }
180
181 /**
182 * @return error message returned while retrieving this object
183 */
184 public String getErrorMessage() {
185 return attrs.get(ERROR_MESSAGE);
186 }
187
188 /**
189 * @param error error related to this object
190 * @since 10469
191 */
192 public void setError(Exception error) {
193 setErrorMessage(Logging.getErrorMessage(error));
194 }
195
196 /**
197 * @param message error message related to this object
198 */
199 public void setErrorMessage(String message) {
200 attrs.put(ERROR_MESSAGE, message);
201 }
202}
Note: See TracBrowser for help on using the repository browser.