Helper wrapper to protect against objects that have some kind of timestamp
checking. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@922758 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -56,6 +56,7 @@ under the License.
|
||||
<file name="org/ofbiz/base/util/collections/test/GenericMapTest.java"/>
|
||||
<file name="org/ofbiz/base/concurrent/test/SyncTTLObjectTest.java"/>
|
||||
<file name="org/ofbiz/base/concurrent/test/AsyncTTLObjectTest.java"/>
|
||||
<file name="org/ofbiz/base/concurrent/test/TTLCachedObjectTest.java"/>
|
||||
</filelist>
|
||||
|
||||
<target name="init">
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.base.concurrent;
|
||||
|
||||
import org.ofbiz.base.util.ObjectWrapper;
|
||||
|
||||
public final class GeneratedResult<T> {
|
||||
public final long lastModifiedTime;
|
||||
public final T object;
|
||||
|
||||
public GeneratedResult(long lastModifiedTime, T object) {
|
||||
this.lastModifiedTime = lastModifiedTime;
|
||||
this.object = object;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.base.concurrent;
|
||||
|
||||
public abstract class TTLCachedObject<T> extends TTLObject<T> {
|
||||
public static final long NOT_EXISTANT_TIMESTAMP = Long.MIN_VALUE;
|
||||
public static final long FORCE_REGEN = Long.MIN_VALUE + 1;
|
||||
|
||||
protected long lastModifiedTime = NOT_EXISTANT_TIMESTAMP;
|
||||
|
||||
public long getTimestamp() throws ObjectException {
|
||||
getObject();
|
||||
return lastModifiedTime;
|
||||
}
|
||||
|
||||
protected T load(T old, int serial) throws Exception {
|
||||
long timestamp = getTimestamp(old);
|
||||
if (lastModifiedTime != timestamp) {
|
||||
if (timestamp != NOT_EXISTANT_TIMESTAMP) {
|
||||
GeneratedResult<T> result = generate(old, serial);
|
||||
lastModifiedTime = result.lastModifiedTime;
|
||||
return result.object;
|
||||
} else {
|
||||
lastModifiedTime = NOT_EXISTANT_TIMESTAMP;
|
||||
return getInitial();
|
||||
}
|
||||
}
|
||||
return old;
|
||||
}
|
||||
|
||||
protected abstract GeneratedResult<T> generate(T old, int serial) throws Exception;
|
||||
protected abstract long getTimestamp(T old) throws Exception;
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.base.concurrent.test;
|
||||
|
||||
import org.ofbiz.base.concurrent.GeneratedResult;
|
||||
import org.ofbiz.base.concurrent.TTLCachedObject;
|
||||
import org.ofbiz.base.concurrent.TTLObject;
|
||||
|
||||
public class TTLCachedObjectTest extends TTLObjectTest {
|
||||
static {
|
||||
TTLObject.setDefaultTTLForClass(TTLCachedObjectTestTTLObject.class, 100);
|
||||
}
|
||||
|
||||
public TTLCachedObjectTest(String name) {
|
||||
super(name, true);
|
||||
}
|
||||
|
||||
protected final class TTLCachedObjectTestTTLObject extends TTLCachedObject<String> {
|
||||
protected long dataTimestamp = NOT_EXISTANT_TIMESTAMP;
|
||||
protected String data = "first";
|
||||
|
||||
protected long getTimestamp(String old) throws Exception {
|
||||
return dataTimestamp;
|
||||
}
|
||||
|
||||
protected GeneratedResult<String> generate(String old, int serial) throws Exception {
|
||||
return new GeneratedResult<String>(dataTimestamp, data);
|
||||
}
|
||||
}
|
||||
|
||||
public void testTTLCachedObject() throws Exception {
|
||||
TTLCachedObjectTestTTLObject object = new TTLCachedObjectTestTTLObject();
|
||||
assertNull("initial non-existant value", object.getObject());
|
||||
assertEquals("initial non-existant timestamp", TTLCachedObject.NOT_EXISTANT_TIMESTAMP, object.getTimestamp());
|
||||
object.dataTimestamp = 1;
|
||||
assertNull("initial no-refresh value", object.getObject());
|
||||
assertEquals("initial no-refresh timestamp", TTLCachedObject.NOT_EXISTANT_TIMESTAMP, object.getTimestamp());
|
||||
object.refresh();
|
||||
assertEquals("first value", "first", object.getObject());
|
||||
assertEquals("first timestamp", 1, object.getTimestamp());
|
||||
object.data = "second";
|
||||
object.refresh();
|
||||
assertEquals("not-modified value", "first", object.getObject());
|
||||
assertEquals("not-modified timestamp", 1, object.getTimestamp());
|
||||
object.dataTimestamp = 2;
|
||||
assertEquals("cached modified value", "first", object.getObject());
|
||||
assertEquals("cached modified timestamp", 1, object.getTimestamp());
|
||||
object.refresh();
|
||||
assertEquals("refresh second value", "second", object.getObject());
|
||||
assertEquals("refresh second timestamp", 2, object.getTimestamp());
|
||||
object.dataTimestamp = TTLCachedObject.NOT_EXISTANT_TIMESTAMP;
|
||||
object.refresh();
|
||||
assertNull("refresh non-existant value", object.getObject());
|
||||
assertEquals("refresh non-existant timestamp", TTLCachedObject.NOT_EXISTANT_TIMESTAMP, object.getTimestamp());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user