Author: ppoddar
Date: Fri Aug 27 19:33:37 2010
New Revision: 990238

URL: http://svn.apache.org/viewvc?rev=990238&view=rev
Log:
OPENJPA-1776,OPENJPA-1777: Delay distribution decision to flush. Set 
diagonistic context by reflection.

Modified:
    
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributedBrokerImpl.java
    
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributionPolicy.java
    
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/SliceImplHelper.java
    
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedJDBCConfigurationImpl.java
    
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/PersistenceTestCase.java
    openjpa/trunk/openjpa-slice/src/test/resources/META-INF/persistence.xml

Modified: 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributedBrokerImpl.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributedBrokerImpl.java?rev=990238&r1=990237&r2=990238&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributedBrokerImpl.java
 (original)
+++ 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributedBrokerImpl.java
 Fri Aug 27 19:33:37 2010
@@ -26,6 +26,7 @@ import org.apache.openjpa.kernel.Finaliz
 import org.apache.openjpa.kernel.OpCallbacks;
 import org.apache.openjpa.kernel.OpenJPAStateManager;
 import org.apache.openjpa.kernel.QueryImpl;
+import org.apache.openjpa.kernel.StateManagerImpl;
 import org.apache.openjpa.kernel.StoreQuery;
 import org.apache.openjpa.lib.util.Localizer;
 import org.apache.openjpa.slice.jdbc.TargetFetchConfiguration;
@@ -85,18 +86,35 @@ public class DistributedBrokerImpl exten
         boolean replicated = SliceImplHelper.isReplicated(pc, 
getConfiguration());
         if (getOperatingSet().isEmpty() && 
!SliceImplHelper.isSliceAssigned(sm)) {
             info = SliceImplHelper.getSlicesByPolicy(pc, getConfiguration(), 
this);
-            _rootSlice = info.getSlices()[0];
+            if (info != null) {
+                _rootSlice = info.getSlices()[0];
+            }
+        }
+        if (sm == null) {
+            sm = super.persist(pc, id, explicit, call);
         }
-        sm = super.persist(pc, id, explicit, call);
         if (!SliceImplHelper.isSliceAssigned(sm)) {
             if (info == null) {
-                info = replicated ? SliceImplHelper.getSlicesByPolicy(pc, 
getConfiguration(), this) : new SliceInfo(
-                        _rootSlice);
+                info = replicated 
+                     ? SliceImplHelper.getSlicesByPolicy(pc, 
getConfiguration(), this) 
+                     : _rootSlice != null ? new SliceInfo(_rootSlice) : null;
             }
-            info.setInto(sm);
+            if (info != null)
+                info.setInto(sm);
         }
         return sm;
     }
+    
+    @Override
+    protected void setStateManager(Object id, StateManagerImpl sm, int status) 
{
+        try {
+            super.setStateManager(id, sm, status);
+        } catch (Exception e) {
+            if (status == 0) { // STATUS_INIT
+                // ignore
+            }
+        }
+    }    
 
     @Override
     public boolean endOperation() {

Modified: 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributionPolicy.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributionPolicy.java?rev=990238&r1=990237&r2=990238&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributionPolicy.java
 (original)
+++ 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/DistributionPolicy.java
 Fri Aug 27 19:33:37 2010
@@ -37,6 +37,11 @@ public interface DistributionPolicy {
        /**
         * Gets the name of the target slice where the given newly persistent or
         * the detached, to-be-merged instance will be stored.
+        * <br>
+        * If the current state of the given instance is sufficient to determine
+        * the target slice, return null. In that case, the runtime will 
callback
+        * this method again before the instance being flushed to the datastore.
+        * By then, the policy <em>must</em> be able to determine the target 
slice.  
         *  
         * @param pc The newly persistent or to-be-merged object. 
         * @param slices list of names of the active slices. The ordering of 

Modified: 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/SliceImplHelper.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/SliceImplHelper.java?rev=990238&r1=990237&r2=990238&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/SliceImplHelper.java
 (original)
+++ 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/SliceImplHelper.java
 Fri Aug 27 19:33:37 2010
@@ -38,12 +38,22 @@ public class SliceImplHelper {
                Localizer.forPackage(SliceImplHelper.class);
        
        /**
-        * Gets the target slices by calling user-specified 
-        * {...@link DistributionPolicy} or {...@link ReplicationPolicy} 
+        * Gets the target slices by calling user-specified {...@link 
DistributionPolicy} or {...@link ReplicationPolicy} 
      * depending on whether the given instance is {...@link 
DistributedConfiguration#isReplicated(Class) replicated}.
+     * The policy is invoked when an instance enters the managed life cycle. 
However, if the instance
+     * being persisted is distributed to a target slice that is <em>not</em> 
determinable by its own basic attributes,
+     * but on its associated instance then those association may not have been 
initialized at the point of entry. 
+     * In such case, the policy may return null. However, when a target slice 
may not be determinable at the 
+     * entry to managed life cycle, a target slice must be determinable by the 
time an instance is flushed.   
+     * 
+     * @param pc the managed instance whose target slice is to be determined.
+     * @param conf to supply the distribution policy
+     * @param ctx the (opaque) context of invocation. No semantics is 
currently associated.
+     * 
+     * @return information about the target slice for the given instance. Can 
be null if the policy
+     * can not determine the target slice(s) based on the current state of the 
instance.  
         */
-       public static SliceInfo getSlicesByPolicy(Object pc, 
-                       DistributedConfiguration conf, Object ctx) {
+       public static SliceInfo getSlicesByPolicy(Object pc, 
DistributedConfiguration conf, Object ctx) {
                List<String> actives = conf.getActiveSliceNames();
                Object policy = null;
                String[] targets = null;
@@ -51,17 +61,19 @@ public class SliceImplHelper {
                if (replicated) {
                        policy = conf.getReplicationPolicyInstance();
             targets = ((ReplicationPolicy)policy).replicate(pc, actives, ctx);
+            assertSlices(targets, pc, conf.getActiveSliceNames(), policy);
                } else {
                        policy = conf.getDistributionPolicyInstance();
-            targets = new String[]{((DistributionPolicy)policy).distribute
-                               (pc, actives, ctx)};
+                       String target = 
((DistributionPolicy)policy).distribute(pc, actives, ctx);
+                       if (target != null) {
+                           targets = new String[]{target};
+                       assertSlices(targets, pc, conf.getActiveSliceNames(), 
policy);
+                       }
                }
-               assertSlices(targets, pc, conf.getActiveSliceNames(), policy);
-               return new SliceInfo(replicated, targets);
+               return targets != null ? new SliceInfo(replicated, targets) : 
null;
        }
        
-       private static void assertSlices(String[] targets, Object pc, 
-           List<String> actives, Object policy) {
+       private static void assertSlices(String[] targets, Object pc, 
List<String> actives, Object policy) {
            if (targets == null || targets.length == 0)
             throw new UserException(_loc.get("no-policy-slice", new Object[] {
                 policy.getClass().getName(), pc, actives}));
@@ -80,7 +92,6 @@ public class SliceImplHelper {
         return getSlicesByPolicy(sm.getPersistenceCapable(), conf, ctx);
     }
     
-    
        /**
         * Affirms if the given instance be replicated to multiple slices.
         */
@@ -88,15 +99,6 @@ public class SliceImplHelper {
         return pc == null ? false : conf.isReplicated(pc.getClass());
        }
 
-       /**
-        * Affirms if the given instance be replicated to multiple slices.
-        */
-//     public static boolean isReplicated(OpenJPAStateManager sm) {
-//         return sm == null ? false : 
-//             if (sm == null)
-//                     return false;
-//             return sm.getMetaData().isReplicated();
-//     }
        
        /**
         * Affirms if the given StateManager has an assigned slice.

Modified: 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedJDBCConfigurationImpl.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedJDBCConfigurationImpl.java?rev=990238&r1=990237&r2=990238&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedJDBCConfigurationImpl.java
 (original)
+++ 
openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedJDBCConfigurationImpl.java
 Fri Aug 27 19:33:37 2010
@@ -18,6 +18,8 @@
  */
 package org.apache.openjpa.slice.jdbc;
 
+import java.lang.reflect.Method;
+import java.security.AccessController;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.util.ArrayList;
@@ -45,7 +47,7 @@ import org.apache.openjpa.lib.jdbc.Decor
 import org.apache.openjpa.lib.jdbc.DelegatingDataSource;
 import org.apache.openjpa.lib.log.Log;
 import org.apache.openjpa.lib.log.LogFactory;
-import org.apache.openjpa.lib.log.LogFactoryImpl;
+import org.apache.openjpa.lib.util.J2DoPrivHelper;
 import org.apache.openjpa.lib.util.Localizer;
 import org.apache.openjpa.meta.ClassMetaData;
 import org.apache.openjpa.meta.MetaDataRepository;
@@ -138,10 +140,14 @@ public class DistributedJDBCConfiguratio
     }
     
     private void setDiagnosticContext(OpenJPAConfiguration conf) {
-        String unit = conf.getId();
         LogFactory logFactory = conf.getLogFactory();
-        if (logFactory instanceof LogFactoryImpl) {
-            ((LogFactoryImpl)logFactory).setDiagnosticContext(unit);
+        try {
+            Method setter = AccessController.doPrivileged(J2DoPrivHelper.
+                    getDeclaredMethodAction(logFactory.getClass(), 
+                    "setDiagnosticContext", new Class[]{String.class}));
+            setter.invoke(logFactory, conf.getId());
+        } catch (Throwable t) {
+            // no contextual logging
         }
     }
 
@@ -630,6 +636,7 @@ public class DistributedJDBCConfiguratio
         return _replicationRepos.contains(cls);
     }
     
+    
     /**
      * A private repository of replicated types.
      * 

Modified: 
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/PersistenceTestCase.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/PersistenceTestCase.java?rev=990238&r1=990237&r2=990238&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/PersistenceTestCase.java
 (original)
+++ 
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/PersistenceTestCase.java
 Fri Aug 27 19:33:37 2010
@@ -111,7 +111,9 @@ public abstract class PersistenceTestCas
             map.put("openjpa.MetaDataFactory",
                 "jpa(Types=" + buf.toString() + ")");
         }
-
+        if (!map.containsKey("openjpa.ConnectionFactoryProperties")) {
+            map.put("openjpa.ConnectionFactoryProperties", 
"PrintParameters=true");
+        }
         return (OpenJPAEntityManagerFactorySPI) Persistence.
             createEntityManagerFactory(pu, map);
     }

Modified: 
openjpa/trunk/openjpa-slice/src/test/resources/META-INF/persistence.xml
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-slice/src/test/resources/META-INF/persistence.xml?rev=990238&r1=990237&r2=990238&view=diff
==============================================================================
--- openjpa/trunk/openjpa-slice/src/test/resources/META-INF/persistence.xml 
(original)
+++ openjpa/trunk/openjpa-slice/src/test/resources/META-INF/persistence.xml Fri 
Aug 27 19:33:37 2010
@@ -202,7 +202,7 @@
          
          <property name="openjpa.Multithreaded" value="false"/>
          <property name="openjpa.Log" value="DefaultLevel=INFO, Enhance=TRACE, 
SQL=TRACE"/>
-         <property name="openjpa.jdbc.SynchronizeMappings" 
value="buildSchema(ForeignKeys=native"/>
+         <property name="openjpa.jdbc.SynchronizeMappings" 
value="buildSchema(ForeignKeys=native)"/>
          <property name="openjpa.jdbc.MappingDefaults" 
value="DefaultMissingInfo=true"/>
             <property name="openjpa.RuntimeUnenhancedClasses" 
value="unsupported"/>
             <property name="openjpa.DynamicEnhancementAgent"   value="false"/>
@@ -224,10 +224,10 @@
          <property name="openjpa.slice.Ford.ConnectionURL" 
value="jdbc:mysql://localhost/honda"/>
          
          <property name="openjpa.Multithreaded" value="false"/>
-         <property name="openjpa.jdbc.SynchronizeMappings" value="refresh"/>
+         <property name="openjpa.jdbc.SynchronizeMappings" 
value="buildSchema(ForeignKeys=native)"/>
          <property name="openjpa.jdbc.MappingDefaults" 
value="DefaultMissingInfo=true"/>
-            <property name="openjpa.RuntimeUnenhancedClasses" 
value="unsupported"/>
-            <property name="openjpa.DynamicEnhancementAgent"   value="false"/>
+         <property name="openjpa.RuntimeUnenhancedClasses" 
value="unsupported"/>
+         <property name="openjpa.DynamicEnhancementAgent"   value="false"/>
       </properties>
    </persistence-unit>
    


Reply via email to