cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dkazimirc...@apache.org
Subject svn commit: r1147058 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/access/ main/java/org/apache/cayenne/configuration/server/ test/java/org/apache/cayenne/configuration/server/
Date Fri, 15 Jul 2011 09:37:54 GMT
Author: dkazimirchyk
Date: Fri Jul 15 09:37:53 2011
New Revision: 1147058

URL: http://svn.apache.org/viewvc?rev=1147058&view=rev
Log:
CAY-1586 New extension point: a strategy for retaining objects in the ObjectStore

added injectable ObjectMapRetainStrategy that can be configured via RuntimeProperties

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DefaultObjectMapRetainStrategy.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/ObjectMapRetainStrategy.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataContext.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/ObjectStore.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/DataContextFactory.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/ServerModule.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/server/DataContextFactoryTest.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataContext.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataContext.java?rev=1147058&r1=1147057&r2=1147058&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataContext.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataContext.java
Fri Jul 15 09:37:53 2011
@@ -43,6 +43,7 @@ import org.apache.cayenne.Persistent;
 import org.apache.cayenne.QueryResponse;
 import org.apache.cayenne.access.util.IteratedSelectObserver;
 import org.apache.cayenne.cache.NestedQueryCache;
+import org.apache.cayenne.configuration.server.DataContextFactory;
 import org.apache.cayenne.event.EventManager;
 import org.apache.cayenne.graph.ChildDiffLoader;
 import org.apache.cayenne.graph.CompoundDiff;
@@ -113,7 +114,9 @@ public class DataContext extends BaseCon
      * Creates and returns a new child ObjectContext.
      * 
      * @since 3.0
+     * @deprecated Since 3.1 replaced by {@link DataContextFactory#createContext(DataChannel)}
      */
+    @Deprecated
     public ObjectContext createChildContext() {
 
         // child ObjectStore should not have direct access to snapshot cache, so do not

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DefaultObjectMapRetainStrategy.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DefaultObjectMapRetainStrategy.java?rev=1147058&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DefaultObjectMapRetainStrategy.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DefaultObjectMapRetainStrategy.java
Fri Jul 15 09:37:53 2011
@@ -0,0 +1,55 @@
+/*****************************************************************
+ *   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.apache.cayenne.access;
+
+import java.util.Map;
+
+import org.apache.cayenne.Persistent;
+import org.apache.cayenne.configuration.RuntimeProperties;
+import org.apache.cayenne.di.Inject;
+import org.apache.commons.collections.map.AbstractReferenceMap;
+import org.apache.commons.collections.map.ReferenceMap;
+
+/**
+ * Default implementation of {@link ObjectMapRetainStrategy}.
+ * 
+ * @since 3.1
+ */
+public class DefaultObjectMapRetainStrategy implements ObjectMapRetainStrategy {
+    
+    protected RuntimeProperties runtimeProperties;
+    
+    public DefaultObjectMapRetainStrategy(@Inject RuntimeProperties runtimeProperties) {
+        this.runtimeProperties = runtimeProperties;
+    }
+
+    public Map<Object, Persistent> createObjectMap() {
+        String retainStrategy = runtimeProperties.get(MAP_RETAIN_STRATEGY_PROPERTY);
+        
+        if (SOFT_RETAIN_STRATEGY.equals(retainStrategy)) {
+            return new ReferenceMap(AbstractReferenceMap.HARD, AbstractReferenceMap.SOFT);
+        }
+        else if (HARD_RETAIN_STRATEGY.equals(retainStrategy)) {
+            return new ReferenceMap(AbstractReferenceMap.HARD, AbstractReferenceMap.HARD);
+        }
+        else {
+            return new ReferenceMap(AbstractReferenceMap.HARD, AbstractReferenceMap.WEAK);
+        }
+    }
+}

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/ObjectMapRetainStrategy.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/ObjectMapRetainStrategy.java?rev=1147058&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/ObjectMapRetainStrategy.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/ObjectMapRetainStrategy.java
Fri Jul 15 09:37:53 2011
@@ -0,0 +1,41 @@
+/*****************************************************************
+ *   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.apache.cayenne.access;
+
+import java.util.Map;
+
+import org.apache.cayenne.Persistent;
+
+/**
+ * A strategy for retaining objects in {@link ObjectStore}. May be used
+ * weak, soft or hard references. 
+ * 
+ * @since 3.1
+ */
+public interface ObjectMapRetainStrategy {
+    
+    static final String MAP_RETAIN_STRATEGY_PROPERTY = "org.apache.cayenne.context_object_retain_strategy";
+    
+    static final String WEAK_RETAIN_STRATEGY = "weak";
+    static final String SOFT_RETAIN_STRATEGY = "soft";
+    static final String HARD_RETAIN_STRATEGY = "hard";
+    
+    Map<Object, Persistent> createObjectMap();
+
+}

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/ObjectStore.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/ObjectStore.java?rev=1147058&r1=1147057&r2=1147058&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/ObjectStore.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/ObjectStore.java
Fri Jul 15 09:37:53 2011
@@ -106,12 +106,20 @@ public class ObjectStore implements Seri
      */
     protected DataContext context;
 
+    /**
+     * @deprecated since 3.1
+     */
+    @Deprecated
     public ObjectStore() {
         this(null);
     }
 
+    /**
+     * @deprecated since 3.1
+     */
+    @Deprecated
     public ObjectStore(DataRowStore dataRowCache) {
-        this(dataRowCache, null);
+        this(dataRowCache, createObjectMap());
     }
 
     /**
@@ -123,7 +131,12 @@ public class ObjectStore implements Seri
      */
     public ObjectStore(DataRowStore dataRowCache, Map<Object, Persistent> objectMap)
{
         setDataRowCache(dataRowCache);
-        this.objectMap = objectMap != null ? objectMap : ObjectStore.createObjectMap();
+        if (objectMap != null) {
+            this.objectMap = objectMap;
+        }
+        else {
+            throw new CayenneRuntimeException("Object map is null.");
+        }
         this.changes = new HashMap<Object, ObjectDiff>();
     }
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/DataContextFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/DataContextFactory.java?rev=1147058&r1=1147057&r2=1147058&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/DataContextFactory.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/DataContextFactory.java
Fri Jul 15 09:37:53 2011
@@ -24,6 +24,7 @@ import org.apache.cayenne.ObjectContext;
 import org.apache.cayenne.access.DataContext;
 import org.apache.cayenne.access.DataDomain;
 import org.apache.cayenne.access.DataRowStore;
+import org.apache.cayenne.access.ObjectMapRetainStrategy;
 import org.apache.cayenne.access.ObjectStore;
 import org.apache.cayenne.cache.QueryCache;
 import org.apache.cayenne.configuration.ObjectContextFactory;
@@ -45,6 +46,9 @@ public class DataContextFactory implemen
 
     @Inject
     protected Injector injector;
+    
+    @Inject
+    protected ObjectMapRetainStrategy retainStrategy;
 
     public ObjectContext createContext() {
         return createdFromDataDomain(dataDomain);
@@ -74,7 +78,8 @@ public class DataContextFactory implemen
                 dataDomain.getProperties(),
                 eventManager);
 
-        DataContext context = new DataContext(parent, new ObjectStore(snapshotCache));
+        DataContext context = new DataContext(parent, new ObjectStore(snapshotCache, 
+                retainStrategy.createObjectMap()));
         context.setValidatingObjectsOnCommit(dataDomain.isValidatingObjectsOnCommit());
         context.setQueryCache(injector.getInstance(Key.get(
                 QueryCache.class,
@@ -85,7 +90,7 @@ public class DataContextFactory implemen
     protected ObjectContext createFromDataContext(DataContext parent) {
         // child ObjectStore should not have direct access to snapshot cache, so do not
         // pass it in constructor.
-        ObjectStore objectStore = new ObjectStore();
+        ObjectStore objectStore = new ObjectStore(null, retainStrategy.createObjectMap());
 
         DataContext context = new DataContext(parent, objectStore);
 
@@ -106,12 +111,12 @@ public class DataContextFactory implemen
                 .getSharedSnapshotCache() : new DataRowStore(parent.getName(), parent
                 .getProperties(), eventManager);
 
-        DataContext context = new DataContext(parent, new ObjectStore(snapshotCache));
+        DataContext context = new DataContext(parent, new ObjectStore(snapshotCache, 
+                retainStrategy.createObjectMap()));
         context.setValidatingObjectsOnCommit(parent.isValidatingObjectsOnCommit());
         context.setQueryCache(injector.getInstance(Key.get(
                 QueryCache.class,
                 BaseContext.QUERY_CACHE_INJECTION_KEY)));
         return context;
     }
-
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/ServerModule.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/ServerModule.java?rev=1147058&r1=1147057&r2=1147058&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/ServerModule.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/ServerModule.java
Fri Jul 15 09:37:53 2011
@@ -21,6 +21,9 @@ package org.apache.cayenne.configuration
 import org.apache.cayenne.BaseContext;
 import org.apache.cayenne.DataChannel;
 import org.apache.cayenne.access.DataDomain;
+import org.apache.cayenne.access.DefaultObjectMapRetainStrategy;
+import org.apache.cayenne.access.ObjectMapRetainStrategy;
+import org.apache.cayenne.access.QueryLogger;
 import org.apache.cayenne.access.dbsync.SchemaUpdateStrategy;
 import org.apache.cayenne.access.dbsync.SkipSchemaUpdateStrategy;
 import org.apache.cayenne.access.jdbc.BatchQueryBuilderFactory;
@@ -97,7 +100,9 @@ public class ServerModule implements Mod
         // configure empty global stack properties
         binder.bindMap(DefaultRuntimeProperties.PROPERTIES_MAP);
 
-        binder.bind(JdbcEventLogger.class).to(CommonsJdbcEventLogger.class);
+        CommonsJdbcEventLogger logger = new CommonsJdbcEventLogger();
+        QueryLogger.setLogger(logger);
+        binder.bind(JdbcEventLogger.class).toInstance(logger);
 
         // configure known DbAdapter detectors in reverse order of popularity. Users can
         // add their own to install custom adapters automatically
@@ -180,5 +185,8 @@ public class ServerModule implements Mod
 
         binder.bind(BatchQueryBuilderFactory.class).to(
                 DefaultBatchQueryBuilderFactory.class);
+        
+        // a default ObjectMapRetainStrategy used to create objects map for ObjectStore
+        binder.bind(ObjectMapRetainStrategy.class).to(DefaultObjectMapRetainStrategy.class);
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/server/DataContextFactoryTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/server/DataContextFactoryTest.java?rev=1147058&r1=1147057&r2=1147058&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/server/DataContextFactoryTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/server/DataContextFactoryTest.java
Fri Jul 15 09:37:53 2011
@@ -18,13 +18,19 @@
  ****************************************************************/
 package org.apache.cayenne.configuration.server;
 
+import java.util.Collections;
+
 import junit.framework.TestCase;
 
 import org.apache.cayenne.BaseContext;
 import org.apache.cayenne.access.DataContext;
 import org.apache.cayenne.access.DataDomain;
+import org.apache.cayenne.access.DefaultObjectMapRetainStrategy;
+import org.apache.cayenne.access.ObjectMapRetainStrategy;
 import org.apache.cayenne.cache.MapQueryCache;
 import org.apache.cayenne.cache.QueryCache;
+import org.apache.cayenne.configuration.DefaultRuntimeProperties;
+import org.apache.cayenne.configuration.RuntimeProperties;
 import org.apache.cayenne.di.Binder;
 import org.apache.cayenne.di.DIBootstrap;
 import org.apache.cayenne.di.Injector;
@@ -51,6 +57,9 @@ public class DataContextFactoryTest exte
                 binder.bind(DataDomain.class).toInstance(domain);
                 binder.bind(EventManager.class).toInstance(eventManager);
                 binder.bind(QueryCache.class).toInstance(new MapQueryCache(5));
+                binder.bind(RuntimeProperties.class).toInstance(
+                        new DefaultRuntimeProperties(Collections.EMPTY_MAP));
+                binder.bind(ObjectMapRetainStrategy.class).to(DefaultObjectMapRetainStrategy.class);
                 binder
                         .bind(
                                 Key.get(
@@ -85,6 +94,9 @@ public class DataContextFactoryTest exte
                 binder.bind(DataDomain.class).toInstance(domain);
                 binder.bind(EventManager.class).toInstance(eventManager);
                 binder.bind(QueryCache.class).toInstance(new MapQueryCache(5));
+                binder.bind(RuntimeProperties.class).toInstance(
+                        new DefaultRuntimeProperties(Collections.EMPTY_MAP));
+                binder.bind(ObjectMapRetainStrategy.class).to(DefaultObjectMapRetainStrategy.class);
                 binder
                         .bind(
                                 Key.get(



Mime
View raw message