You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by tv...@apache.org on 2007/01/06 12:46:57 UTC
svn commit: r493449 - in /db/torque:
runtime/trunk/src/java/org/apache/torque/
runtime/trunk/src/java/org/apache/torque/avalon/
runtime/trunk/src/java/org/apache/torque/util/
templates/trunk/src/templates/om/
Author: tv
Date: Sat Jan 6 03:46:50 2007
New Revision: 493449
URL: http://svn.apache.org/viewvc?view=rev&rev=493449
Log:
- Centralized the building and caching of MapBuilders
- Deprecated the BasePeer method and point to the related Torque method
- Changed the Peer template to reflect this
Modified:
db/torque/runtime/trunk/src/java/org/apache/torque/Torque.java
db/torque/runtime/trunk/src/java/org/apache/torque/TorqueInstance.java
db/torque/runtime/trunk/src/java/org/apache/torque/avalon/Torque.java
db/torque/runtime/trunk/src/java/org/apache/torque/util/BasePeer.java
db/torque/templates/trunk/src/templates/om/Peer.vm
Modified: db/torque/runtime/trunk/src/java/org/apache/torque/Torque.java
URL: http://svn.apache.org/viewvc/db/torque/runtime/trunk/src/java/org/apache/torque/Torque.java?view=diff&rev=493449&r1=493448&r2=493449
==============================================================================
--- db/torque/runtime/trunk/src/java/org/apache/torque/Torque.java (original)
+++ db/torque/runtime/trunk/src/java/org/apache/torque/Torque.java Sat Jan 6 03:46:50 2007
@@ -26,6 +26,7 @@
import org.apache.torque.adapter.DB;
import org.apache.torque.manager.AbstractBaseManager;
import org.apache.torque.map.DatabaseMap;
+import org.apache.torque.map.MapBuilder;
/**
* A static facade wrapper around the Torque implementation (which is in
@@ -261,6 +262,31 @@
getInstance().registerMapBuilder(className);
}
+ /**
+ * Register a MapBuilder
+ *
+ * @param builder the instance of the MapBuilder
+ *
+ */
+ public static void registerMapBuilder(MapBuilder builder)
+ {
+ getInstance().registerMapBuilder(builder);
+ }
+
+ /**
+ * Get a MapBuilder
+ *
+ * @param className of the MapBuilder
+ * @return A MapBuilder, not null
+ * @throws TorqueException if the Map Builder cannot be instantiated
+ *
+ */
+ public static MapBuilder getMapBuilder(String className)
+ throws TorqueException
+ {
+ return getInstance().getMapBuilder(className);
+ }
+
/**
* This method returns a Connection from the default pool.
*
Modified: db/torque/runtime/trunk/src/java/org/apache/torque/TorqueInstance.java
URL: http://svn.apache.org/viewvc/db/torque/runtime/trunk/src/java/org/apache/torque/TorqueInstance.java?view=diff&rev=493449&r1=493448&r2=493449
==============================================================================
--- db/torque/runtime/trunk/src/java/org/apache/torque/TorqueInstance.java (original)
+++ db/torque/runtime/trunk/src/java/org/apache/torque/TorqueInstance.java Sat Jan 6 03:46:50 2007
@@ -21,11 +21,9 @@
import java.sql.Connection;
import java.sql.SQLException;
-import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
-import java.util.List;
import java.util.Map;
import org.apache.commons.configuration.Configuration;
@@ -38,9 +36,9 @@
import org.apache.torque.dsfactory.DataSourceFactory;
import org.apache.torque.manager.AbstractBaseManager;
import org.apache.torque.map.DatabaseMap;
+import org.apache.torque.map.MapBuilder;
import org.apache.torque.oid.IDBroker;
import org.apache.torque.oid.IDGeneratorFactory;
-import org.apache.torque.util.BasePeer;
/**
* The core of Torque's implementation. Both the classic {@link
@@ -100,7 +98,7 @@
* are serialized then unserialized prior to Torque being reinitialized.
* This condition exists in a normal catalina restart.
*/
- private List mapBuilders = null;
+ private Map mapBuilderCache = null;
/**
* Creates a new instance with default configuration.
@@ -157,13 +155,34 @@
initAdapters(conf);
initDataSourceFactories(conf);
- for (Iterator i = mapBuilders.iterator(); i.hasNext();)
+ // re-build any MapBuilders that may have gone lost during serialization
+ synchronized (mapBuilderCache)
{
- //this will add any maps in this builder to the proper database map
- BasePeer.getMapBuilder((String) i.next());
+ for (Iterator i = mapBuilderCache.entrySet().iterator(); i.hasNext();)
+ {
+ Map.Entry entry = (Map.Entry)i.next();
+
+ if (null == entry.getValue())
+ {
+ try
+ {
+ // create and build the MapBuilder
+ MapBuilder builder = (MapBuilder) Class.forName((String) entry.getKey()).newInstance();
+
+ if (!builder.isBuilt())
+ {
+ builder.doBuild();
+ }
+
+ entry.setValue(builder);
+ }
+ catch (Exception e)
+ {
+ throw new TorqueException(e);
+ }
+ }
+ }
}
- // any further mapBuilders will be called/built on demand
- mapBuilders = null;
// setup manager mappings
initManagerMappings(conf);
@@ -723,7 +742,7 @@
*/
private void resetConfiguration()
{
- mapBuilders = Collections.synchronizedList(new ArrayList());
+ mapBuilderCache = Collections.synchronizedMap(new HashMap());
managers = new HashMap();
isInit = false;
}
@@ -770,9 +789,68 @@
*/
public void registerMapBuilder(String className)
{
- mapBuilders.add(className);
+ mapBuilderCache.put(className, null);
}
+ /**
+ * Register a MapBuilder
+ *
+ * @param builder the instance of the MapBuilder
+ *
+ */
+ public void registerMapBuilder(MapBuilder builder)
+ {
+ mapBuilderCache.put(builder.getClass().getName(), builder);
+ }
+
+ /**
+ * Get a MapBuilder
+ *
+ * @param className of the MapBuilder
+ * @return A MapBuilder, not null
+ * @throws TorqueException if the Map Builder cannot be instantiated
+ *
+ */
+ public MapBuilder getMapBuilder(String className)
+ throws TorqueException
+ {
+ try
+ {
+ MapBuilder mb = (MapBuilder)mapBuilderCache.get(className);
+
+ if (mb == null)
+ {
+ mb = (MapBuilder) Class.forName(className).newInstance();
+ // Cache the MapBuilder before it is built.
+ mapBuilderCache.put(className, mb);
+ }
+
+ if (mb.isBuilt())
+ {
+ return mb;
+ }
+
+ try
+ {
+ mb.doBuild();
+ }
+ catch (Exception e)
+ {
+ // remove the MapBuilder from the cache if it can't be built correctly
+ mapBuilderCache.remove(className);
+ throw e;
+ }
+
+ return mb;
+ }
+ catch (Exception e)
+ {
+ log.error("getMapBuilder failed trying to instantiate: "
+ + className, e);
+ throw new TorqueException(e);
+ }
+ }
+
/**
* This method returns a Connection from the default pool.
*
Modified: db/torque/runtime/trunk/src/java/org/apache/torque/avalon/Torque.java
URL: http://svn.apache.org/viewvc/db/torque/runtime/trunk/src/java/org/apache/torque/avalon/Torque.java?view=diff&rev=493449&r1=493448&r2=493449
==============================================================================
--- db/torque/runtime/trunk/src/java/org/apache/torque/avalon/Torque.java (original)
+++ db/torque/runtime/trunk/src/java/org/apache/torque/avalon/Torque.java Sat Jan 6 03:46:50 2007
@@ -28,6 +28,7 @@
import org.apache.torque.adapter.DB;
import org.apache.torque.manager.AbstractBaseManager;
import org.apache.torque.map.DatabaseMap;
+import org.apache.torque.map.MapBuilder;
/**
* Avalon role interface for Torque.
@@ -112,6 +113,25 @@
*/
void registerMapBuilder(String className);
+ /**
+ * Register a MapBuilder
+ *
+ * @param builder the instance of the MapBuilder
+ *
+ */
+ void registerMapBuilder(MapBuilder builder);
+
+ /**
+ * Get a MapBuilder
+ *
+ * @param className of the MapBuilder
+ * @return A MapBuilder, not null
+ * @throws TorqueException if the Map Builder cannot be instantiated
+ *
+ */
+ MapBuilder getMapBuilder(String className)
+ throws TorqueException;
+
/**
* This method returns a Connection from the default pool.
*
Modified: db/torque/runtime/trunk/src/java/org/apache/torque/util/BasePeer.java
URL: http://svn.apache.org/viewvc/db/torque/runtime/trunk/src/java/org/apache/torque/util/BasePeer.java?view=diff&rev=493449&r1=493448&r2=493449
==============================================================================
--- db/torque/runtime/trunk/src/java/org/apache/torque/util/BasePeer.java (original)
+++ db/torque/runtime/trunk/src/java/org/apache/torque/util/BasePeer.java Sat Jan 6 03:46:50 2007
@@ -27,7 +27,6 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
-import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -91,9 +90,6 @@
/** Classes that implement this class should override this value. */
public static final String TABLE_NAME = "TABLE_NAME";
- /** Hashtable that contains the cached mapBuilders. */
- private static Hashtable mapBuilders = new Hashtable(5);
-
/** the log */
protected static final Log log = LogFactory.getLog(BasePeer.class);
@@ -1333,60 +1329,17 @@
* This method returns the MapBuilder specified in the name
* parameter. You should pass in the full path to the class, ie:
* org.apache.torque.util.db.map.TurbineMapBuilder. The
- * MapBuilder instances are cached in this class for speed.
+ * MapBuilder instances are cached in the TorqueInstance for speed.
*
* @param name name of the MapBuilder
* @return A MapBuilder, not null
* @throws TorqueException if the Map Builder cannot be instantiated
+ * @deprecated Use Torque.getMapBuilder(name) instead
*/
public static MapBuilder getMapBuilder(String name)
throws TorqueException
{
- synchronized (mapBuilders)
- {
- try
- {
- MapBuilder mb = (MapBuilder) mapBuilders.get(name);
-
- if (mb == null)
- {
- mb = (MapBuilder) Class.forName(name).newInstance();
- // Cache the MapBuilder before it is built.
- mapBuilders.put(name, mb);
- }
-
- // Build the MapBuilder in its own synchronized block to
- // avoid locking up the whole Hashtable while doing so.
- // Note that *all* threads need to do a sync check on isBuilt()
- // to avoid grabing an uninitialized MapBuilder. This, however,
- // is a relatively fast operation.
-
- if (mb.isBuilt())
- {
- return mb;
- }
-
- try
- {
- mb.doBuild();
- }
- catch (Exception e)
- {
- // need to think about whether we'd want to remove
- // the MapBuilder from the cache if it can't be
- // built correctly...? pgo
- throw e;
- }
-
- return mb;
- }
- catch (Exception e)
- {
- log.error("BasePeer.MapBuilder failed trying to instantiate: "
- + name, e);
- throw new TorqueException(e);
- }
- }
+ return Torque.getMapBuilder(name);
}
/**
Modified: db/torque/templates/trunk/src/templates/om/Peer.vm
URL: http://svn.apache.org/viewvc/db/torque/templates/trunk/src/templates/om/Peer.vm?view=diff&rev=493449&r1=493448&r2=493449
==============================================================================
--- db/torque/templates/trunk/src/templates/om/Peer.vm (original)
+++ db/torque/templates/trunk/src/templates/om/Peer.vm Sat Jan 6 03:46:50 2007
@@ -86,11 +86,12 @@
* @return the map builder for this peer
* @throws TorqueException Any exceptions caught during processing will be
* rethrown wrapped into a TorqueException.
+ * @deprecated Torque.getMapBuilder(${table.JavaName}MapBuilder.CLASS_NAME) instead
*/
public static MapBuilder getMapBuilder()
throws TorqueException
{
- return getMapBuilder(${table.JavaName}MapBuilder.CLASS_NAME);
+ return Torque.getMapBuilder(${table.JavaName}MapBuilder.CLASS_NAME);
}
#foreach ($col in $table.Columns)
@@ -116,9 +117,9 @@
{
try
{
- getMapBuilder(${table.JavaName}MapBuilder.CLASS_NAME);
+ Torque.getMapBuilder(${table.JavaName}MapBuilder.CLASS_NAME);
}
- catch (Exception e)
+ catch (TorqueException e)
{
log.error("Could not initialize Peer", e);
throw new RuntimeException(e);
---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org