diff --git a/framework/base/build.xml b/framework/base/build.xml
index e00655434a..3ff9440a18 100644
--- a/framework/base/build.xml
+++ b/framework/base/build.xml
@@ -56,6 +56,7 @@ under the License.
+
diff --git a/framework/base/src/org/ofbiz/base/concurrent/GeneratedResult.java b/framework/base/src/org/ofbiz/base/concurrent/GeneratedResult.java
new file mode 100644
index 0000000000..925cd5ec6a
--- /dev/null
+++ b/framework/base/src/org/ofbiz/base/concurrent/GeneratedResult.java
@@ -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 {
+ public final long lastModifiedTime;
+ public final T object;
+
+ public GeneratedResult(long lastModifiedTime, T object) {
+ this.lastModifiedTime = lastModifiedTime;
+ this.object = object;
+ }
+}
diff --git a/framework/base/src/org/ofbiz/base/concurrent/TTLCachedObject.java b/framework/base/src/org/ofbiz/base/concurrent/TTLCachedObject.java
new file mode 100644
index 0000000000..ae0330db70
--- /dev/null
+++ b/framework/base/src/org/ofbiz/base/concurrent/TTLCachedObject.java
@@ -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 extends TTLObject {
+ 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 result = generate(old, serial);
+ lastModifiedTime = result.lastModifiedTime;
+ return result.object;
+ } else {
+ lastModifiedTime = NOT_EXISTANT_TIMESTAMP;
+ return getInitial();
+ }
+ }
+ return old;
+ }
+
+ protected abstract GeneratedResult generate(T old, int serial) throws Exception;
+ protected abstract long getTimestamp(T old) throws Exception;
+}
diff --git a/framework/base/src/org/ofbiz/base/concurrent/test/TTLCachedObjectTest.java b/framework/base/src/org/ofbiz/base/concurrent/test/TTLCachedObjectTest.java
new file mode 100644
index 0000000000..c28c7f96bc
--- /dev/null
+++ b/framework/base/src/org/ofbiz/base/concurrent/test/TTLCachedObjectTest.java
@@ -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 {
+ protected long dataTimestamp = NOT_EXISTANT_TIMESTAMP;
+ protected String data = "first";
+
+ protected long getTimestamp(String old) throws Exception {
+ return dataTimestamp;
+ }
+
+ protected GeneratedResult generate(String old, int serial) throws Exception {
+ return new GeneratedResult(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());
+ }
+}