You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by da...@apache.org on 2007/10/22 02:44:25 UTC
svn commit: r586980 - in /tapestry/tapestry5/trunk/tapestry-hibernate/src:
main/java/org/apache/tapestry/hibernate/
main/java/org/apache/tapestry/internal/hibernate/
main/resources/org/apache/tapestry/internal/hibernate/
test/java/org/apache/tapestry/i...
Author: dadams
Date: Sun Oct 21 17:44:17 2007
New Revision: 586980
URL: http://svn.apache.org/viewvc?rev=586980&view=rev
Log:
Fixed TAPESTRY-1576: Getting access to the created configuration
Added:
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/ImmutableConfiguration.java
Modified:
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateSessionSource.java
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/HibernateMessages.java
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/HibernateSessionSourceImpl.java
tapestry/tapestry5/trunk/tapestry-hibernate/src/main/resources/org/apache/tapestry/internal/hibernate/HibernateStrings.properties
tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry/internal/hibernate/HibernateSessionSourceImplTest.java
Modified: tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateSessionSource.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateSessionSource.java?rev=586980&r1=586979&r2=586980&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateSessionSource.java (original)
+++ tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateSessionSource.java Sun Oct 21 17:44:17 2007
@@ -32,4 +32,9 @@
/** Returns the SessionFactory from which Hibernate sessions are created. */
SessionFactory getSessionFactory();
+
+ /** Returns the final configuration used to create the {@link SessionFactory}.
+ * The configuration is immutable.
+ */
+ Configuration getConfiguration();
}
Modified: tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/HibernateMessages.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/HibernateMessages.java?rev=586980&r1=586979&r2=586980&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/HibernateMessages.java (original)
+++ tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/HibernateMessages.java Sun Oct 21 17:44:17 2007
@@ -33,4 +33,8 @@
{
return MESSAGES.format("entity-catalog", InternalUtils.joinSorted(entityNames));
}
+
+ static String configurationImmutable() {
+ return MESSAGES.get("configuration-immutable");
+ }
}
Modified: tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/HibernateSessionSourceImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/HibernateSessionSourceImpl.java?rev=586980&r1=586979&r2=586980&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/HibernateSessionSourceImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/HibernateSessionSourceImpl.java Sun Oct 21 17:44:17 2007
@@ -21,17 +21,19 @@
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
+import org.hibernate.cfg.Configuration;
import org.slf4j.Logger;
public class HibernateSessionSourceImpl implements HibernateSessionSource
{
- private SessionFactory _sessionFactory;
+ private final SessionFactory _sessionFactory;
+ private final Configuration _configuration;
public HibernateSessionSourceImpl(Logger logger, List<HibernateConfigurer> hibernateConfigurers)
{
long startTime = System.currentTimeMillis();
- AnnotationConfiguration configuration = new AnnotationConfiguration();
+ Configuration configuration = new AnnotationConfiguration();
for(HibernateConfigurer configurer : hibernateConfigurers)
configurer.configure(configuration);
@@ -39,6 +41,7 @@
long configurationComplete = System.currentTimeMillis();
_sessionFactory = configuration.buildSessionFactory();
+ _configuration = new ImmutableConfiguration(configuration);
long factoryCreated = System.currentTimeMillis();
@@ -61,4 +64,10 @@
{
return _sessionFactory;
}
+
+ public Configuration getConfiguration() {
+ return _configuration;
+ }
+
+
}
Added: tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/ImmutableConfiguration.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/ImmutableConfiguration.java?rev=586980&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/ImmutableConfiguration.java (added)
+++ tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/ImmutableConfiguration.java Sun Oct 21 17:44:17 2007
@@ -0,0 +1,421 @@
+// Copyright 2007 The Apache Software Foundation
+//
+// Licensed 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.tapestry.internal.hibernate;
+
+import java.io.File;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Properties;
+
+import org.dom4j.Document;
+import org.hibernate.HibernateException;
+import org.hibernate.Interceptor;
+import org.hibernate.MappingException;
+import org.hibernate.SessionFactory;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.cfg.Mappings;
+import org.hibernate.cfg.NamingStrategy;
+import org.hibernate.cfg.Settings;
+import org.hibernate.dialect.Dialect;
+import org.hibernate.dialect.function.SQLFunction;
+import org.hibernate.engine.FilterDefinition;
+import org.hibernate.engine.Mapping;
+import org.hibernate.event.EventListeners;
+import org.hibernate.mapping.AuxiliaryDatabaseObject;
+import org.hibernate.mapping.Collection;
+import org.hibernate.mapping.PersistentClass;
+import org.hibernate.proxy.EntityNotFoundDelegate;
+import org.hibernate.tool.hbm2ddl.DatabaseMetadata;
+import org.xml.sax.EntityResolver;
+
+/** Delegates all method calls to another instance.
+ * Any calls that modify state or return objects that are
+ * meant to be used to change state will throw an {@link UnsupportedOperationException}.
+ * This class is specifically final because there are protected methods that
+ * cannot be called on the contained instance (because they are protected).
+ * <p>
+ * Note that this class does not guarantee that the objects returned are mutable
+ * thus changes to the configuration are still possible.
+ */
+@SuppressWarnings("unchecked")
+final class ImmutableConfiguration extends Configuration {
+ private static final long serialVersionUID = -4039250481581260132L;
+
+ private final Configuration _config;
+
+ public ImmutableConfiguration(Configuration configuration) {
+ _config = configuration;
+ }
+
+ @Override
+ protected void add(Document doc) throws MappingException {
+ unsupported();
+ }
+
+ /** Throws an exception. Has a return value for convenience.
+ * @return nothing because it always throws an exception
+ */
+ private <T> T unsupported() {
+ throw new UnsupportedOperationException(HibernateMessages.configurationImmutable());
+ }
+
+ @Override
+ public void addAuxiliaryDatabaseObject(AuxiliaryDatabaseObject object) {
+ unsupported();
+ }
+
+ @Override
+ public Configuration addCacheableFile(File xmlFile) throws MappingException {
+ return unsupported();
+ }
+
+ @Override
+ public Configuration addCacheableFile(String xmlFile) throws MappingException {
+ return unsupported();
+ }
+
+ @Override
+ public Configuration addClass(Class persistentClass) throws MappingException {
+ return unsupported();
+ }
+
+ @Override
+ public Configuration addDirectory(File dir) throws MappingException {
+ return unsupported();
+ }
+
+ @Override
+ public Configuration addDocument(org.w3c.dom.Document doc) throws MappingException {
+ return unsupported();
+ }
+
+ @Override
+ public Configuration addFile(File xmlFile) throws MappingException {
+ return unsupported();
+ }
+
+ @Override
+ public Configuration addFile(String xmlFile) throws MappingException {
+ return unsupported();
+ }
+
+ @Override
+ public void addFilterDefinition(FilterDefinition definition) {
+ unsupported();
+ }
+
+ @Override
+ public Configuration addInputStream(InputStream xmlInputStream) throws MappingException {
+ return unsupported();
+ }
+
+ @Override
+ public Configuration addJar(File jar) throws MappingException {
+ return unsupported();
+ }
+
+ @Override
+ public Configuration addProperties(Properties extraProperties) {
+ return unsupported();
+ }
+
+ @Override
+ public Configuration addResource(String resourceName, ClassLoader classLoader) throws MappingException {
+ return unsupported();
+ }
+
+ @Override
+ public Configuration addResource(String resourceName) throws MappingException {
+ return unsupported();
+ }
+
+ @Override
+ public void addSqlFunction(String functionName, SQLFunction function) {
+ unsupported();
+ }
+
+ @Override
+ public Configuration addURL(URL url) throws MappingException {
+ return unsupported();
+ }
+
+ @Override
+ public Configuration addXML(String xml) throws MappingException {
+ return unsupported();
+ }
+
+ /* Since this is called from the constructor of the superclass, it calls
+ * the superclass method rather than delegating to the contained instance.
+ * We could also just not override the method but for completeness I'll
+ * leave it in.
+ * It's unfortunate that Configuration isn't an interface.
+ */
+ @Override
+ public Mapping buildMapping() {
+ return super.buildMapping();
+ }
+
+ @Override
+ public void buildMappings() {
+ _config.buildMappings();
+ }
+
+ @Override
+ public SessionFactory buildSessionFactory() throws HibernateException {
+ return _config.buildSessionFactory();
+ }
+
+ @Override
+ public Settings buildSettings() throws HibernateException {
+ return _config.buildSettings();
+ }
+
+ @Override
+ public Settings buildSettings(Properties props) throws HibernateException {
+ return _config.buildSettings(props);
+ }
+
+ @Override
+ public Configuration configure() throws HibernateException {
+ return unsupported();
+ }
+
+ @Override
+ public Configuration configure(org.w3c.dom.Document document) throws HibernateException {
+ return unsupported();
+ }
+
+ @Override
+ public Configuration configure(File configFile) throws HibernateException {
+ return unsupported();
+ }
+
+ @Override
+ public Configuration configure(String resource) throws HibernateException {
+ return unsupported();
+ }
+
+ @Override
+ public Configuration configure(URL url) throws HibernateException {
+ return unsupported();
+ }
+
+ @Override
+ public Mappings createMappings() {
+ return unsupported();
+ }
+
+ @Override
+ protected Configuration doConfigure(Document doc) throws HibernateException {
+ return unsupported();
+ }
+
+ @Override
+ protected Configuration doConfigure(InputStream stream, String resourceName) throws HibernateException {
+ return unsupported();
+ }
+
+ @Override
+ protected Document findPossibleExtends() {
+ return unsupported();
+ }
+
+ @Override
+ public String[] generateDropSchemaScript(Dialect dialect) throws HibernateException {
+ return _config.generateDropSchemaScript(dialect);
+ }
+
+ @Override
+ public String[] generateSchemaCreationScript(Dialect dialect) throws HibernateException {
+ return _config.generateSchemaCreationScript(dialect);
+ }
+
+ @Override
+ public String[] generateSchemaUpdateScript(Dialect dialect, DatabaseMetadata databaseMetadata) throws HibernateException {
+ return _config.generateSchemaUpdateScript(dialect, databaseMetadata);
+ }
+
+ @Override
+ public PersistentClass getClassMapping(String entityName) {
+ return _config.getClassMapping(entityName);
+ }
+
+ @Override
+ public Iterator getClassMappings() {
+ return _config.getClassMappings();
+ }
+
+ @Override
+ public Collection getCollectionMapping(String role) {
+ return _config.getCollectionMapping(role);
+ }
+
+ @Override
+ public Iterator getCollectionMappings() {
+ return _config.getCollectionMappings();
+ }
+
+ @Override
+ public EntityNotFoundDelegate getEntityNotFoundDelegate() {
+ return _config.getEntityNotFoundDelegate();
+ }
+
+ @Override
+ public EntityResolver getEntityResolver() {
+ return _config.getEntityResolver();
+ }
+
+ @Override
+ public EventListeners getEventListeners() {
+ return _config.getEventListeners();
+ }
+
+ @Override
+ public Map getFilterDefinitions() {
+ return _config.getFilterDefinitions();
+ }
+
+ @Override
+ public Map getImports() {
+ return _config.getImports();
+ }
+
+ @Override
+ public Interceptor getInterceptor() {
+ return _config.getInterceptor();
+ }
+
+ @Override
+ public Map getNamedQueries() {
+ return _config.getNamedQueries();
+ }
+
+ @Override
+ public Map getNamedSQLQueries() {
+ return _config.getNamedSQLQueries();
+ }
+
+ @Override
+ public NamingStrategy getNamingStrategy() {
+ return _config.getNamingStrategy();
+ }
+
+ @Override
+ public Properties getProperties() {
+ return _config.getProperties();
+ }
+
+ @Override
+ public String getProperty(String propertyName) {
+ return _config.getProperty(propertyName);
+ }
+
+ @Override
+ public Map getSqlFunctions() {
+ return _config.getSqlFunctions();
+ }
+
+ @Override
+ public Map getSqlResultSetMappings() {
+ return _config.getSqlResultSetMappings();
+ }
+
+ @Override
+ public Iterator getTableMappings() {
+ return _config.getTableMappings();
+ }
+
+ @Override
+ public Configuration mergeProperties(Properties properties) {
+ return unsupported();
+ }
+
+ @Override
+ public void setCacheConcurrencyStrategy(String clazz, String concurrencyStrategy, String region) throws MappingException {
+ unsupported();
+ }
+
+ @Override
+ public Configuration setCacheConcurrencyStrategy(String clazz, String concurrencyStrategy) throws MappingException {
+ return unsupported();
+ }
+
+ @Override
+ public void setCollectionCacheConcurrencyStrategy(String collectionRole, String concurrencyStrategy, String region) throws MappingException {
+ unsupported();
+ }
+
+ @Override
+ public Configuration setCollectionCacheConcurrencyStrategy(String collectionRole, String concurrencyStrategy) throws MappingException {
+ return unsupported();
+ }
+
+ @Override
+ public void setEntityNotFoundDelegate(EntityNotFoundDelegate entityNotFoundDelegate) {
+ unsupported();
+ }
+
+ @Override
+ public void setEntityResolver(EntityResolver entityResolver) {
+ unsupported();
+ }
+
+ @Override
+ public Configuration setInterceptor(Interceptor interceptor) {
+ return unsupported();
+ }
+
+ @Override
+ public void setListener(String type, Object listener) {
+ unsupported();
+ }
+
+ @Override
+ public void setListeners(String type, Object[] listeners) {
+ unsupported();
+ }
+
+ @Override
+ public void setListeners(String type, String[] listenerClasses) {
+ unsupported();
+ }
+
+ @Override
+ public Configuration setNamingStrategy(NamingStrategy namingStrategy) {
+ return unsupported();
+ }
+
+ @Override
+ public Configuration setProperties(Properties properties) {
+ return unsupported();
+ }
+
+ @Override
+ public Configuration setProperty(String propertyName, String value) {
+ return unsupported();
+ }
+
+ @Override
+ public void validateSchema(Dialect dialect, DatabaseMetadata databaseMetadata) throws HibernateException {
+ _config.validateSchema(dialect, databaseMetadata);
+ }
+
+ @Override
+ public String toString() {
+ return "ImmutableConfiguration[" + _config + "]";
+ }
+}
Modified: tapestry/tapestry5/trunk/tapestry-hibernate/src/main/resources/org/apache/tapestry/internal/hibernate/HibernateStrings.properties
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/resources/org/apache/tapestry/internal/hibernate/HibernateStrings.properties?rev=586980&r1=586979&r2=586980&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-hibernate/src/main/resources/org/apache/tapestry/internal/hibernate/HibernateStrings.properties (original)
+++ tapestry/tapestry5/trunk/tapestry-hibernate/src/main/resources/org/apache/tapestry/internal/hibernate/HibernateStrings.properties Sun Oct 21 17:44:17 2007
@@ -13,4 +13,5 @@
# limitations under the License.
startup-timing=Hibernate startup: %,d ms to configure, %,d ms overall.
-entity-catalog=Configured Hibernate entities: %s
\ No newline at end of file
+entity-catalog=Configured Hibernate entities: %s
+configuration-immutable=The Hibernate configuration is now immutable since the SessionFactory has already been created.
Modified: tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry/internal/hibernate/HibernateSessionSourceImplTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry/internal/hibernate/HibernateSessionSourceImplTest.java?rev=586980&r1=586979&r2=586980&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry/internal/hibernate/HibernateSessionSourceImplTest.java (original)
+++ tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry/internal/hibernate/HibernateSessionSourceImplTest.java Sun Oct 21 17:44:17 2007
@@ -16,6 +16,7 @@
import java.util.Arrays;
import java.util.Collection;
+import java.util.Collections;
import java.util.List;
import org.apache.tapestry.hibernate.HibernateConfigurer;
@@ -26,6 +27,7 @@
import org.apache.tapestry.test.TapestryTestCase;
import org.example.app0.entities.User;
import org.hibernate.Session;
+import org.hibernate.cfg.Configuration;
import org.hibernate.metadata.ClassMetadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -61,4 +63,28 @@
verify();
}
+
+ @Test
+ public void get_configuration() {
+ HibernateConfigurer configurer = new HibernateConfigurer() {
+ public void configure(Configuration configuration) {
+ configuration.setProperty("foo", "bar");
+ configuration.configure();
+ }
+ };
+ HibernateSessionSource source = new HibernateSessionSourceImpl(_log, Arrays.asList(configurer));
+
+ Configuration config = source.getConfiguration();
+ assertNotNull(config);
+ assertEquals("bar", config.getProperty("foo"));
+
+ // configuration should be immutable
+ try {
+ config.setProperty("hibernate.dialect", "foo");
+ fail("did not throw");
+ } catch(UnsupportedOperationException e) {
+ assertTrue(e.getMessage().contains("immutable"));
+ }
+ }
+
}