You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2004/12/18 14:57:02 UTC
cvs commit: db-ojb/src/java/org/apache/ojb/broker OJB.java PersistenceConfiguration.java
arminw 2004/12/18 05:57:02
Modified: src/java/org/apache/ojb/broker/metadata MetadataManager.java
src/java/org/apache/ojb/broker OJB.java
PersistenceConfiguration.java
Log:
bind ConnectionFactory to PersistenceConfiguration
improve handling of PersistenceConfiguration/Descriptor
Revision Changes Path
1.24 +54 -10 db-ojb/src/java/org/apache/ojb/broker/metadata/MetadataManager.java
Index: MetadataManager.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/MetadataManager.java,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -r1.23 -r1.24
--- MetadataManager.java 14 Nov 2004 09:36:10 -0000 1.23
+++ MetadataManager.java 18 Dec 2004 13:57:02 -0000 1.24
@@ -17,9 +17,12 @@
import java.io.FileNotFoundException;
import java.io.InputStream;
+import java.util.Collections;
+import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
+import java.util.Map;
import org.apache.commons.lang.SerializationUtils;
import org.apache.ojb.broker.PBKey;
@@ -118,6 +121,9 @@
*
* @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
* @version $Id$
+ *
+ *
+ * TODO: This class needs adaptation to new repository.dtd with new persistence-configuration element
*/
public class MetadataManager implements NeedsInitialization
{
@@ -142,14 +148,61 @@
private ConnectionRepository connectionRepository;
private boolean enablePerThreadChanges;
private PBKey defaultPBKey;
+ private Map persistenceConfigurationDescriptorMap;
public MetadataManager(PersistentFieldFactory fieldFactory)
{
+ this.persistenceConfigurationDescriptorMap = Collections.synchronizedMap(new HashMap());
this.fieldFactory = fieldFactory;
this.persistor = getRepositoryPersistor();
}
/**
+ * Returns the {@link PersistenceConfigurationDescriptor} of the given {@link org.apache.ojb.broker.PBKey}.
+ *
+ * @param key The key to look for. If key is <code>null</code> the default key was used.
+ * @return
+ */
+ public PersistenceConfigurationDescriptor getConfigurationFor(PBKey key)
+ {
+ /*
+ TODO: Implement this method, current impl is an early alpha version of the new upcoming metadata handling.
+ */
+ if(key == null)
+ {
+ key = defaultPBKey;
+ }
+ PersistenceConfigurationDescriptor pcd = (PersistenceConfigurationDescriptor) persistenceConfigurationDescriptorMap.get(key);
+ // TODO: this is only a workaround!!!
+ if(pcd == null)
+ {
+ pcd = new PersistenceConfigurationDescriptor(key, getRepository(), connectionRepository().getDescriptor(key));
+ }
+ return pcd;
+ }
+
+ /**
+ * Add a new {@link PersistenceConfigurationDescriptor} for use by OJB.
+ * @param pcd The descriptor to use. The {@link PBKey}
+ */
+ public void addPersistenceConfiguration(PersistenceConfigurationDescriptor pcd)
+ {
+ if(persistenceConfigurationDescriptorMap.containsKey(pcd.getKey()))
+ {
+ throw new MetadataException("Can't add persistence configuration, duplicate key found, " + pcd.getKey());
+ }
+ else
+ {
+ persistenceConfigurationDescriptorMap.put(pcd.getKey(), pcd);
+ }
+ }
+
+ public PersistenceConfigurationDescriptor removePersistenceConfiguration(PBKey key)
+ {
+ return (PersistenceConfigurationDescriptor) persistenceConfigurationDescriptorMap.remove(key);
+ }
+
+ /**
* Returns the repository path.
*
* @return The repository path
@@ -242,15 +295,6 @@
{
// we use the setup'd OJB instance
return PersistenceBrokerFactory.getOjb().getMetadataManager();
- }
-
- public PersistenceConfigurationDescriptor getConfigurationFor(PBKey key)
- {
- /*
- TODO: Implement this method, current impl is an early alpha version of the new upcoming metadata handling.
- */
- PersistenceConfigurationDescriptor pcd = new PersistenceConfigurationDescriptor(key, getRepository(), connectionRepository().getDescriptor(key));
- return pcd;
}
/**
1.8 +41 -15 db-ojb/src/java/org/apache/ojb/broker/OJB.java
Index: OJB.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/OJB.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- OJB.java 9 Dec 2004 17:12:14 -0000 1.7
+++ OJB.java 18 Dec 2004 13:57:02 -0000 1.8
@@ -49,7 +49,15 @@
{
/** Default filename of the OJB properties file */
public static final String DEFAULT_PROPERTIES_FILE = "OJB.properties";
+ /**
+ * Force INSERT on {@link PersistenceBroker#store(Object, org.apache.ojb.broker.util.ObjectModification)}
+ * method.
+ */
public static final ObjectModification INSERT = ObjectModificationImpl.INSERT;
+ /**
+ * Force UPDATE on {@link PersistenceBroker#store(Object, org.apache.ojb.broker.util.ObjectModification)}
+ * method.
+ */
public static final ObjectModification UPDATE = ObjectModificationImpl.UPDATE;
private Logger log = LoggerFactory.getLogger(OJB.class);
@@ -205,11 +213,11 @@
try
{
- conf.destroy();
+ conf.close();
}
catch (Exception e)
{
- log.error("Error while destroy PersistenceConfiguration for key " + (conf != null ? conf.getKey() : null), e);
+ log.error("Error while close PersistenceConfiguration for key " + (conf != null ? conf.getKey() : null), e);
}
}
log.info("Shutdown of OJB finished");
@@ -221,6 +229,7 @@
/**
* Get the default {@link org.apache.ojb.broker.PersistenceConfiguration}. If none
* exists but a default connection descriptor exists, a new configuration will be created.
+ * A convenience method for {@link #getConfiguration(PBKey)}.
*
* @return The configuration
*/
@@ -233,29 +242,39 @@
* Get the {@link org.apache.ojb.broker.PersistenceConfiguration} for given key. If none
* exists but a connection descriptor exists for this key, a new configuration will be created.
*
- * @param key The key of the searched {@link org.apache.ojb.broker.PersistenceConfiguration}.
+ * @param key The key of the searched {@link org.apache.ojb.broker.PersistenceConfiguration}
+ * or <code>null</code> if a default configuration was set.
* @return The associated configuration
*/
public PersistenceConfiguration getConfiguration(PBKey key) throws ConfigurationException
{
initDynamicSystem();
- PBKey realKey = key;
+ PBKey realKey = key;
PersistenceConfiguration conf = null;
if (realKey == null)
{
realKey = metadataManager.getDefaultPBKey();
}
+ else
+ {
+ // if User name was null, but the jcdAlias exists, OJB try to
+ // find a valid PBKey with set User/Password in xml configuration files
+ // by searching jdbcConnectionDescriptor
+ realKey = BrokerHelper.crossCheckPBKey(this, key);
+ }
conf = (PersistenceConfiguration) configurationMap.get(realKey);
if (conf == null)
{
- conf = createAndAddNewPersistenceConfiguration(realKey);
+ conf = createPersistenceConfiguration(realKey);
+ // buffer the new configuration for reuse
+ addPersistenceConfiguration(conf);
}
return conf;
}
- private synchronized PersistenceConfiguration createAndAddNewPersistenceConfiguration(PBKey key) throws ConfigurationException
+ private synchronized PersistenceConfiguration createPersistenceConfiguration(PBKey key) throws ConfigurationException
{
// check again, make sure that no other thread has created same PC
PersistenceConfiguration conf = (PersistenceConfiguration) configurationMap.get(key);
@@ -286,9 +305,6 @@
CacheManager cm = new CacheManager(ocd);
cm.bindToModel(conf.getModel());
conf.setCacheManager(cm);
-
- // buffer the new configuration for reuse
- addPersistenceConfiguration(conf);
return conf;
}
@@ -299,7 +315,7 @@
* @throws ConfigurationException Will be thrown if the persistence configuration instance is
* <em>null</em> or as no valid key or is already defined in system.
*/
- public void addPersistenceConfiguration(PersistenceConfiguration pc) throws ConfigurationException
+ private void addPersistenceConfiguration(PersistenceConfiguration pc) throws ConfigurationException
{
initDynamicSystem();
if (pc == null)
@@ -322,18 +338,28 @@
}
/**
- * Remove the specified {@link org.apache.ojb.broker.PersistenceConfiguration}.
+ * Remove the specified {@link org.apache.ojb.broker.PersistenceConfiguration}, all resources used
+ * used by the specified configuration will be closed.
+ * <br/>
+ * NOTE: Please handle with care!
*
* @param key The {@link PBKey} to identify the configuration.
- * @return The removed configuration or <em>null</em> if not found.
+ * @return The associated descriptor or <em>null</em> if configuration could not be found.
*/
- public PersistenceConfiguration removePersistenceConfiguration(PBKey key)
+ public PersistenceConfigurationDescriptor removePersistenceConfiguration(PBKey key)
{
initDynamicSystem();
+ PersistenceConfigurationDescriptor pcd = null;
synchronized (dummy)
{
- return (PersistenceConfiguration) configurationMap.remove(key);
+ PersistenceConfiguration pc = (PersistenceConfiguration) configurationMap.remove(key);
+ if(pc != null)
+ {
+ pcd = pc.getPersistenceConfigurationDescriptor();
+ pc.close();
+ }
}
+ return pcd;
}
/**
1.3 +66 -40 db-ojb/src/java/org/apache/ojb/broker/PersistenceConfiguration.java
Index: PersistenceConfiguration.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/PersistenceConfiguration.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- PersistenceConfiguration.java 14 Nov 2004 09:33:37 -0000 1.2
+++ PersistenceConfiguration.java 18 Dec 2004 13:57:02 -0000 1.3
@@ -2,16 +2,16 @@
import java.util.HashMap;
-import org.apache.ojb.broker.OJB;
-import org.apache.ojb.broker.PBKey;
+import org.apache.ojb.broker.accesslayer.ConnectionFactory;
import org.apache.ojb.broker.accesslayer.RowReader;
import org.apache.ojb.broker.accesslayer.StatementsForClassIF;
import org.apache.ojb.broker.accesslayer.sql.SqlGenerator;
+import org.apache.ojb.broker.cache.CacheManager;
import org.apache.ojb.broker.core.PersistenceBrokerFactoryIF;
import org.apache.ojb.broker.core.configuration.ComponentContainer;
import org.apache.ojb.broker.core.factory.ObjectFactory;
-import org.apache.ojb.broker.cache.CacheManager;
import org.apache.ojb.broker.metadata.ClassDescriptor;
+import org.apache.ojb.broker.metadata.ConnectionFactoryDescriptor;
import org.apache.ojb.broker.metadata.DescriptorRepository;
import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
import org.apache.ojb.broker.metadata.MetadataException;
@@ -41,35 +41,56 @@
*/
public class PersistenceConfiguration
{
- /** Our own sub-subContainer */
+ /**
+ * Our own sub-subContainer
+ */
private ComponentContainer subContainer;
private PersistenceConfigurationDescriptor pcd;
private OJB ojb;
private PersistenceBrokerFactoryIF persistenceBrokerFactory;
private CacheManager cacheManager;
+ private ConnectionFactory connectionFactory;
private ObjectFactory objectFactory;
- /** The sql generator for this configuration */
+ /**
+ * The sql generator for this configuration
+ */
private SqlGenerator sqlGenerator;
- /** Cache for class-statement objects keyed per class descriptor */
+ /**
+ * Cache for class-statement objects keyed per class descriptor
+ */
private HashMap statementsForClass = new HashMap();
- /** Cache for row-reader objects keyed per class descriptor */
+ /**
+ * Cache for row-reader objects keyed per class descriptor
+ */
private HashMap rowReaderForClass = new HashMap();
public PersistenceConfiguration(ComponentContainer container, PersistenceConfigurationDescriptor pcd)
{
this.pcd = pcd;
- this.ojb = (OJB)container.getInstance(OJB.class);
+ this.ojb = (OJB) container.getInstance(OJB.class);
subContainer = container.createChildContainer();
subContainer.setSingletonInstance(this);
subContainer.setSingletonInstance(pcd.getJdbcConnectionDescriptor());
subContainer.setSingletonInstance(pcd.getJdbcConnectionDescriptor().getPlatform());
- persistenceBrokerFactory = (PersistenceBrokerFactoryIF)subContainer.getSingletonInstance(PersistenceBrokerFactoryIF.class);
+ ConnectionFactoryDescriptor cfd = pcd.getJdbcConnectionDescriptor().getConnectionFactoryDescriptor();
+ if((cfd != null) && (cfd.getConnectionFactoryClass() != null))
+ {
+ subContainer.setImplementationClass(ConnectionFactory.class, cfd.getConnectionFactoryClass());
+ }
+ connectionFactory = (ConnectionFactory) subContainer.getSingletonInstance(ConnectionFactory.class);
+
+ persistenceBrokerFactory = (PersistenceBrokerFactoryIF) subContainer.getSingletonInstance(PersistenceBrokerFactoryIF.class);
// we're registering the sql generator instance as a singleton so that any pb instance
// created in the context of this pc will get the same sql generator instance
- sqlGenerator = (SqlGenerator)subContainer.getSingletonInstance(SqlGenerator.class);
- objectFactory = (ObjectFactory)subContainer.getSingletonInstance(ObjectFactory.class);
+ sqlGenerator = (SqlGenerator) subContainer.getSingletonInstance(SqlGenerator.class);
+ objectFactory = (ObjectFactory) subContainer.getSingletonInstance(ObjectFactory.class);
+ }
+
+ public PersistenceConfigurationDescriptor getPersistenceConfigurationDescriptor()
+ {
+ return pcd;
}
public PBKey getKey()
@@ -84,14 +105,14 @@
/**
* Returns the component container.
- *
+ *
* @return The container
*/
public ComponentContainer getComponentContainer()
{
return subContainer;
}
-
+
public DescriptorRepository getModel()
{
/*
@@ -107,6 +128,11 @@
return pcd.getJdbcConnectionDescriptor();
}
+ public ConnectionFactory getConnectionFactory()
+ {
+ return connectionFactory;
+ }
+
public CacheManager getCacheManager()
{
return cacheManager;
@@ -116,10 +142,10 @@
{
this.cacheManager = cacheManager;
}
-
+
/**
* Returns the object factory.
- *
+ *
* @return The object factory
*/
public ObjectFactory getObjectFactory()
@@ -129,29 +155,28 @@
/**
* Returns the statement generator for the given class descriptor.
- *
+ *
* @param classDesc The class descriptor
* @return The statement generator object
*/
public StatementsForClassIF getStatementsForClass(ClassDescriptor classDesc)
{
- StatementsForClassIF result = (StatementsForClassIF)statementsForClass.get(classDesc);
+ StatementsForClassIF result = (StatementsForClassIF) statementsForClass.get(classDesc);
- if (result == null)
+ if(result == null)
{
- Platform platform = getJdbcConnectionDescriptor().getPlatform();
- Double jdbcLevel = new Double(getJdbcConnectionDescriptor().getJdbcLevel());
-
+ Platform platform = getJdbcConnectionDescriptor().getPlatform();
+ Double jdbcLevel = new Double(getJdbcConnectionDescriptor().getJdbcLevel());
+
try
{
- result = (StatementsForClassIF)ClassHelper.newInstance(
- subContainer.getImplementationClass(StatementsForClassIF.class),
- new Class[]{ Platform.class, ClassDescriptor.class, SqlGenerator.class, Double.class },
- new Object[]{ platform, classDesc, sqlGenerator, jdbcLevel });
+ result = (StatementsForClassIF) ClassHelper.newInstance(subContainer.getImplementationClass(StatementsForClassIF.class),
+ new Class[]{Platform.class, ClassDescriptor.class, SqlGenerator.class, Double.class},
+ new Object[]{platform, classDesc, sqlGenerator, jdbcLevel});
}
- catch (Exception ex)
+ catch(Exception ex)
{
- throw new OJBRuntimeException("Failed to create statement generator for class "+classDesc.getClassNameOfObject(), ex);
+ throw new OJBRuntimeException("Failed to create statement generator for class " + classDesc.getClassNameOfObject(), ex);
}
statementsForClass.put(classDesc, result);
}
@@ -160,15 +185,15 @@
/**
* Returns the row reader object for the given class descriptor.
- *
+ *
* @param classDesc The class descriptor
* @return The row reader
*/
public RowReader getRowReaderForClass(ClassDescriptor classDesc)
{
- RowReader reader = (RowReader)rowReaderForClass.get(classDesc);
+ RowReader reader = (RowReader) rowReaderForClass.get(classDesc);
- if (reader == null)
+ if(reader == null)
{
ComponentContainer localContainer = getComponentContainer().createChildContainer();
@@ -176,7 +201,7 @@
// the row reader is probably interested)
localContainer.setSingletonInstance(ClassDescriptor.class, classDesc);
- if (classDesc.getRowReaderClassName() != null)
+ if(classDesc.getRowReaderClassName() != null)
{
Class rowReaderClass;
@@ -184,24 +209,24 @@
{
rowReaderClass = ClassHelper.getClass(classDesc.getRowReaderClassName());
}
- catch (ClassNotFoundException ex)
+ catch(ClassNotFoundException ex)
{
- throw new MetadataException("Could not create RowReader of type "+classDesc.getRowReaderClassName()+
- " that is configured for class descriptor "+classDesc.getClassNameOfObject(), ex);
+ throw new MetadataException("Could not create RowReader of type " + classDesc.getRowReaderClassName() +
+ " that is configured for class descriptor " + classDesc.getClassNameOfObject(), ex);
}
- reader = (RowReader)localContainer.getInstance(rowReaderClass);
+ reader = (RowReader) localContainer.getInstance(rowReaderClass);
}
else
{
- reader = (RowReader)localContainer.getInstance(RowReader.class);
+ reader = (RowReader) localContainer.getInstance(RowReader.class);
}
rowReaderForClass.put(classDesc, reader);
}
return reader;
}
-
+
public PersistenceBroker createPersistenceBroker() throws PBFactoryException
{
return persistenceBrokerFactory.createPersistenceBroker();
@@ -209,13 +234,14 @@
public void releaseAllInstances()
{
+ connectionFactory.releaseAllResources();
persistenceBrokerFactory.releaseAllInstances();
rowReaderForClass.clear();
+ cacheManager.clearCaches();
}
- void destroy()
+ void close()
{
releaseAllInstances();
- cacheManager.clearCaches();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org