You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by pp...@apache.org on 2007/12/27 00:43:21 UTC

svn commit: r606972 - in /labs/fluid/slice/src/main: java/org/apache/openjpa/slice/ java/org/apache/openjpa/slice/jdbc/ resources/org/apache/openjpa/slice/

Author: ppoddar
Date: Wed Dec 26 15:43:13 2007
New Revision: 606972

URL: http://svn.apache.org/viewvc?rev=606972&view=rev
Log:
Automatic collocation by cascading relations

Added:
    labs/fluid/slice/src/main/java/org/apache/openjpa/slice/DistributedBrokerImpl.java
    labs/fluid/slice/src/main/resources/org/apache/openjpa/slice/localizer.properties
Modified:
    labs/fluid/slice/src/main/java/org/apache/openjpa/slice/DistributedConfiguration.java
    labs/fluid/slice/src/main/java/org/apache/openjpa/slice/ProductDerivation.java
    labs/fluid/slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedDataSource.java
    labs/fluid/slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedJDBCBrokerFactory.java
    labs/fluid/slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedJDBCConfigurationImpl.java

Added: labs/fluid/slice/src/main/java/org/apache/openjpa/slice/DistributedBrokerImpl.java
URL: http://svn.apache.org/viewvc/labs/fluid/slice/src/main/java/org/apache/openjpa/slice/DistributedBrokerImpl.java?rev=606972&view=auto
==============================================================================
--- labs/fluid/slice/src/main/java/org/apache/openjpa/slice/DistributedBrokerImpl.java (added)
+++ labs/fluid/slice/src/main/java/org/apache/openjpa/slice/DistributedBrokerImpl.java Wed Dec 26 15:43:13 2007
@@ -0,0 +1,81 @@
+/*
+ * 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.slice;
+
+import org.apache.openjpa.kernel.BrokerImpl;
+import org.apache.openjpa.kernel.OpCallbacks;
+import org.apache.openjpa.kernel.OpenJPAStateManager;
+import org.apache.openjpa.lib.util.Localizer;
+import org.apache.openjpa.util.UserException;
+
+/**
+ * A specialized Broker to associate slice identifiers with the StateManagers as
+ * they are persisted in a cascade. This intervention helps the user to define
+ * distribution policy only for root instances i.e. the instances that are
+ * explicit argument to persist() call. The cascaded instances are assigned the
+ * same slice to honor collocation constraint.
+ * 
+ * @author Pinaki Poddar
+ * 
+ */
+public class DistributedBrokerImpl extends BrokerImpl {
+	private transient String slice;
+
+	private static final Localizer _loc =
+			Localizer.forPackage(DistributedBrokerImpl.class);
+
+	/**
+	 * Assigns slice identifier to the resultant StateManager as initialized by
+	 * the super class implementation. The slice identifier is decided by
+	 * {@link DistributionPolicy} for given <code>pc</code> if it is a root
+	 * instance i.e. the argument of the user application's persist() call. The
+	 * cascaded instances are detected by non-empty status of the current
+	 * operating set. The slice is assigned only if a StateManager has never
+	 * been assigned before.
+	 */
+	public OpenJPAStateManager persist(Object pc, Object id, boolean explicit,
+			OpCallbacks call) {
+		OpenJPAStateManager sm = getStateManager(pc);
+		if (getOperatingSet().isEmpty()
+				&& (sm == null || sm.getImplData() == null)) {
+			slice = getSlice(pc);
+		}
+		sm = super.persist(pc, id, explicit, call);
+		if (sm.getImplData() == null)
+			sm.setImplData(slice, true);
+
+		return sm;
+	}
+
+	/**
+	 * Gets the slice by the user-defined distribution policy.
+	 */
+	String getSlice(Object pc) {
+		DistributedConfiguration conf =
+				(DistributedConfiguration) getConfiguration();
+		String slice =
+				(conf.getDistributionPolicyInstance().distribute(pc, conf
+						.getSliceNames(), this));
+		if (!conf.getSliceNames().contains(slice))
+			throw new UserException(_loc.get("bad-policy-slice", new Object[] {
+					conf.getDistributionPolicyInstance().getClass().getName(),
+					slice, pc, conf.getSliceNames() }));
+		return slice;
+	}
+}

Modified: labs/fluid/slice/src/main/java/org/apache/openjpa/slice/DistributedConfiguration.java
URL: http://svn.apache.org/viewvc/labs/fluid/slice/src/main/java/org/apache/openjpa/slice/DistributedConfiguration.java?rev=606972&r1=606971&r2=606972&view=diff
==============================================================================
--- labs/fluid/slice/src/main/java/org/apache/openjpa/slice/DistributedConfiguration.java (original)
+++ labs/fluid/slice/src/main/java/org/apache/openjpa/slice/DistributedConfiguration.java Wed Dec 26 15:43:13 2007
@@ -23,9 +23,9 @@
 
 /**
  * A configuration for multiple data stores, each referred as <em>slice</em>.
- * This configuration allows each underlying slices be configured with their
+ * This configuration allows each underlying slice be configured with its
  * own specific configuration properties such as JDBC Driver or connection
- * user/password etc. 
+ * user/password etc. <br>
  * This configuration also extends by adding a {@link DistributionPolicy 
  * DistributionPolicy} that governs how new instances be distributed
  * among the slices.

Modified: labs/fluid/slice/src/main/java/org/apache/openjpa/slice/ProductDerivation.java
URL: http://svn.apache.org/viewvc/labs/fluid/slice/src/main/java/org/apache/openjpa/slice/ProductDerivation.java?rev=606972&r1=606971&r2=606972&view=diff
==============================================================================
--- labs/fluid/slice/src/main/java/org/apache/openjpa/slice/ProductDerivation.java (original)
+++ labs/fluid/slice/src/main/java/org/apache/openjpa/slice/ProductDerivation.java Wed Dec 26 15:43:13 2007
@@ -20,15 +20,11 @@
 
 import java.security.AccessController;
 import java.util.Map;
-import java.util.Properties;
 
 import org.apache.openjpa.conf.OpenJPAProductDerivation;
 import org.apache.openjpa.lib.conf.AbstractProductDerivation;
-import org.apache.openjpa.lib.conf.Configuration;
-import org.apache.openjpa.lib.conf.Configurations;
 import org.apache.openjpa.lib.util.J2DoPrivHelper;
 import org.apache.openjpa.slice.jdbc.DistributedJDBCBrokerFactory;
-import org.apache.openjpa.slice.jdbc.DistributedJDBCConfigurationImpl;
 
 /**
  * Derives configuration for Slice.<br>

Modified: labs/fluid/slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedDataSource.java
URL: http://svn.apache.org/viewvc/labs/fluid/slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedDataSource.java?rev=606972&r1=606971&r2=606972&view=diff
==============================================================================
--- labs/fluid/slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedDataSource.java (original)
+++ labs/fluid/slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedDataSource.java Wed Dec 26 15:43:13 2007
@@ -42,8 +42,6 @@
 	
 	public DistributedDataSource(List<DataSource> dataSources) {
 		super(dataSources.get(0));
-		if (dataSources == null || dataSources.isEmpty())
-			throw new NullPointerException();
 		real = dataSources;
 		master = dataSources.get(0);
 	}

Modified: labs/fluid/slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedJDBCBrokerFactory.java
URL: http://svn.apache.org/viewvc/labs/fluid/slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedJDBCBrokerFactory.java?rev=606972&r1=606971&r2=606972&view=diff
==============================================================================
--- labs/fluid/slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedJDBCBrokerFactory.java (original)
+++ labs/fluid/slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedJDBCBrokerFactory.java Wed Dec 26 15:43:13 2007
@@ -18,6 +18,7 @@
  */
 package org.apache.openjpa.slice.jdbc;
 
+import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
 import org.apache.openjpa.jdbc.kernel.JDBCBrokerFactory;
 import org.apache.openjpa.kernel.Bootstrap;
 import org.apache.openjpa.kernel.StoreManager;
@@ -43,6 +44,40 @@
 				new DistributedJDBCConfigurationImpl(cp);
 		cp.setInto(conf);
 		return new DistributedJDBCBrokerFactory(conf);
+	}
+
+	/**
+	 * Factory method for obtaining a possibly-pooled factory from properties.
+	 * Invoked from {@link Bootstrap#getBrokerFactory}.
+	 */
+	public static JDBCBrokerFactory getInstance(ConfigurationProvider cp) {
+		DistributedJDBCBrokerFactory factory =
+				(DistributedJDBCBrokerFactory) getPooledFactory(cp
+						.getProperties());
+		if (factory != null)
+			return factory;
+
+		factory = newInstance(cp);
+		factory.pool();
+		return factory;
+	}
+
+	/**
+	 * Factory method for constructing a factory from a configuration.
+	 */
+	public static synchronized JDBCBrokerFactory getInstance(
+			JDBCConfiguration conf) {
+		DistributedJDBCBrokerFactory factory =
+				(DistributedJDBCBrokerFactory) getPooledFactory(conf
+						.toProperties(false));
+		if (factory != null)
+			return factory;
+
+		factory =
+				new DistributedJDBCBrokerFactory(
+						(DistributedJDBCConfiguration) conf);
+		factory.pool();
+		return factory;
 	}
 
 	public DistributedJDBCBrokerFactory(DistributedJDBCConfiguration conf) {

Modified: labs/fluid/slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedJDBCConfigurationImpl.java
URL: http://svn.apache.org/viewvc/labs/fluid/slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedJDBCConfigurationImpl.java?rev=606972&r1=606971&r2=606972&view=diff
==============================================================================
--- labs/fluid/slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedJDBCConfigurationImpl.java (original)
+++ labs/fluid/slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedJDBCConfigurationImpl.java Wed Dec 26 15:43:13 2007
@@ -48,6 +48,7 @@
 import org.apache.openjpa.lib.jdbc.DecoratingDataSource;
 import org.apache.openjpa.lib.log.Log;
 import org.apache.openjpa.lib.util.Localizer;
+import org.apache.openjpa.slice.DistributedBrokerImpl;
 import org.apache.openjpa.slice.DistributionPolicy;
 import org.apache.openjpa.util.UserException;
 
@@ -78,6 +79,7 @@
 	 */
 	public DistributedJDBCConfigurationImpl(ConfigurationProvider cp) {
 		super(true, false);
+		brokerPlugin.setString(DistributedBrokerImpl.class.getName());
 		distributionPolicyPlugin = addPlugin("DistributionPolicy", true);
 		distributionPolicyPlugin.setDynamic(true);
 		lenient = addBoolean("Lenient");

Added: labs/fluid/slice/src/main/resources/org/apache/openjpa/slice/localizer.properties
URL: http://svn.apache.org/viewvc/labs/fluid/slice/src/main/resources/org/apache/openjpa/slice/localizer.properties?rev=606972&view=auto
==============================================================================
--- labs/fluid/slice/src/main/resources/org/apache/openjpa/slice/localizer.properties (added)
+++ labs/fluid/slice/src/main/resources/org/apache/openjpa/slice/localizer.properties Wed Dec 26 15:43:13 2007
@@ -0,0 +1,4 @@
+bad-policy-slice:Distribution policy "{0}" has returned invalid slice \
+	"{1}" for "{2}". The valid slices are {3}. This error may happen \
+	when one or more of the originally configured slices are unavailable \
+	and Lenient property is set to true.
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org