comparison artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java @ 84:72e2dd4feb31

Added the time to live of an artifact to the CallContext. artifacts/trunk@828 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 25 Mar 2010 17:32:54 +0000
parents 8447467cef86
children b2e0cb83631c
comparison
equal deleted inserted replaced
83:8c4638abd518 84:72e2dd4feb31
56 public final class PersistentArtifact 56 public final class PersistentArtifact
57 extends Id 57 extends Id
58 { 58 {
59 private Artifact artifact; 59 private Artifact artifact;
60 private ArtifactSerializer serializer; 60 private ArtifactSerializer serializer;
61 private Long ttl;
61 62
62 public PersistentArtifact( 63 public PersistentArtifact(
63 Artifact artifact, 64 Artifact artifact,
64 ArtifactSerializer serializer, 65 ArtifactSerializer serializer,
66 Long ttl,
65 int id 67 int id
66 ) { 68 ) {
67 super(id); 69 super(id);
68 this.artifact = artifact; 70 this.artifact = artifact;
69 this.serializer = serializer; 71 this.serializer = serializer;
72 this.ttl = ttl;
70 } 73 }
71 74
72 public Artifact getArtifact() { 75 public Artifact getArtifact() {
73 return artifact; 76 return artifact;
74 } 77 }
75 78
76 public ArtifactSerializer getSerializer() { 79 public ArtifactSerializer getSerializer() {
77 return serializer; 80 return serializer;
81 }
82
83 public Long getTTL() {
84 return ttl;
78 } 85 }
79 86
80 public void store() { 87 public void store() {
81 if (logger.isDebugEnabled()) { 88 if (logger.isDebugEnabled()) {
82 logger.debug("storing artifact id = " + getId()); 89 logger.debug("storing artifact id = " + getId());
120 throws Exception 127 throws Exception
121 { 128 {
122 return new PersistentArtifact( 129 return new PersistentArtifact(
123 artifact, 130 artifact,
124 factory.getSerializer(), 131 factory.getSerializer(),
132 ttl,
125 insertDatabase(artifact, factory, ttl)); 133 insertDatabase(artifact, factory, ttl));
126 } 134 }
127 135
128 public PersistentArtifact storeOrReplace( 136 public PersistentArtifact storeOrReplace(
129 Artifact artifact, 137 Artifact artifact,
133 throws Exception 141 throws Exception
134 { 142 {
135 return new PersistentArtifact( 143 return new PersistentArtifact(
136 artifact, 144 artifact,
137 factory.getSerializer(), 145 factory.getSerializer(),
146 ttl,
138 storeOrReplaceDatabase(artifact, factory, ttl)); 147 storeOrReplaceDatabase(artifact, factory, ttl));
139 } 148 }
140 149
141 public interface ArtifactLoader { 150 public interface ArtifactLoader {
142 151
143 Object load(ArtifactFactory factory, byte [] bytes, int id); 152 Object load(ArtifactFactory factory, Long ttl, byte [] bytes, int id);
144 153
145 } // interface ArtifactLoader 154 } // interface ArtifactLoader
146 155
147 public PersistentArtifact getArtifact(String identifer) { 156 public PersistentArtifact getArtifact(String identifer) {
148 157
150 identifer, 159 identifer,
151 new ArtifactLoader() { 160 new ArtifactLoader() {
152 161
153 public Object load( 162 public Object load(
154 ArtifactFactory factory, 163 ArtifactFactory factory,
164 Long ttl,
155 byte [] bytes, 165 byte [] bytes,
156 int id 166 int id
157 ) { 167 ) {
158 ArtifactSerializer serializer = factory.getSerializer(); 168 ArtifactSerializer serializer = factory.getSerializer();
159 169
160 Artifact artifact = serializer.fromBytes(bytes); 170 Artifact artifact = serializer.fromBytes(bytes);
161 171
162 return artifact == null 172 return artifact == null
163 ? null 173 ? null
164 : new PersistentArtifact(artifact, serializer, id); 174 : new PersistentArtifact(artifact, serializer, ttl, id);
165 } 175 }
166 }); 176 });
167 } 177 }
168 178
169 public Object loadArtifact(String identifer, ArtifactLoader loader) { 179 public Object loadArtifact(String identifer, ArtifactLoader loader) {
186 196
187 if (!load_result.next()) { 197 if (!load_result.next()) {
188 return null; 198 return null;
189 } 199 }
190 200
191 int id = load_result.getInt(1); 201 int id = load_result.getInt(1);
192 long ttl = load_result.getLong(3); 202 long ttlX = load_result.getLong(3);
193 203
194 if (!load_result.wasNull()) { // real time to life 204 Long ttl = load_result.wasNull() ? null : Long.valueOf(ttlX);
205
206 if (ttl != null) { // real time to life
195 long last_access = load_result.getTimestamp(2).getTime(); 207 long last_access = load_result.getTimestamp(2).getTime();
196 if (last_access + ttl < System.currentTimeMillis()) { 208 if (last_access + ttlX < System.currentTimeMillis()) {
197 artifactOutdated(id); 209 artifactOutdated(id);
198 return null; 210 return null;
199 } 211 }
200 } 212 }
201 213
214 return null; 226 return null;
215 } 227 }
216 228
217 byte [] bytes = load_result.getBytes(5); 229 byte [] bytes = load_result.getBytes(5);
218 230
219 return loader.load(factory, bytes, id); 231 return loader.load(factory, ttl, bytes, id);
220 } 232 }
221 catch (SQLException sqle) { 233 catch (SQLException sqle) {
222 logger.error(sqle.getLocalizedMessage(), sqle); 234 logger.error(sqle.getLocalizedMessage(), sqle);
223 } 235 }
224 finally { 236 finally {

http://dive4elements.wald.intevation.org