You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2011/01/30 02:48:38 UTC

svn commit: r1065165 - in /cayenne/main/trunk: framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/ framework/cayenne-jdk1.5-unpublished/src/main/java/org/a...

Author: aadamchik
Date: Sun Jan 30 01:48:37 2011
New Revision: 1065165

URL: http://svn.apache.org/viewvc?rev=1065165&view=rev
Log:
CAY-1445 Initialize QueryCache via dependency injection

removal of QueryCacheFactory

Removed:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/QueryCacheFactory.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/cache/MockQueryCacheFactory.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/BaseContext.java
    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/DataDomain.java
    cayenne/main/trunk/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/DataDomainView.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/BaseContext.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/BaseContext.java?rev=1065165&r1=1065164&r2=1065165&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/BaseContext.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/BaseContext.java Sun Jan 30 01:48:37 2011
@@ -24,8 +24,8 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import org.apache.cayenne.cache.MapQueryCache;
 import org.apache.cayenne.cache.QueryCache;
+import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.event.EventManager;
 import org.apache.cayenne.exp.ValueInjector;
 import org.apache.cayenne.graph.CompoundDiff;
@@ -53,6 +53,8 @@ import org.apache.cayenne.util.ObjectCon
  */
 public abstract class BaseContext implements ObjectContext, DataChannel {
 
+    protected static final String QUERY_CACHE_INJECTION_KEY = "local";
+
     /**
      * A holder of a ObjectContext bound to the current thread.
      * 
@@ -88,10 +90,12 @@ public abstract class BaseContext implem
         threadObjectContext.set(context);
     }
 
-    // if we are to pass the context around, channel should be left alone and
-    // reinjected later if needed
+    // transient variables that should be reinitialized on deserialization from the
+    // registry
     protected transient DataChannel channel;
-    protected QueryCache queryCache;
+
+    @Inject(QUERY_CACHE_INJECTION_KEY)
+    protected transient QueryCache queryCache;
 
     /**
      * Graph action that handles property changes
@@ -258,20 +262,7 @@ public abstract class BaseContext implem
 
     public abstract Collection<?> uncommittedObjects();
 
-    /**
-     * Returns {@link QueryCache}, creating it on the fly if needed.
-     */
     public QueryCache getQueryCache() {
-        if (queryCache == null) {
-            synchronized (this) {
-                if (queryCache == null) {
-                    // TODO: andrus, 7/27/2006 - figure out the factory stuff like we have
-                    // in DataContext
-                    queryCache = new MapQueryCache();
-                }
-            }
-        }
-
         return queryCache;
     }
 

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=1065165&r1=1065164&r2=1065165&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 Sun Jan 30 01:48:37 2011
@@ -43,9 +43,9 @@ import org.apache.cayenne.Persistent;
 import org.apache.cayenne.QueryResponse;
 import org.apache.cayenne.access.util.IteratedSelectObserver;
 import org.apache.cayenne.cache.QueryCache;
-import org.apache.cayenne.cache.QueryCacheFactory;
 import org.apache.cayenne.configuration.CayenneRuntime;
 import org.apache.cayenne.di.Injector;
+import org.apache.cayenne.di.Key;
 import org.apache.cayenne.event.EventManager;
 import org.apache.cayenne.graph.ChildDiffLoader;
 import org.apache.cayenne.graph.CompoundDiff;
@@ -116,29 +116,6 @@ public class DataContext extends BaseCon
     }
 
     /**
-     * Returns {@link QueryCache} used by this DataContext, creating it on the fly if
-     * needed. Uses parent DataDomain {@link QueryCacheFactory} to initialize the cache
-     * for the first time, passing parent DataDomain's properties.
-     * 
-     * @since 3.0
-     */
-    @Override
-    public QueryCache getQueryCache() {
-        if (queryCache == null) {
-            synchronized (this) {
-                if (queryCache == null) {
-
-                    DataDomain domain = getParentDataDomain();
-                    queryCache = domain.getQueryCacheFactory().getQueryCache(
-                            domain.getProperties());
-                }
-            }
-        }
-
-        return queryCache;
-    }
-
-    /**
      * Creates and returns a new child ObjectContext.
      * 
      * @since 3.0
@@ -1147,6 +1124,9 @@ public class DataContext extends BaseCon
     private void readObject(ObjectInputStream in) throws IOException,
             ClassNotFoundException {
 
+        // TODO: most of this should be in the superclass, especially the code connecting
+        // super transient ivars
+
         // 1. read non-transient properties
         in.defaultReadObject();
 
@@ -1163,6 +1143,9 @@ public class DataContext extends BaseCon
         Injector injector = CayenneRuntime.getThreadInjector();
         if (injector != null) {
             setChannel(injector.getInstance(DataChannel.class));
+            setQueryCache(injector.getInstance(Key.get(
+                    QueryCache.class,
+                    QUERY_CACHE_INJECTION_KEY)));
         }
         else {
             // throw?

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataDomain.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataDomain.java?rev=1065165&r1=1065164&r2=1065165&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataDomain.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DataDomain.java Sun Jan 30 01:48:37 2011
@@ -36,11 +36,10 @@ import org.apache.cayenne.DataChannelSyn
 import org.apache.cayenne.ObjectContext;
 import org.apache.cayenne.QueryResponse;
 import org.apache.cayenne.access.jdbc.BatchQueryBuilderFactory;
-import org.apache.cayenne.cache.MapQueryCacheFactory;
 import org.apache.cayenne.cache.QueryCache;
-import org.apache.cayenne.cache.QueryCacheFactory;
 import org.apache.cayenne.configuration.ObjectContextFactory;
 import org.apache.cayenne.di.Inject;
+import org.apache.cayenne.di.Provider;
 import org.apache.cayenne.event.EventManager;
 import org.apache.cayenne.graph.CompoundDiff;
 import org.apache.cayenne.graph.GraphDiff;
@@ -71,13 +70,6 @@ public class DataDomain implements Query
     public static final boolean USING_EXTERNAL_TRANSACTIONS_DEFAULT = false;
 
     /**
-     * Defines a property name for storing optional {@link QueryCacheFactory}.
-     * 
-     * @since 3.0
-     */
-    public static final String QUERY_CACHE_FACTORY_PROPERTY = "cayenne.DataDomain.queryCacheFactory";
-
-    /**
      * @since 3.1
      */
     @Inject
@@ -104,7 +96,6 @@ public class DataDomain implements Query
     protected EntityResolver entityResolver;
     protected DataRowStore sharedSnapshotCache;
     protected TransactionDelegate transactionDelegate;
-    protected QueryCacheFactory queryCacheFactory;
     protected String name;
 
     // these are initialized from properties...
@@ -123,9 +114,14 @@ public class DataDomain implements Query
     protected EntitySorter entitySorter;
 
     /**
-     * @since 3.0
+     * An injected provider of {@link QueryCache}. Note that QueryCache is not injected
+     * directly to ensure lazy initialization (e.g. it may never be used and should not be
+     * instantiated).
+     * 
+     * @since 3.1
      */
-    protected QueryCache queryCache;
+    @Inject
+    protected Provider<QueryCache> queryCacheProvider;
 
     protected boolean stopped;
 
@@ -217,8 +213,6 @@ public class DataDomain implements Query
         String usingExternalTransactions = localMap
                 .get(USING_EXTERNAL_TRANSACTIONS_PROPERTY);
 
-        String queryCacheFactoryName = localMap.get(QUERY_CACHE_FACTORY_PROPERTY);
-
         // init ivars from properties
         this.sharedCacheEnabled = (sharedCacheEnabled != null) ? "true"
                 .equalsIgnoreCase(sharedCacheEnabled) : SHARED_CACHE_ENABLED_DEFAULT;
@@ -228,41 +222,6 @@ public class DataDomain implements Query
         this.usingExternalTransactions = (usingExternalTransactions != null)
                 ? "true".equalsIgnoreCase(usingExternalTransactions)
                 : USING_EXTERNAL_TRANSACTIONS_DEFAULT;
-
-        if (queryCacheFactoryName != null) {
-            queryCacheFactory = createInstance(
-                    queryCacheFactoryName,
-                    QueryCacheFactory.class);
-        }
-        else {
-            queryCacheFactory = null;
-        }
-    }
-
-    private <T> T createInstance(String className, Class<T> implementedInterface) {
-        Class<?> aClass;
-        try {
-            aClass = Class.forName(className, true, Thread
-                    .currentThread()
-                    .getContextClassLoader());
-        }
-        catch (Exception e) {
-            throw new CayenneRuntimeException("Error loading '" + className + "'", e);
-        }
-
-        if (!implementedInterface.isAssignableFrom(aClass)) {
-            throw new CayenneRuntimeException("Failed to load '"
-                    + className
-                    + "' - it is expected to implement "
-                    + implementedInterface);
-        }
-
-        try {
-            return (T) aClass.newInstance();
-        }
-        catch (Exception e) {
-            throw new CayenneRuntimeException("Error instantiating " + className, e);
-        }
     }
 
     /**
@@ -895,47 +854,12 @@ public class DataDomain implements Query
     }
 
     /**
-     * Returns a non-null {@link QueryCacheFactory}.
-     * 
-     * @since 3.0
-     */
-    public QueryCacheFactory getQueryCacheFactory() {
-        return queryCacheFactory != null ? queryCacheFactory : new MapQueryCacheFactory();
-    }
-
-    /**
-     * @since 3.0
-     */
-    public void setQueryCacheFactory(QueryCacheFactory queryCacheFactory) {
-        this.queryCacheFactory = queryCacheFactory;
-    }
-
-    /**
-     * Returns shared {@link QueryCache} used by this DataDomain, creating it on the fly
-     * if needed. Uses factory obtained via {@link #getQueryCacheFactory()} to initialize
-     * the cache for the first time. This domain properties are passed to the
-     * {@link QueryCacheFactory#getQueryCache(Map)} method.
+     * Returns shared {@link QueryCache} used by this DataDomain.
      * 
      * @since 3.0
      */
     public QueryCache getQueryCache() {
-
-        if (queryCache == null) {
-            synchronized (this) {
-                if (queryCache == null) {
-                    queryCache = getQueryCacheFactory().getQueryCache(getProperties());
-                }
-            }
-        }
-
-        return queryCache;
-    }
-
-    /**
-     * @since 3.0
-     */
-    QueryCache getQueryCacheInternal() {
-        return queryCache;
+        return queryCacheProvider.get();
     }
 
     /**

Modified: cayenne/main/trunk/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/DataDomainView.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/DataDomainView.java?rev=1065165&r1=1065164&r2=1065165&view=diff
==============================================================================
--- cayenne/main/trunk/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/DataDomainView.java (original)
+++ cayenne/main/trunk/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/DataDomainView.java Sun Jan 30 01:48:37 2011
@@ -25,17 +25,13 @@ import java.awt.event.ActionListener;
 import java.util.Map;
 import java.util.prefs.Preferences;
 
-import javax.swing.DefaultComboBoxModel;
 import javax.swing.JButton;
 import javax.swing.JCheckBox;
-import javax.swing.JComboBox;
 import javax.swing.JPanel;
 import javax.swing.JTextField;
 
 import org.apache.cayenne.access.DataDomain;
 import org.apache.cayenne.access.DataRowStore;
-import org.apache.cayenne.cache.MapQueryCacheFactory;
-import org.apache.cayenne.cache.OSQueryCacheFactory;
 import org.apache.cayenne.configuration.DataChannelDescriptor;
 import org.apache.cayenne.configuration.event.DomainEvent;
 import org.apache.cayenne.modeler.Application;
@@ -57,17 +53,12 @@ import com.jgoodies.forms.layout.FormLay
  */
 public class DataDomainView extends JPanel implements DomainDisplayListener {
 
-    final static String[] QUERY_CACHE_FACTORIES = new String[] {
-            MapQueryCacheFactory.class.getName(), OSQueryCacheFactory.class.getName()
-    };
-
     protected ProjectController projectController;
 
     protected TextAdapter name;
     protected TextAdapter cacheSize;
     protected JCheckBox objectValidation;
     protected JCheckBox externalTransactions;
-    protected JComboBox queryCacheFactory;
     protected JCheckBox sharedCache;
     protected JCheckBox remoteUpdates;
     protected JButton configRemoteUpdates;
@@ -101,9 +92,6 @@ public class DataDomainView extends JPan
 
         this.objectValidation = new JCheckBox();
         this.externalTransactions = new JCheckBox();
-
-        this.queryCacheFactory = Application.getWidgetFactory().createUndoableComboBox();
-
         this.sharedCache = new JCheckBox();
         this.remoteUpdates = new JCheckBox();
         this.configRemoteUpdates = new JButton("Configure...");
@@ -113,7 +101,7 @@ public class DataDomainView extends JPan
         CellConstraints cc = new CellConstraints();
         FormLayout layout = new FormLayout(
                 "right:pref, 3dlu, fill:50dlu, 3dlu, fill:47dlu, 3dlu, fill:100",
-                "p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p");
+                "p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p");
 
         PanelBuilder builder = new PanelBuilder(layout);
         builder.setDefaultDialogBorder();
@@ -129,18 +117,16 @@ public class DataDomainView extends JPan
         builder.add(externalTransactions, cc.xy(3, 7));
 
         builder.addSeparator("Cache Configuration", cc.xywh(1, 9, 7, 1));
-        builder.addLabel("Query Cache Factory:", cc.xy(1, 11));
-        builder.add(queryCacheFactory, cc.xywh(3, 11, 5, 1));
 
-        builder.addLabel("Size of Object Cache:", cc.xy(1, 13));
-        builder.add(cacheSize.getComponent(), cc.xy(3, 13));
+        builder.addLabel("Size of Object Cache:", cc.xy(1, 11));
+        builder.add(cacheSize.getComponent(), cc.xy(3, 11));
 
-        builder.addLabel("Use Shared Cache:", cc.xy(1, 15));
-        builder.add(sharedCache, cc.xy(3, 15));
+        builder.addLabel("Use Shared Cache:", cc.xy(1, 13));
+        builder.add(sharedCache, cc.xy(3, 13));
 
-        builder.addLabel("Remote Change Notifications:", cc.xy(1, 17));
-        builder.add(remoteUpdates, cc.xy(3, 17));
-        builder.add(configRemoteUpdates, cc.xy(7, 17));
+        builder.addLabel("Remote Change Notifications:", cc.xy(1, 15));
+        builder.add(remoteUpdates, cc.xy(3, 15));
+        builder.add(configRemoteUpdates, cc.xy(7, 15));
 
         this.setLayout(new BorderLayout());
         this.add(builder.getPanel(), BorderLayout.CENTER);
@@ -149,19 +135,6 @@ public class DataDomainView extends JPan
     protected void initController() {
         projectController.addDomainDisplayListener(this);
 
-        queryCacheFactory.setEditable(true);
-        queryCacheFactory.setModel(new DefaultComboBoxModel(QUERY_CACHE_FACTORIES));
-
-        queryCacheFactory.addActionListener(new ActionListener() {
-
-            public void actionPerformed(ActionEvent e) {
-                setDomainProperty(
-                        DataDomain.QUERY_CACHE_FACTORY_PROPERTY,
-                        (String) queryCacheFactory.getModel().getSelectedItem(),
-                        MapQueryCacheFactory.class.getName());
-            }
-        });
-
         // add action listener to checkboxes
         objectValidation.addActionListener(new ActionListener() {
 
@@ -323,10 +296,6 @@ public class DataDomainView extends JPan
         remoteUpdates.setEnabled(sharedCache.isSelected());
         configRemoteUpdates.setEnabled(remoteUpdates.isEnabled()
                 && remoteUpdates.isSelected());
-
-        queryCacheFactory.setSelectedItem(getDomainProperty(
-                DataDomain.QUERY_CACHE_FACTORY_PROPERTY,
-                MapQueryCacheFactory.class.getName()));
     }
 
     void setDomainName(String newName) {