openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mik...@apache.org
Subject svn commit: r791628 [1/2] - in /openjpa/trunk: openjpa-kernel/src/main/java/org/apache/openjpa/conf/ openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/ openjpa-persistence-jdbc/src/test/...
Date Mon, 06 Jul 2009 22:09:33 GMT
Author: mikedd
Date: Mon Jul  6 22:09:32 2009
New Revision: 791628

URL: http://svn.apache.org/viewvc?rev=791628&view=rev
Log:
OPENJPA-1150. Adding support for simple write-behind cache scenarios.

Added:
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/AbstractWriteBehindCache.java   (with props)
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/AbstractWriteBehindCallback.java   (with props)
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/SimpleWriteBehindCache.java   (with props)
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/SimpleWriteBehindCacheKey.java   (with props)
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/SimpleWriteBehindCallback.java   (with props)
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCache.java   (with props)
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCacheKey.java   (with props)
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCacheManager.java   (with props)
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCacheManagerImpl.java   (with props)
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCallback.java   (with props)
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCallbackManager.java   (with props)
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindConfigurationException.java   (with props)
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindException.java   (with props)
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindStoreManager.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/AbstractWriteBehindTestCase.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/CustomWriteBehindCache.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/CustomWriteBehindCallback.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/TestWriteBehindConfiguration.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/crud/
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/crud/AbstractCrudTest.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/crud/AbstractDeleteTest.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/crud/AbstractInsertTest.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/crud/AbstractUpdateTest.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/crud/TestNonGeneratedIdDelete.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/crud/TestNonGeneratedIdInsert.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/crud/TestNonGeneratedIdUpdate.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/crud/TestTableGeneratedIdDelete.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/crud/TestTableGeneratedIdUpdate.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/crud/TestTableGeneratedInsert.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/entities/
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/entities/AbstractSimpleEntity.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/entities/SimpleDatabaseGeneratedIdEntity.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/entities/SimpleEntity.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/entities/SimpleNonGeneratedIdEntity.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/entities/SimpleTableGeneratedIdEntity.java   (with props)
Modified:
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfiguration.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfigurationImpl.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfiguration.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfiguration.java?rev=791628&r1=791627&r2=791628&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfiguration.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfiguration.java Mon Jul  6 22:09:32 2009
@@ -52,6 +52,8 @@
 import org.apache.openjpa.util.ClassResolver;
 import org.apache.openjpa.util.ProxyManager;
 import org.apache.openjpa.util.StoreFacadeTypeRegistry;
+import org.apache.openjpa.writebehind.WriteBehindCacheManager;
+import org.apache.openjpa.writebehind.WriteBehindCallback;
 
 /**
  * Defines the properties necessary to configure runtime properties and
@@ -1722,5 +1724,53 @@
      * 
      * @since 2.0.0
      */
-    public void setLifecycleEventManager(String eventMgr);    
+    public void setLifecycleEventManager(String eventMgr);
+
+    /**
+     * The {@link WriteBehindCache} configuration string.
+     * @since 2.0.0
+     */
+    public String getWriteBehindCache();
+
+    /**
+     * Set the {@link WriteBehindCache} configuration string.
+     * @since 2.0.0
+     */
+    public void setWriteBehindCache(String writeBehindCache);
+    
+    /**
+     * Get the {@link WriteBehindCacheManager} configuration string.
+     * @since 2.0.0
+     */
+    public String getWriteBehindCacheManager();
+
+    /**
+     * Set the {@link WriteBehindCacheManager} configuration string.
+     * @since 2.0.0
+     */
+    public void setWriteBehindCacheManager(String writeBehindCache);
+    
+    /**
+     * Get the {@link WriteBehindCacheManager} instance for this configuration.
+     * @since 2.0.0
+     */
+    public WriteBehindCacheManager getWriteBehindCacheManagerInstance();
+    
+    /**
+     * Get the {@link WriteBehindCallback} configuration string.
+     * @since 2.0.0
+     */
+    public String getWriteBehindCallback();
+    
+    /**
+     * Get the {@link WriteBehindCallback} instance for this configuration.
+     * @since 2.0.0 
+     */
+    public WriteBehindCallback getWriteBehindCallbackInstance();
+    
+    /**
+     * Set the {@link WriteBehindCallback} configuration string.
+     * @since 2.0.0
+     */
+    public void setWriteBehindCallback(String wbcallback);
 }

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfigurationImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfigurationImpl.java?rev=791628&r1=791627&r2=791628&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfigurationImpl.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfigurationImpl.java Mon Jul  6 22:09:32 2009
@@ -67,6 +67,12 @@
 import org.apache.openjpa.util.ProxyManager;
 import org.apache.openjpa.util.StoreFacadeTypeRegistry;
 import org.apache.openjpa.validation.ValidatingLifecycleEventManager;
+import org.apache.openjpa.writebehind.SimpleWriteBehindCache;
+import org.apache.openjpa.writebehind.SimpleWriteBehindCallback;
+import org.apache.openjpa.writebehind.WriteBehindCacheManager;
+import org.apache.openjpa.writebehind.WriteBehindCacheManagerImpl;
+import org.apache.openjpa.writebehind.WriteBehindCallback;
+import org.apache.openjpa.writebehind.WriteBehindCallbackManager;
 
 /**
  * Implementation of the {@link OpenJPAConfiguration} interface.
@@ -159,6 +165,9 @@
     public ObjectValue validator;
     public ObjectValue lifecycleEventManager;
 
+    public ObjectValue writeBehindCachePlugin;
+    public ObjectValue writeBehindCacheManagerPlugin;
+    public ObjectValue writeBehindCallbackPlugin;
     
     // custom values
     public BrokerFactoryValue brokerFactoryPlugin;
@@ -605,6 +614,31 @@
         supportedOptions.add(OPTION_VALUE_AUTOASSIGN);
         supportedOptions.add(OPTION_VALUE_INCREMENT);
         supportedOptions.add(OPTION_DATASTORE_CONNECTION);
+        
+        writeBehindCacheManagerPlugin = addPlugin("WriteBehindCacheManager", true);
+        aliases = new String[] { "default", WriteBehindCacheManagerImpl.class.getName() };
+        writeBehindCacheManagerPlugin.setAliases(aliases);
+        writeBehindCacheManagerPlugin.setDefault(aliases[0]);
+        writeBehindCacheManagerPlugin.setString(aliases[0]);
+        writeBehindCacheManagerPlugin.setInstantiatingGetter("getWriteBehindCacheManager");
+
+        writeBehindCachePlugin = addPlugin("WriteBehindCache", false);
+        aliases = new String[] { 
+            "false", null, 
+            "true", SimpleWriteBehindCache.class.getName(), 
+            "simple", SimpleWriteBehindCache.class.getName() };
+        writeBehindCachePlugin.setAliases(aliases);
+        writeBehindCachePlugin.setDefault(aliases[0]);
+        writeBehindCachePlugin.setString(aliases[0]);
+        
+        writeBehindCallbackPlugin = addPlugin("WriteBehindCallback", true); 
+        aliases = new String[] { 
+            "false", null, 
+            "true", SimpleWriteBehindCallback.class.getName(), 
+            "simple", SimpleWriteBehindCallback.class.getName() };
+        writeBehindCallbackPlugin.setAliases(aliases);
+        writeBehindCallbackPlugin.setDefault(aliases[0]);
+        writeBehindCallbackPlugin.setString(aliases[0]);
 
         if (derivations)
             ProductDerivations.beforeConfigurationLoad(this);
@@ -1655,5 +1689,56 @@
     public void setDynamicEnhancementAgent(boolean dynamic) {
         dynamicEnhancementAgent.set(dynamic);
     }    
+
+    public String getWriteBehindCache() {
+        return writeBehindCachePlugin.getString();
+    }
+
+    public void setWriteBehindCache(String writeBehindCache) {
+        writeBehindCachePlugin.setString(writeBehindCache);
+    }
+    
+    public String getWriteBehindCacheManager() {
+        return writeBehindCacheManagerPlugin.getString();
+    }
+
+    public void setWriteBehindCacheManager(String writeBehindCacheManager) {
+        writeBehindCacheManagerPlugin.setString(writeBehindCacheManager);
+    }
+
+    public WriteBehindCacheManager getWriteBehindCacheManagerInstance() {
+        WriteBehindCacheManager wbcm =
+            (WriteBehindCacheManager) writeBehindCacheManagerPlugin.get();
+        if (wbcm == null) {
+            wbcm =
+                (WriteBehindCacheManager) writeBehindCacheManagerPlugin
+                    .instantiate(WriteBehindCacheManager.class, this);
+            wbcm.initialize(this, writeBehindCachePlugin); 
+        }
+        return wbcm;
+    }
+    
+    public String getWriteBehindCallback() { 
+        return writeBehindCallbackPlugin.getString();
+        
+    }
+    public WriteBehindCallback getWriteBehindCallbackInstance() {
+        WriteBehindCallback callback =
+            (WriteBehindCallback) writeBehindCallbackPlugin.get();
+        
+        if (callback == null) {
+            WriteBehindCallbackManager manager = WriteBehindCallbackManager.getInstance();
+            callback = manager.getCallback(getWriteBehindCallback());
+            if (callback == null) {
+                callback = (WriteBehindCallback) writeBehindCallbackPlugin.instantiate(WriteBehindCallback.class, this);
+                manager.putCallback(getWriteBehindCallback(), callback);
+            }
+        }
+        return callback;
+    }
+    
+    public void setWriteBehindCallback(String writeBehindCallback) {
+        writeBehindCallbackPlugin.setString(writeBehindCallback);
+    }
 }
 

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java?rev=791628&r1=791627&r2=791628&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java Mon Jul  6 22:09:32 2009
@@ -63,6 +63,11 @@
 import org.apache.openjpa.util.InvalidStateException;
 import org.apache.openjpa.util.OpenJPAException;
 import org.apache.openjpa.util.UserException;
+import org.apache.openjpa.writebehind.WriteBehindCache;
+import org.apache.openjpa.writebehind.WriteBehindCallback;
+import org.apache.openjpa.writebehind.WriteBehindConfigurationException;
+import org.apache.openjpa.writebehind.WriteBehindException;
+import org.apache.openjpa.writebehind.WriteBehindStoreManager;
 
 /**
  * Abstract implementation of the {@link BrokerFactory}
@@ -116,6 +121,8 @@
     
     // Set of properties supported for the EntityManagerFactory
     private Set<String> _supportedPropertyNames = new TreeSet<String>();
+    
+    private WriteBehindCallback _writeBehindCallback; 
 
     /**
      * Return an internal factory pool key for the given configuration.
@@ -162,6 +169,7 @@
                 _conf.getConnectionRetainModeConstant(), false).close(); 
         }
 
+        initWriteBehindCallback();
     }
 
     /**
@@ -377,6 +385,11 @@
 
     public void close() {
         lock();
+
+        if(_writeBehindCallback != null) {
+            _writeBehindCallback.close();
+        }
+        
         try {
             assertOpen();
             assertNoActiveTransaction();
@@ -872,11 +885,20 @@
     }
 
     /**
-     * Create a DelegatingStoreManager for use with a Broker created by this
-     * factory.
+     * <P>
+     * Create a DelegatingStoreManager for use with a Broker created by this factory.
+     * </P>
+     * <P>
+     * If a DataCache has been enabled a DataCacheStoreManager will be returned. If a WriteBehind cache is also enabled 
+     * the DataCacheStoreManager will  delegate to a WriteBehindStoreManager. If no WriteBehindCache is in use the 
+     * DataCache will delegate to a StoreManager returned by newStoreManager(). 
+     * </P>
+     * <P>
+     * If no DataCache is in use an ROPStoreManager will be returned. 
+     * </P>
      * 
-     * @return A DataCacheStoreManager if a DataCache is in use otherwise an
-     *         ROPStoreManager
+     * @return A delegating store manager suitable for the current
+     *         configuration.
      */
     protected DelegatingStoreManager createDelegatingStoreManager() { 
         // decorate the store manager for data caching and custom
@@ -885,11 +907,39 @@
         // that way
         StoreManager sm = newStoreManager();
         DelegatingStoreManager dsm = null;
-        if (_conf.getDataCacheManagerInstance().getSystemDataCache()
-            != null)
-            dsm = new DataCacheStoreManager(sm);
+        if (_conf.getDataCacheManagerInstance().getSystemDataCache() != null) {
+            WriteBehindCache wbCache = _conf.getWriteBehindCacheManagerInstance().getSystemWriteBehindCache();
+            if (wbCache != null) {
+                dsm = new DataCacheStoreManager(new WriteBehindStoreManager(sm, wbCache));
+            } else {
+                dsm = new DataCacheStoreManager(sm);
+            }
+        }
         dsm = new ROPStoreManager((dsm == null) ? sm : dsm);
         
         return dsm;
     }
+    
+    protected void initWriteBehindCallback() { 
+        WriteBehindCache cache = _conf.getWriteBehindCacheManagerInstance().getSystemWriteBehindCache();
+        if (cache != null) {
+            Broker broker =
+                newBroker(_conf.getConnectionUserName(), 
+                          _conf.getConnectionPassword(), 
+                          _conf.isConnectionFactoryModeManaged(), 
+                          _conf.getConnectionRetainModeConstant(),
+                          false);
+
+            // The Broker used by the WriteBehind cache should not be tracked
+            // by the factory - we'll manually clean up when the factory is
+            // closed.
+            _transactional.remove(broker);
+            _brokers.remove(broker);
+            
+            _writeBehindCallback = _conf.getWriteBehindCallbackInstance();
+            _writeBehindCallback.initialize(broker, cache);
+
+            new Thread(_writeBehindCallback).start();
+        }
+    }
 }

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java?rev=791628&r1=791627&r2=791628&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java Mon Jul  6 22:09:32 2009
@@ -174,6 +174,53 @@
     }
 
     /**
+     * Create a new StateManager instance based on the StateManager provided. A
+     * new PersistenceCapable instance will be created and associated with the
+     * new StateManager. All fields will be copied into the ne PC instance as
+     * well as the dirty, loaded, and flushed bitsets.
+     * 
+     * @param sm A statemanager instance which will effectively be cloned.
+     */
+    public StateManagerImpl(StateManagerImpl sm) {
+        this(sm, sm.getPCState());
+    }
+
+    /**
+     * Create a new StateManager instance, optionally overriding the state
+     * (FLUSHED, DELETED, etc) of the underlying PersistenceCapable instance).
+     * 
+     * @param sm
+     *            A statemanager instance which will effectively be cloned.
+     * @param newState
+     *            The new state of the underlying persistence capable object.
+     */
+    public StateManagerImpl(StateManagerImpl sm, PCState newState) { 
+        this(sm.getId(), sm.getMetaData(), sm.getBroker());
+
+        PersistenceCapable origPC = sm.getPersistenceCapable();
+        _pc = origPC.pcNewInstance(sm, false);
+        
+        int[] fields = new int[sm.getMetaData().getFields().length];
+        for (int i = 0; i < fields.length; i++) {
+            fields[i] = i;
+        }
+        _pc.pcCopyFields(origPC, fields);
+        _pc.pcReplaceStateManager(this);
+        _state = newState;
+
+        // clone the field bitsets. 
+        _dirty=(BitSet)sm.getDirty().clone();
+        _loaded = (BitSet)sm.getLoaded().clone();
+        _flush = (BitSet) sm.getFlushed().clone();
+        _version = sm.getVersion();
+        
+        _oid = sm.getObjectId(); 
+        _id = sm.getId();
+        
+        // more data may need to be copied. 
+    }
+
+    /**
      * Set the owning state and field if this is an embedded instance.
      */
     void setOwner(StateManagerImpl owner, ValueMetaData ownerMeta) {

Added: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/AbstractWriteBehindCache.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/AbstractWriteBehindCache.java?rev=791628&view=auto
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/AbstractWriteBehindCache.java (added)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/AbstractWriteBehindCache.java Mon Jul  6 22:09:32 2009
@@ -0,0 +1,30 @@
+/*
+ * 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.openjpa.writebehind;
+
+public abstract class AbstractWriteBehindCache implements WriteBehindCache {
+    public String getName() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+    
+    public void setName(String name) {
+        // TODO Auto-generated method stub
+    }
+}

Propchange: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/AbstractWriteBehindCache.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/AbstractWriteBehindCallback.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/AbstractWriteBehindCallback.java?rev=791628&view=auto
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/AbstractWriteBehindCallback.java (added)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/AbstractWriteBehindCallback.java Mon Jul  6 22:09:32 2009
@@ -0,0 +1,23 @@
+/*
+ * 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.openjpa.writebehind;
+
+public abstract class AbstractWriteBehindCallback implements
+    WriteBehindCallback {
+}

Propchange: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/AbstractWriteBehindCallback.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/SimpleWriteBehindCache.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/SimpleWriteBehindCache.java?rev=791628&view=auto
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/SimpleWriteBehindCache.java (added)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/SimpleWriteBehindCache.java Mon Jul  6 22:09:32 2009
@@ -0,0 +1,122 @@
+/*
+ * 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.openjpa.writebehind;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.openjpa.enhance.PersistenceCapable;
+import org.apache.openjpa.kernel.StateManagerImpl;
+import org.apache.openjpa.meta.ClassMetaData;
+
+public class SimpleWriteBehindCache extends AbstractWriteBehindCache {
+
+    private Map<WriteBehindCacheKey, StateManagerImpl> _cache = new HashMap<WriteBehindCacheKey, StateManagerImpl>();
+
+    public List<Exception> add(Collection<StateManagerImpl> sms) {
+        List<Exception> exceptions = new ArrayList<Exception>();
+        for (StateManagerImpl sm : sms) {
+            try {
+                add(sm);
+            } catch (WriteBehindException wbe) {
+                exceptions.add(wbe);
+            }
+        }
+        return exceptions;
+    }
+
+    protected void add(StateManagerImpl sm) {
+        WriteBehindCacheKey key = getKey(sm);
+        synchronized (_cache) {
+            if (_cache.containsKey(key) && _cache.get(key) != sm) {
+                _cache.put(key, merge(_cache.get(key), sm));
+            } else { 
+                _cache.put(key, sm);
+            }
+        }
+
+    }
+
+    public boolean contains(Object o) {
+        boolean rval = false;
+        StateManagerImpl sm = getStateManager(o);
+        if(sm != null) {
+            rval =  _cache.containsKey(getKey(sm));    
+        }
+        return rval;
+    }
+
+    public WriteBehindCacheKey getKey(StateManagerImpl sm) {
+        ClassMetaData md = sm.getMetaData();
+        Class<?> cls = md.getDescribedType();
+        String className = cls.getCanonicalName();
+        Object id = sm.getId();
+        
+        SimpleWriteBehindCacheKey key =
+            new SimpleWriteBehindCacheKey(className, id);
+        return key;
+    }
+
+    public int getSize() {
+        return _cache.size();
+    }
+
+    public Collection<StateManagerImpl> getStateManagers() {
+        return new ArrayList<StateManagerImpl>(_cache.values());
+    }
+
+    public void initialize(WriteBehindCacheManager manager) {
+        // TODO Auto-generated method stub
+    }
+
+    protected StateManagerImpl getStateManager(Object o) {
+        StateManagerImpl rval = null;
+        if (o instanceof StateManagerImpl) {
+            rval = (StateManagerImpl) o;
+        } else if (o instanceof PersistenceCapable) {
+            rval = (StateManagerImpl) ((PersistenceCapable) o).pcGetStateManager();
+        }
+        return rval;
+    }
+
+    public void clear() {
+        synchronized(_cache) { 
+            _cache.clear();
+        }
+    }
+
+    public boolean isEmpty() {
+        return _cache.isEmpty();
+    }
+    
+    // Might be better in smImpl. 
+    protected StateManagerImpl merge(StateManagerImpl from,
+        StateManagerImpl into) {
+        for (int i = 0; i < into.getMetaData().getFields().length; i++) {
+            if (from.getDirty().get(i)) {
+                into.dirty(i);
+            }
+        }
+        return into;
+    }
+
+}

Propchange: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/SimpleWriteBehindCache.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/SimpleWriteBehindCacheKey.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/SimpleWriteBehindCacheKey.java?rev=791628&view=auto
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/SimpleWriteBehindCacheKey.java (added)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/SimpleWriteBehindCacheKey.java Mon Jul  6 22:09:32 2009
@@ -0,0 +1,82 @@
+/*
+ * 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.openjpa.writebehind;
+
+/**
+ * Simple key implementation (concatenates entity classname with primary key
+ * value).
+ */
+public class SimpleWriteBehindCacheKey implements WriteBehindCacheKey {
+    public String _className;
+    public Object _pk;
+
+    public SimpleWriteBehindCacheKey(String className, Object pk) {
+        _className = className;
+        _pk = pk;
+    }
+
+    public Object getPk() {
+        return _pk;
+    }
+
+    public void setPk(Object pk) {
+        this._pk = pk;
+    }
+
+    public String getClassName() {
+        return _className;
+    }
+
+    public void setClassName(String className) {
+        _className = className;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result =
+            prime * result + ((_className == null) ? 0 : _className.hashCode());
+        result = prime * result + ((_pk == null) ? 0 : _pk.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        SimpleWriteBehindCacheKey other = (SimpleWriteBehindCacheKey) obj;
+        if (_className == null) {
+            if (other._className != null)
+                return false;
+        } else if (!_className.equals(other._className))
+            return false;
+        if (_pk == null) {
+            if (other._pk != null)
+                return false;
+        } else if (!_pk.equals(other._pk))
+            return false;
+        return true;
+    }
+
+}

Propchange: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/SimpleWriteBehindCacheKey.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/SimpleWriteBehindCallback.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/SimpleWriteBehindCallback.java?rev=791628&view=auto
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/SimpleWriteBehindCallback.java (added)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/SimpleWriteBehindCallback.java Mon Jul  6 22:09:32 2009
@@ -0,0 +1,93 @@
+/*
+ * 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.openjpa.writebehind;
+
+import java.util.Collection;
+
+import org.apache.openjpa.kernel.Broker;
+import org.apache.openjpa.kernel.StateManagerImpl;
+import org.apache.openjpa.kernel.StoreManager;
+
+public class SimpleWriteBehindCallback extends AbstractWriteBehindCallback {
+
+    private int _sleepTime = 2000;
+    private boolean done = false;
+
+    private WriteBehindCache _cache = null;
+    private Broker _broker;
+
+    public void initialize(Broker broker, WriteBehindCache cache) {
+        _cache = cache;
+        _broker = broker;
+    }
+
+    public Collection<Exception> flush() {
+        Collection<Exception> errors = null;
+        // skip past any delegating store managers.
+        StoreManager storeManager =
+            _broker.getStoreManager().getInnermostDelegate();
+        Collection<StateManagerImpl> sms = null;
+        if (_cache != null && !_cache.isEmpty()) {
+            // TODO lock or switch the cache
+            sms = _cache.getStateManagers();
+            _cache.clear();
+        }
+        if (sms != null && !sms.isEmpty()) {
+            storeManager.retainConnection();
+            storeManager.begin();
+            errors = storeManager.flush(sms);
+            if(errors != null && !errors.isEmpty() ) {
+                for(Exception e : errors) { 
+                    e.printStackTrace();
+                }
+            }
+            storeManager.commit();
+            storeManager.releaseConnection();
+        }
+        return errors;
+    }
+
+    public void run() {
+        while (!done) {
+            try {
+                Thread.sleep(_sleepTime);
+                handleExceptions(flush());
+            } catch (InterruptedException e) {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
+            }
+        }
+    }
+
+    public void handleExceptions(Collection<Exception> exceptions) {
+        if (exceptions != null && !exceptions.isEmpty()) {
+            done = true;
+            for (Exception e : exceptions) {
+                System.out.println(e.getMessage());
+                e.printStackTrace();
+            }
+        }
+    }
+
+    public void close() {
+        done = true;
+        flush();
+        _broker.close();
+    }
+}

Propchange: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/SimpleWriteBehindCallback.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCache.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCache.java?rev=791628&view=auto
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCache.java (added)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCache.java Mon Jul  6 22:09:32 2009
@@ -0,0 +1,54 @@
+/*
+ * 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.openjpa.writebehind;
+
+import java.util.Collection;
+import java.util.List;
+
+import org.apache.openjpa.kernel.StateManagerImpl;
+
+public interface WriteBehindCache {
+    public int getSize();
+    public boolean contains(Object o);
+    
+    /**
+     * Returns a string name that can be used by end-user-visible
+     * code to identify this cache.
+     */
+    public String getName();
+
+    /**
+     * Sets a string name to be used to identify this cache to end-user needs.
+     */
+    public void setName(String name);
+
+    public List<Exception> add(Collection<StateManagerImpl> sms);
+    
+    public Collection<StateManagerImpl> getStateManagers();
+    
+    /**
+     * Initialize any resources associated with the given
+     * {@link WriteBehindCacheManager}.
+     */
+    public void initialize(WriteBehindCacheManager manager);
+    
+    public WriteBehindCacheKey getKey(StateManagerImpl sm);
+    public boolean isEmpty();
+    public void clear(); 
+}

Propchange: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCache.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCacheKey.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCacheKey.java?rev=791628&view=auto
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCacheKey.java (added)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCacheKey.java Mon Jul  6 22:09:32 2009
@@ -0,0 +1,25 @@
+/*
+ * 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.openjpa.writebehind;
+
+public interface WriteBehindCacheKey {
+    public int hashCode();
+
+    public boolean equals(Object obj);
+}

Propchange: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCacheKey.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCacheManager.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCacheManager.java?rev=791628&view=auto
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCacheManager.java (added)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCacheManager.java Mon Jul  6 22:09:32 2009
@@ -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.apache.openjpa.writebehind;
+
+import org.apache.openjpa.conf.OpenJPAConfiguration;
+import org.apache.openjpa.lib.conf.ObjectValue;
+
+public interface WriteBehindCacheManager {
+    public WriteBehindCache getSystemWriteBehindCache();
+
+    public WriteBehindCache getWriteBehindCache(String name);
+
+    public void initialize(OpenJPAConfiguration conf,
+        ObjectValue writeBehindCache);
+}

Propchange: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCacheManager.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCacheManagerImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCacheManagerImpl.java?rev=791628&view=auto
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCacheManagerImpl.java (added)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCacheManagerImpl.java Mon Jul  6 22:09:32 2009
@@ -0,0 +1,39 @@
+/*
+ * 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.openjpa.writebehind;
+
+import org.apache.openjpa.conf.OpenJPAConfiguration;
+import org.apache.openjpa.lib.conf.ObjectValue;
+
+public class WriteBehindCacheManagerImpl implements WriteBehindCacheManager {
+
+    WriteBehindCache _cache;
+
+    public WriteBehindCache getSystemWriteBehindCache() {
+        return _cache;
+    }
+
+    public WriteBehindCache getWriteBehindCache(String name) {
+        return _cache;
+    }
+
+    public void initialize(OpenJPAConfiguration conf, ObjectValue writeBehindCache) {
+        _cache = (WriteBehindCache) writeBehindCache.instantiate(WriteBehindCache.class, conf);
+    }
+}

Propchange: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCacheManagerImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCallback.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCallback.java?rev=791628&view=auto
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCallback.java (added)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCallback.java Mon Jul  6 22:09:32 2009
@@ -0,0 +1,33 @@
+/*
+ * 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.openjpa.writebehind;
+
+import java.util.Collection;
+
+import org.apache.openjpa.kernel.Broker;
+
+public interface WriteBehindCallback extends Runnable {
+
+    public void initialize(Broker broker,
+        WriteBehindCache cache);
+
+    public Collection<Exception> flush();
+
+    public void close();
+}

Propchange: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCallback.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCallbackManager.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCallbackManager.java?rev=791628&view=auto
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCallbackManager.java (added)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCallbackManager.java Mon Jul  6 22:09:32 2009
@@ -0,0 +1,51 @@
+/*
+ * 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.openjpa.writebehind;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class WriteBehindCallbackManager {
+    private static WriteBehindCallbackManager _instance =
+        new WriteBehindCallbackManager();
+
+    private Map<String, WriteBehindCallback> _callbacks = null;
+    
+    public static final String DEFAULT_CALLBACK_NAME = "default";
+
+    public WriteBehindCallbackManager() {
+        _callbacks = new HashMap<String, WriteBehindCallback>();
+    }
+
+    public static WriteBehindCallbackManager getInstance() {
+        return _instance;
+    }
+
+    public WriteBehindCallback getCallback(String name) {
+        return _callbacks.get(name);
+    }
+
+    public void putCallback(String name, WriteBehindCallback callback) {
+        if (_callbacks.containsKey(name)) {
+            throw new RuntimeException("Duplicate callback created for " 
+                + name);
+        }
+        _callbacks.put(name, callback);
+    }
+}

Propchange: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindCallbackManager.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindConfigurationException.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindConfigurationException.java?rev=791628&view=auto
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindConfigurationException.java (added)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindConfigurationException.java Mon Jul  6 22:09:32 2009
@@ -0,0 +1,32 @@
+/*
+ * 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.openjpa.writebehind;
+
+import org.apache.openjpa.util.OpenJPAException;
+
+public class WriteBehindConfigurationException extends OpenJPAException {
+
+    private static final long serialVersionUID = 4378466107767896762L;
+
+    @Override
+    public int getType() {
+        return USER;
+    }
+
+}

Propchange: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindConfigurationException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindException.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindException.java?rev=791628&view=auto
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindException.java (added)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindException.java Mon Jul  6 22:09:32 2009
@@ -0,0 +1,30 @@
+/*
+ * 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.openjpa.writebehind;
+
+import org.apache.openjpa.util.OpenJPAException;
+
+public class WriteBehindException extends OpenJPAException {
+    private static final long serialVersionUID = 3377707939479198010L;
+
+    @Override
+    public int getType() {
+        return INTERNAL;
+    }
+}

Propchange: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindStoreManager.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindStoreManager.java?rev=791628&view=auto
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindStoreManager.java (added)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindStoreManager.java Mon Jul  6 22:09:32 2009
@@ -0,0 +1,152 @@
+/*
+ * 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.openjpa.writebehind;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+import org.apache.openjpa.kernel.DelegatingStoreManager;
+import org.apache.openjpa.kernel.PCState;
+import org.apache.openjpa.kernel.StateManagerImpl;
+import org.apache.openjpa.kernel.StoreManager;
+
+public class WriteBehindStoreManager extends DelegatingStoreManager {
+
+    protected Set<Class<?>> includedTypes;
+    protected Set<Class<?>> excludedTypes;
+    
+    protected boolean _cacheInserts = true;
+    protected boolean _cacheUpdates = true;
+    protected boolean _cacheDeletes = true;
+    
+    public static final int OP_INSERT = 1;
+    public static final int OP_DELETE = 2;
+    public static final int OP_UPDATE = 3; 
+
+    WriteBehindCache _cache;
+    Collection<StateManagerImpl> inFlightSMs = new ArrayList<StateManagerImpl>();
+
+    public WriteBehindStoreManager(StoreManager store) {
+        this(store, null);
+    }
+
+    public WriteBehindStoreManager(StoreManager store, 
+        WriteBehindCache wbCache) {
+        super(store);
+        _cache = wbCache;
+    }
+
+    public Collection flush(Collection sms) {
+        LinkedHashSet<StateManagerImpl> passingThrough = null;
+        for (Object o : sms) {
+            if(cacheAble((StateManagerImpl)o)) {
+                StateManagerImpl sm = (StateManagerImpl) o;
+                PCState newState = sm.getPCState();
+                if(newState == PCState.PDELETEDFLUSHED) { 
+                    newState = PCState.PDELETED; // effectively reset the flush
+                }
+                inFlightSMs.add(new StateManagerImpl(sm, newState));
+            }
+            else {
+                if(passingThrough == null) { 
+                    passingThrough = new LinkedHashSet<StateManagerImpl>();
+                }
+                passingThrough.add((StateManagerImpl)o);
+            }
+        }
+        
+        Collection rval;
+        if(passingThrough != null) { 
+            rval = getDelegate().flush(passingThrough);
+        }
+        else { 
+            rval = new ArrayList<Exception>();
+        }
+        return rval;
+    }
+
+    public void commit() {
+        try {
+            super.commit();
+            _cache.add(inFlightSMs);
+        } finally {
+            inFlightSMs.clear();
+        }
+    }
+
+    public void rollback() {
+        try {
+            super.rollback();
+        } finally {
+            inFlightSMs.clear();
+        }
+    }
+
+    public Collection<Exception> flushBehind(Collection<StateManagerImpl> sms) {
+        return super.flush(sms);
+    }
+
+    public boolean cacheAble(StateManagerImpl sm) {
+        boolean rval = false;
+        switch (getOperation(sm)) {
+        case OP_INSERT:
+            if (_cacheInserts) {
+                rval = true;
+            }
+            break;
+        case OP_DELETE:
+            if (_cacheDeletes) {
+                rval = true;
+            }
+            break;
+        case OP_UPDATE:
+            if (_cacheUpdates) {
+                rval = true;
+            }
+            break;
+            default:
+                rval = false; // not sure what this is. Don't mess with it.
+        }
+        
+        // TODO Check includedTypes
+        // Like the DataCache this information should be contained in the 
+        // Mapping repository
+        // Check after checking operations since unlisted types will pre-empt 
+        // operations.
+
+        return rval;
+    }
+    
+    protected int getOperation(StateManagerImpl sm) {
+        int rval = -1; // TODO define me
+        if (sm.isDirty()) {
+            if (sm.isNew()) {
+                rval = OP_INSERT;
+            } else if (sm.isDeleted()) {
+                rval = OP_DELETE;
+            } else {
+                rval = OP_UPDATE;
+            }
+        }
+        return rval;
+    }
+
+}

Propchange: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/writebehind/WriteBehindStoreManager.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/AbstractWriteBehindTestCase.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/AbstractWriteBehindTestCase.java?rev=791628&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/AbstractWriteBehindTestCase.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/AbstractWriteBehindTestCase.java Mon Jul  6 22:09:32 2009
@@ -0,0 +1,114 @@
+/*
+ * 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.openjpa.jdbc.writebehind;
+
+import javax.persistence.EntityManagerFactory;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.openjpa.jdbc.writebehind.entities.AbstractSimpleEntity;
+import org.apache.openjpa.jdbc.writebehind.entities.SimpleNonGeneratedIdEntity;
+import org.apache.openjpa.jdbc.writebehind.entities.SimpleTableGeneratedIdEntity;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
+import org.apache.openjpa.persistence.test.SingleEMTestCase;
+import org.apache.openjpa.writebehind.WriteBehindCache;
+import org.apache.openjpa.writebehind.WriteBehindCacheManager;
+import org.apache.openjpa.writebehind.WriteBehindCallback;
+
+public abstract class AbstractWriteBehindTestCase extends SingleEMTestCase {
+    protected static Object[] writeBehindProps =
+        new Object[] { 
+            "openjpa.DataCache", "true",
+            "openjpa.RemoteCommitProvider", "sjvm", 
+            "openjpa.WriteBehindCache", "true",
+            "openjpa.WriteBehindCallback", "true", 
+            "openjpa.RuntimeUnenhancedClasses", "unsupported",
+            SimpleNonGeneratedIdEntity.class, SimpleTableGeneratedIdEntity.class,
+            AbstractSimpleEntity.class};
+    
+    protected static Object [] validatorProps = 
+        new Object[] { 
+        "openjpa.RuntimeUnenhancedClasses", "unsupported",
+        SimpleNonGeneratedIdEntity.class, SimpleTableGeneratedIdEntity.class,
+        AbstractSimpleEntity.class  };
+    
+    protected EntityManagerFactory _validatorEMF = null; 
+    
+    public void setUp() {
+        if(emf == null) { 
+            super.setUp(writeBehindProps);
+        }
+        if(_validatorEMF == null) { 
+            _validatorEMF = createEMF(validatorProps);
+        }
+    }
+    
+    public static Object[] getDefaultWriteBehindProperties() { 
+        return writeBehindProps;
+    }
+
+    protected WriteBehindCacheManager getWBCacheManager() {
+        return getWBCacheManager(emf);
+    }
+
+    protected WriteBehindCacheManager getWBCacheManager(
+        OpenJPAEntityManagerFactorySPI factory) {
+        WriteBehindCacheManager wbcm =
+            factory.getConfiguration().getWriteBehindCacheManagerInstance();
+        return wbcm;
+    }
+
+    protected WriteBehindCache getWBCache() {
+        return getWBCache(emf);
+    }
+
+    protected WriteBehindCache 
+        getWBCache(OpenJPAEntityManagerFactorySPI factory) {
+        return getWBCache(factory, ""); // TODO handle default name better
+    }
+
+    protected WriteBehindCallback getWBCallback() {
+        return getWBCallback(emf);
+    }
+
+    protected WriteBehindCallback getWBCallback(
+        OpenJPAEntityManagerFactorySPI factory) {
+        return factory.getConfiguration().getWriteBehindCallbackInstance();
+    }
+
+    protected WriteBehindCache getWBCache(
+        OpenJPAEntityManagerFactorySPI factory, String name) {
+        WriteBehindCache wbc = null;
+        if (StringUtils.isEmpty(name)) {
+            wbc = getWBCacheManager(factory).getSystemWriteBehindCache();
+        } else {
+            wbc = getWBCacheManager(factory).getWriteBehindCache(name);
+        }
+        return wbc;
+    }
+    
+    protected EntityManagerFactory getValidatorEMF() { 
+        return _validatorEMF;
+    }
+    
+    public void tearDown() throws Exception { 
+//        _validatorEMF.close();
+//        super.tearDown();
+    }
+
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/AbstractWriteBehindTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/CustomWriteBehindCache.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/CustomWriteBehindCache.java?rev=791628&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/CustomWriteBehindCache.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/CustomWriteBehindCache.java Mon Jul  6 22:09:32 2009
@@ -0,0 +1,84 @@
+/*
+ * 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.openjpa.jdbc.writebehind;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.apache.openjpa.kernel.OpenJPAStateManager;
+import org.apache.openjpa.kernel.StateManagerImpl;
+import org.apache.openjpa.writebehind.AbstractWriteBehindCache;
+import org.apache.openjpa.writebehind.WriteBehindCacheKey;
+import org.apache.openjpa.writebehind.WriteBehindCacheManager;
+
+/**
+ * Skeleton WriteBehindCache used to verify that the configuration options for WriteBehind are working properly. 
+ */
+public class CustomWriteBehindCache extends AbstractWriteBehindCache {
+
+    public List<Exception> add(Collection<StateManagerImpl> sms) {
+        List<Exception> exceptions = new ArrayList<Exception>(); 
+        return exceptions;
+    }
+
+    public void add(StateManagerImpl sm) {
+    }
+
+    public boolean contains(Object o) {
+        return false;
+    }
+
+    public void flush() {
+    }
+
+    public WriteBehindCacheKey getKey(StateManagerImpl sm) {
+        return null;
+    }
+
+    public String getName() {
+        return null;
+    }
+
+    public OpenJPAStateManager getSateManager(WriteBehindCacheKey key) {
+        return null;
+    }
+
+    public int getSize() {
+        return 0;
+    }
+
+    public Collection<StateManagerImpl> getStateManagers() {
+        return null;
+    }
+
+    public void initialize(WriteBehindCacheManager manager) {
+    }
+
+    public void setName(String name) {
+    }
+
+    public void clear() {
+    }
+
+    public boolean isEmpty() {
+        return false;
+    }
+}
+

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/CustomWriteBehindCache.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/CustomWriteBehindCallback.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/CustomWriteBehindCallback.java?rev=791628&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/CustomWriteBehindCallback.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/CustomWriteBehindCallback.java Mon Jul  6 22:09:32 2009
@@ -0,0 +1,45 @@
+/*
+ * 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.openjpa.jdbc.writebehind;
+
+import java.util.Collection;
+
+import org.apache.openjpa.kernel.Broker;
+import org.apache.openjpa.writebehind.AbstractWriteBehindCallback;
+import org.apache.openjpa.writebehind.WriteBehindCache;
+
+/**
+ * Skeleton WriteBehindCallback used to verify that a custom instance can be created.
+ */
+public class CustomWriteBehindCallback extends AbstractWriteBehindCallback {
+
+    public Collection<Exception> flush() {
+        return null;
+    }
+
+    public void run() {
+    }
+
+    public void initialize(Broker broker,
+        WriteBehindCache cache) {
+    }
+
+    public void close() {
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/CustomWriteBehindCallback.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/TestWriteBehindConfiguration.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/TestWriteBehindConfiguration.java?rev=791628&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/TestWriteBehindConfiguration.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/TestWriteBehindConfiguration.java Mon Jul  6 22:09:32 2009
@@ -0,0 +1,117 @@
+/*
+ * 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.openjpa.jdbc.writebehind;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.persistence.Persistence;
+import javax.persistence.PersistenceException;
+
+import org.apache.openjpa.datacache.DataCacheStoreManager;
+import org.apache.openjpa.kernel.DelegatingStoreManager;
+import org.apache.openjpa.persistence.EntityManagerImpl;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
+import org.apache.openjpa.writebehind.SimpleWriteBehindCache;
+import org.apache.openjpa.writebehind.SimpleWriteBehindCallback;
+import org.apache.openjpa.writebehind.WriteBehindCache;
+import org.apache.openjpa.writebehind.WriteBehindCacheManager;
+import org.apache.openjpa.writebehind.WriteBehindCallback;
+import org.apache.openjpa.writebehind.WriteBehindException;
+import org.apache.openjpa.writebehind.WriteBehindStoreManager;
+
+public class TestWriteBehindConfiguration extends AbstractWriteBehindTestCase {
+    public void testWriteBehindStoreManagerCreated() {
+        assertNotNull(emf);
+        assertNotNull(em);
+        DelegatingStoreManager storeManager = ((EntityManagerImpl) em).getBroker().getStoreManager();
+        
+        storeManager = (DelegatingStoreManager) storeManager.getDelegate();
+        assertTrue(String.format("Unexpected StoreManager type : %s",
+            storeManager.getClass().getName()),
+            storeManager instanceof DataCacheStoreManager);
+        assertTrue(String.format("Unexpected StoreManager type : %s",
+            storeManager.getDelegate().getClass().getName()), 
+            storeManager.getDelegate() instanceof WriteBehindStoreManager);
+    }
+
+    public void testWriteBehindCacheCreated() {
+        assertNotNull("EMF was not created.", emf);
+        WriteBehindCacheManager manager = getWBCacheManager();
+        assertNotNull("WriteBehindCacheManager should exist", manager);
+
+        WriteBehindCache wbcache = manager.getSystemWriteBehindCache();
+        assertNotNull("SystemWriteBehindCache should exist", wbcache);
+
+        assertTrue(String.format("Expecting %s to be an instance of %s",
+            wbcache, SimpleWriteBehindCache.class),
+            wbcache instanceof SimpleWriteBehindCache);
+    }
+
+    public void testWriteBehindCallbackCreated() {
+        assertNotNull("EMF was not created.", emf);
+        WriteBehindCallback callback = getWBCallback();
+        assertNotNull("WB Callback should exist", callback);
+
+        assertTrue(String.format("Expecting %s to be an instance of %s",
+            callback, SimpleWriteBehindCallback.class),
+            callback instanceof SimpleWriteBehindCallback);
+
+    }
+
+    public void testCustomCacheInstanceCreated() {
+        HashMap<String, Object> props = new HashMap<String, Object>();
+        props.put("openjpa.WriteBehindCache", CustomWriteBehindCache.class.getCanonicalName());
+        props.put("openjpa.WriteBehindCallback", "true");
+
+        WriteBehindCacheManager manager = getWBCacheManager(getCustomFactory(props));
+        assertNotNull("WriteBehindCacheManager should exist", manager);
+
+        WriteBehindCache wbcache = manager.getSystemWriteBehindCache();
+        assertNotNull("SystemWriteBehindCache should exist", wbcache);
+
+        assertTrue(String.format("Expecting %s to be an instance of %s",
+            wbcache, CustomWriteBehindCache.class),
+            wbcache instanceof CustomWriteBehindCache);
+    }
+    
+    public void testCustomCallbackCreated() {
+        HashMap<String, Object> props = new HashMap<String, Object>();
+        props.put("openjpa.WriteBehindCache", CustomWriteBehindCache.class.getCanonicalName());
+        props.put("openjpa.WriteBehindCallback", CustomWriteBehindCallback.class.getCanonicalName());
+
+        WriteBehindCallback callback = getWBCallback(getCustomFactory(props));
+        assertNotNull("WB Callback should exist", callback);
+
+        assertTrue(String.format("Expecting %s to be an instance of %s",
+            callback, CustomWriteBehindCallback.class),
+            callback instanceof CustomWriteBehindCallback);
+    }
+    
+    public OpenJPAEntityManagerFactorySPI getCustomFactory(
+        Map<String, Object> extraProps) {
+        Map<String, Object> props = getPropertiesMap(getDefaultWriteBehindProperties());
+        props.putAll(extraProps);
+
+        OpenJPAEntityManagerFactorySPI customEMF =
+            (OpenJPAEntityManagerFactorySPI) Persistence.createEntityManagerFactory(getPersistenceUnitName(), props);
+        assertNotNull("EMF was not created.", customEMF);
+        return customEMF;
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/TestWriteBehindConfiguration.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/crud/AbstractCrudTest.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/crud/AbstractCrudTest.java?rev=791628&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/crud/AbstractCrudTest.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/crud/AbstractCrudTest.java Mon Jul  6 22:09:32 2009
@@ -0,0 +1,264 @@
+/*
+ * 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.openjpa.jdbc.writebehind.crud;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.jdbc.writebehind.AbstractWriteBehindTestCase;
+import org.apache.openjpa.jdbc.writebehind.entities.SimpleEntity;
+
+public abstract class AbstractCrudTest extends AbstractWriteBehindTestCase {
+    protected List<Integer> _ids;
+    protected static int SLEEP_TIME = 6000;
+    protected static int NUM_INSERTS = 5;
+    protected static String[] NAMES = { "Able", "Baker", "Charlie" };
+
+    /**
+     * populate a set of SimpleEntities appropriate for this test.
+     */
+    protected void populate() {
+        _ids = new ArrayList<Integer>();
+        EntityManager em = _validatorEMF.createEntityManager();
+        em.getTransaction().begin();
+        SimpleEntity se = null;
+        for (int i = 0; i < NUM_INSERTS; i++) {
+            se = newEntityInstance();
+            if (!idIsGenerated()) {
+                se.setId(i + 1);
+            }
+            se.setForename(NAMES[i % NAMES.length]);
+            se.setSurname(NAMES[(i + 1) % NAMES.length]);
+            em.persist(se);
+            em.flush();
+            _ids.add(se.getId());
+        }
+        em.getTransaction().commit();
+    }
+    
+    protected void cleanup() { 
+        EntityManager vem = getValidatorEMF().createEntityManager();
+        vem.getTransaction().begin();
+
+        // TODO replace with getTableName();
+        vem.createNativeQuery("Delete from " + getEntityTypeName())
+            .executeUpdate();
+        vem.getTransaction().commit();
+        
+        // clear l2 cache since the native query is outside of the wb cache.
+        emf.getStoreCache().evictAll();
+    }
+
+    /**
+     * Return the class of entities used by this testcase
+     * @return
+     */
+    protected abstract Class<? extends SimpleEntity> getEntityType();
+
+    /**
+     * Get the type name for this entity could be different from the alias
+     * 
+     * @return
+     */
+    protected String getEntityTypeName() {
+        return getEntityType().getSimpleName();
+    }
+
+    /**
+     * Obtain a new instance of the entity used by this test
+     * @return
+     */
+    protected SimpleEntity newEntityInstance() {
+        try {
+            return getEntityType().newInstance();
+        } catch (IllegalAccessException e) {
+            return null;
+        } catch (InstantiationException e) {
+            return null;
+        }
+    }
+
+    /**
+     * Indicate whether the entities used by this test have generated ids
+     * 
+     * @return true if the entity ID is generated, otherwise false
+     */
+    protected abstract boolean idIsGenerated();
+
+    // Begin asserts
+    /**
+     * Assert that an entity "found" by a given EntityManager matches one
+     * provided.
+     * 
+     * @param em
+     *            The entity manager which will be used to find an entity.
+     * @param se
+     *            An entity instance (usually unmanaged) which is contains the
+     *            expected state of the entity.
+     * @throws AssertionFailedError
+     *             an entity matching se's ID cannot be found or if any of the
+     *             non-version fields do not match
+     */
+    public void assertEntityContents(EntityManager em, SimpleEntity se) {
+        SimpleEntity found = em.find(getEntityType(), se.getId());
+        assertNotNull(String.format("%s::%d should be found",
+            getEntityTypeName(), 1), found);
+        assertTrue(found instanceof SimpleEntity);
+        if (se.getForename() != null && se.getForename().length() != 0) {
+            assertEquals(se.getForename(), found.getForename());
+        }
+        if (se.getSurname() != null && se.getSurname().length() != 0) {
+            assertEquals(se.getSurname(), found.getSurname());
+        }
+    }
+
+    /**
+     * Assert that all entities can not be found in the database. No L2 cache or
+     * write behind cache is used.
+     */
+    public void assertEntitiesDeleted() {
+        EntityManager validatorEM = getValidatorEMF().createEntityManager();
+        SimpleEntity se = null;
+        for (int i = 0; i < NUM_INSERTS; i++) {
+            se = validatorEM.find(getEntityType(), _ids.get(i));
+            assertNull(String.format("%s::%d should have been deleted",
+                getEntityTypeName(),_ids.get(i)), se);
+        }
+        validatorEM.close();
+    }
+
+    /**
+     * Assert that all entities can be found in the database. No L2 cache or
+     * write behind cache is used.
+     */
+    public void assertEntitiesExist() {
+        EntityManager validatorEM = getValidatorEMF().createEntityManager();
+        SimpleEntity se = null;
+        for (int i = 0; i < NUM_INSERTS; i++) {
+            se = validatorEM.find(getEntityType(), _ids.get(i));
+            assertNotNull(String.format("%s::%d should exist in the database",
+                getEntityTypeName(), _ids.get(i)), se);
+        }
+        validatorEM.close();
+    }
+
+    /**
+     * Assert that all entities can not be found in the database and the name
+     * field has been updated . No L2 cache or write behind cache is used.
+     */
+    public void assertEntitiesUpdated() {
+        EntityManager validatorEM = getValidatorEMF().createEntityManager();
+        SimpleEntity se = null;
+        for (int i = 0; i < NUM_INSERTS; i++) {
+            se = newEntityInstance();
+            se.setId(_ids.get(i));
+            se.setForename(NAMES[i % NAMES.length] + " UPDATED");
+            se.setSurname(NAMES[(i + 1) % NAMES.length] + " UPDATED");
+            assertEntityContents(validatorEM, se);
+        }
+        validatorEM.close();
+    }
+
+    /**
+     * Assert that all entities can not be found in the database and the name
+     * fields are unmodified. No L2 cache or write behind cache is used.
+     */
+    public void assertEntitiesUnmodified() {
+        EntityManager validatorEM = getValidatorEMF().createEntityManager();
+        SimpleEntity se = null;
+        for (int i = 0; i < NUM_INSERTS; i++) {
+            se = newEntityInstance();
+            se.setId(_ids.get(i));
+            se.setForename(NAMES[i % NAMES.length]);
+            assertEntityContents(validatorEM, se);
+        }
+        validatorEM.close();
+    }
+
+    // begin operations
+
+    public void deleteEntities(boolean flushBetween) {
+        assertEntitiesExist();
+        em.getTransaction().begin();
+        SimpleEntity se = null;
+        for (int i = 0; i < NUM_INSERTS; i++) {
+            se = em.find(getEntityType(),_ids.get(i));
+            em.remove(se);
+            if (flushBetween) {
+                em.flush();
+            }
+        }
+        em.getTransaction().commit();
+    }
+
+    public void updateEntities(boolean flushBetween) {
+        em.getTransaction().begin();
+        SimpleEntity se = null;
+        assertEquals(NUM_INSERTS, _ids.size());
+        try {
+            for (Integer id : _ids) {
+                se = em.find(getEntityType(), id);
+                assertNotNull(String.format("%s::%d should be found",
+                    getEntityTypeName(), id), se);
+                se.setForename(se.getForename() + " UPDATED");
+                if (flushBetween) {
+                    em.flush();
+                }
+            }
+        } finally {
+            em.getTransaction().commit();
+        }
+        
+        em.getTransaction().begin();
+        try {
+            for (Integer id : _ids) {
+                se = em.find(getEntityType(), id);
+                assertNotNull(String.format("%s::%d should be found",
+                    getEntityTypeName(), id), se);
+                se.setSurname(se.getSurname() + " UPDATED");
+                if (flushBetween) {
+                    em.flush();
+                }
+            }
+        } finally {
+            em.getTransaction().commit();
+        }
+    }
+
+    public void insertEntities(boolean flushBetween) {
+        _ids = new ArrayList<Integer>();
+        em.getTransaction().begin();
+        SimpleEntity se = null;
+        for (int i = 0; i < NUM_INSERTS; i++) {
+            se = newEntityInstance();
+            if (!idIsGenerated()) {
+                se.setId(i + 1);
+            }
+            se.setForename(NAMES[i % NAMES.length]);
+            em.persist(se);
+            if (flushBetween) {
+                em.flush();
+            }
+            _ids.add(se.getId());
+        }
+        em.getTransaction().commit();
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/writebehind/crud/AbstractCrudTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message