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/15 15:35:00 UTC

svn commit: r584786 - in /tapestry/tapestry5/trunk/tapestry-hibernate: ./ src/main/java/org/apache/tapestry/hibernate/ src/main/java/org/apache/tapestry/internal/hibernate/ src/test/java/org/apache/tapestry/internal/hibernate/

Author: dadams
Date: Mon Oct 15 06:34:59 2007
New Revision: 584786

URL: http://svn.apache.org/viewvc?rev=584786&view=rev
Log:
Fixed TAPESTRY-1372 : Allow contributions to the Hibernate Configuration

Added:
    tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateConfigurer.java
    tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateEntityPackageManager.java
    tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurer.java
    tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/PackageNameHibernateConfigurer.java
    tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurerFilterTest.java
Modified:
    tapestry/tapestry5/trunk/tapestry-hibernate/   (props changed)
    tapestry/tapestry5/trunk/tapestry-hibernate/pom.xml
    tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateModule.java
    tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/HibernateSessionSourceImpl.java
    tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry/internal/hibernate/HibernateSessionSourceImplTest.java

Propchange: tapestry/tapestry5/trunk/tapestry-hibernate/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Mon Oct 15 06:34:59 2007
@@ -3,3 +3,4 @@
 temp-testng-customsuite.xml
 cobertura.ser
 null
+test-output

Modified: tapestry/tapestry5/trunk/tapestry-hibernate/pom.xml
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/pom.xml?rev=584786&r1=584785&r2=584786&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-hibernate/pom.xml (original)
+++ tapestry/tapestry5/trunk/tapestry-hibernate/pom.xml Mon Oct 15 06:34:59 2007
@@ -34,6 +34,12 @@
       <artifactId>easymock</artifactId>
     </dependency>
     <dependency>
+      <groupId>org.easymock</groupId>
+      <artifactId>easymockclassextension</artifactId>
+      <version>2.2.2</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
       <groupId>hsqldb</groupId>
       <artifactId>hsqldb</artifactId>
       <version>1.8.0.7</version>

Added: tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateConfigurer.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateConfigurer.java?rev=584786&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateConfigurer.java (added)
+++ tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateConfigurer.java Mon Oct 15 06:34:59 2007
@@ -0,0 +1,24 @@
+// 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.hibernate;
+
+import org.hibernate.SessionFactory;
+import org.hibernate.cfg.Configuration;
+
+/** Modifies the Hibernate configuration in some way before the {@link SessionFactory} is created.
+ */
+public interface HibernateConfigurer {
+	void configure(Configuration configuration);
+}

Added: tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateEntityPackageManager.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateEntityPackageManager.java?rev=584786&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateEntityPackageManager.java (added)
+++ tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateEntityPackageManager.java Mon Oct 15 06:34:59 2007
@@ -0,0 +1,24 @@
+// 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.hibernate;
+
+import java.util.Collection;
+
+/** Contains a set of contributed package names from which to load entities.
+ */
+public interface HibernateEntityPackageManager {
+	/** Returns packages from which read entity classes */ 
+	Collection<String> getPackageNames();
+}

Modified: tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateModule.java?rev=584786&r1=584785&r2=584786&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateModule.java (original)
+++ tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateModule.java Mon Oct 15 06:34:59 2007
@@ -16,11 +16,17 @@
 
 import static org.apache.tapestry.ioc.IOCConstants.PERTHREAD_SCOPE;
 
+import java.util.Collection;
+import java.util.List;
+
 import org.apache.tapestry.internal.InternalConstants;
+import org.apache.tapestry.internal.hibernate.DefaultHibernateConfigurer;
 import org.apache.tapestry.internal.hibernate.HibernateSessionManagerImpl;
 import org.apache.tapestry.internal.hibernate.HibernateSessionSourceImpl;
+import org.apache.tapestry.internal.hibernate.PackageNameHibernateConfigurer;
+import org.apache.tapestry.internal.services.ClassNameLocator;
 import org.apache.tapestry.ioc.Configuration;
-import org.apache.tapestry.ioc.ServiceBinder;
+import org.apache.tapestry.ioc.OrderedConfiguration;
 import org.apache.tapestry.ioc.annotations.Inject;
 import org.apache.tapestry.ioc.annotations.InjectService;
 import org.apache.tapestry.ioc.annotations.Scope;
@@ -30,19 +36,24 @@
 import org.apache.tapestry.services.AliasContribution;
 import org.hibernate.Session;
 import org.hibernate.Transaction;
+import org.slf4j.Logger;
 
 public class HibernateModule
 {
-    public static void bind(ServiceBinder binder)
-    {
-        binder.bind(HibernateSessionSource.class, HibernateSessionSourceImpl.class);
-    }
 
+    public static HibernateEntityPackageManager build(final Collection<String> packageNames) {
+    	return new HibernateEntityPackageManager() {
+			public Collection<String> getPackageNames() {
+				return packageNames;
+			}
+    	};
+    }
+    
     /**
      * Contributes the package "&lt;root&gt;.entities" to the configuration, so that it will be
      * scanned for annotated entity classes.
      */
-    public static void contributeHibernateSessionSource(Configuration<String> configuration,
+    public static void contributeHibernateEntityPackageManager(Configuration<String> configuration,
 
     @Inject
     @Symbol(InternalConstants.TAPESTRY_APP_PACKAGE_PARAM)
@@ -95,4 +106,23 @@
     {
         configuration.add(AliasContribution.create(Session.class, session));
     }
+    
+    public static HibernateSessionSource build(Logger log, List<HibernateConfigurer> config) {
+    	return new HibernateSessionSourceImpl(log, config);
+    }
+    
+    /** Adds the following configurers:
+     * <ul>
+     * <li>Default - performs default hibernate configuration</li>
+     * <li>PackageName - loads entities by package name</li>
+     * </ul>
+     */
+    public static void contributeHibernateSessionSource(OrderedConfiguration<HibernateConfigurer> config,
+    		final ClassNameLocator classNameLocator,
+    		final HibernateEntityPackageManager packageManager) 
+    {
+    	config.add("Default", new DefaultHibernateConfigurer());    	
+    	config.add("PackageName", new PackageNameHibernateConfigurer(packageManager, classNameLocator));
+    }
+    
 }

Added: tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurer.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurer.java?rev=584786&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurer.java (added)
+++ tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurer.java Mon Oct 15 06:34:59 2007
@@ -0,0 +1,27 @@
+// 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 org.apache.tapestry.hibernate.HibernateConfigurer;
+import org.hibernate.cfg.Configuration;
+
+/** Simply calls configure() to do the default Hibernate configuration.
+ */
+public final class DefaultHibernateConfigurer implements HibernateConfigurer {
+
+	public void configure(Configuration configuration) {
+		configuration.configure();
+	}
+}

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=584786&r1=584785&r2=584786&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 Mon Oct 15 06:34:59 2007
@@ -14,11 +14,10 @@
 
 package org.apache.tapestry.internal.hibernate;
 
-import java.util.Collection;
+import java.util.List;
 
+import org.apache.tapestry.hibernate.HibernateConfigurer;
 import org.apache.tapestry.hibernate.HibernateSessionSource;
-import org.apache.tapestry.internal.services.ClassNameLocator;
-import org.apache.tapestry.ioc.annotations.InjectService;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.hibernate.cfg.AnnotationConfiguration;
@@ -28,38 +27,14 @@
 {
     private SessionFactory _sessionFactory;
 
-    public HibernateSessionSourceImpl(Logger logger, Collection<String> packageNames,
-
-    @InjectService("ClassNameLocator")
-    ClassNameLocator classNameLocator)
+    public HibernateSessionSourceImpl(Logger logger, List<HibernateConfigurer> hibernateConfigurers)
     {
-        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
         long startTime = System.currentTimeMillis();
 
         AnnotationConfiguration configuration = new AnnotationConfiguration();
 
-        // Perform normal configuration.
-
-        configuration.configure();
-
-        for (String packageName : packageNames)
-        {
-            configuration.addPackage(packageName);
-
-            for (String className : classNameLocator.locateClassNames(packageName))
-            {
-                try
-                {
-                    Class entityClass = contextClassLoader.loadClass(className);
-
-                    configuration.addAnnotatedClass(entityClass);
-                }
-                catch (ClassNotFoundException ex)
-                {
-                    throw new RuntimeException(ex);
-                }
-            }
-        }
+        for(HibernateConfigurer configurer : hibernateConfigurers)
+        	configurer.configure(configuration);
 
         long configurationComplete = System.currentTimeMillis();
 

Added: tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/PackageNameHibernateConfigurer.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/PackageNameHibernateConfigurer.java?rev=584786&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/PackageNameHibernateConfigurer.java (added)
+++ tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/PackageNameHibernateConfigurer.java Mon Oct 15 06:34:59 2007
@@ -0,0 +1,61 @@
+// 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 org.apache.tapestry.hibernate.HibernateConfigurer;
+import org.apache.tapestry.hibernate.HibernateEntityPackageManager;
+import org.apache.tapestry.internal.services.ClassNameLocator;
+import org.apache.tapestry.ioc.internal.util.Defense;
+import org.hibernate.cfg.AnnotationConfiguration;
+import org.hibernate.cfg.Configuration;
+
+/** Adds entity classes from a given set of packages to the configuration.
+ */
+public final class PackageNameHibernateConfigurer implements HibernateConfigurer {
+	private final HibernateEntityPackageManager _packageManager;
+	private final ClassNameLocator _classNameLocator;
+	
+	public PackageNameHibernateConfigurer(HibernateEntityPackageManager packageManager, ClassNameLocator classNameLocator) {
+		super();
+		_packageManager = packageManager;
+		_classNameLocator = classNameLocator;
+	}
+
+	public void configure(Configuration configuration) {
+		Defense.cast(configuration, AnnotationConfiguration.class, "configuration");
+		AnnotationConfiguration cfg = (AnnotationConfiguration)configuration;
+
+		ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
+
+        for (String packageName : _packageManager.getPackageNames())
+        {
+            cfg.addPackage(packageName);
+
+            for (String className : _classNameLocator.locateClassNames(packageName))
+            {
+                try
+                {
+                    Class entityClass = contextClassLoader.loadClass(className);
+                    cfg.addAnnotatedClass(entityClass);
+                }
+                catch (ClassNotFoundException ex)
+                {
+                    throw new RuntimeException(ex);
+                }
+            }
+        }
+	}
+
+}

Added: tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurerFilterTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurerFilterTest.java?rev=584786&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurerFilterTest.java (added)
+++ tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurerFilterTest.java Mon Oct 15 06:34:59 2007
@@ -0,0 +1,35 @@
+// 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 static org.easymock.EasyMock.expect;
+import static org.easymock.classextension.EasyMock.createMock;
+import static org.easymock.classextension.EasyMock.replay;
+import static org.easymock.classextension.EasyMock.verify;
+
+import org.hibernate.cfg.Configuration;
+import org.testng.annotations.Test;
+
+@Test
+public class DefaultHibernateConfigurerFilterTest {
+	public void testConfigure() throws Exception {
+		Configuration config = createMock(Configuration.class);
+		expect(config.configure()).andReturn(config);
+		
+		replay(config);
+		new DefaultHibernateConfigurer().configure(config);
+		verify(config);
+	}
+}

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=584786&r1=584785&r2=584786&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 Mon Oct 15 06:34:59 2007
@@ -14,12 +14,19 @@
 
 package org.apache.tapestry.internal.hibernate;
 
+import java.util.Arrays;
 import java.util.Collection;
+import java.util.List;
 
+import org.apache.tapestry.hibernate.HibernateConfigurer;
+import org.apache.tapestry.hibernate.HibernateEntityPackageManager;
 import org.apache.tapestry.hibernate.HibernateSessionSource;
 import org.apache.tapestry.internal.services.ClassNameLocatorImpl;
 import org.apache.tapestry.ioc.internal.util.CollectionFactory;
 import org.apache.tapestry.test.TapestryTestCase;
+import org.example.app0.entities.User;
+import org.hibernate.Session;
+import org.hibernate.metadata.ClassMetadata;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.testng.annotations.Test;
@@ -35,10 +42,23 @@
         Collection<String> packageNames = CollectionFactory.newList(
                 "org.example.myapp.entities",
                 "org.example.app0.entities");
+    	HibernateEntityPackageManager packageManager = newMock(HibernateEntityPackageManager.class);
+    	expect(packageManager.getPackageNames()).andReturn(packageNames);
 
-        HibernateSessionSource source = new HibernateSessionSourceImpl(_log, packageNames,
-                new ClassNameLocatorImpl());
+    	List<HibernateConfigurer> filters = Arrays.asList(
+    			new DefaultHibernateConfigurer(),
+    			new PackageNameHibernateConfigurer(packageManager, new ClassNameLocatorImpl()));
+    	
+    	replay();
+        HibernateSessionSource source = new HibernateSessionSourceImpl(_log, filters);
 
-        assertNotNull(source.create());
+        Session session = source.create();
+		assertNotNull(session);
+		
+		// make sure it found the entity in the package
+		ClassMetadata meta = session.getSessionFactory().getClassMetadata(User.class);
+		assertEquals(meta.getEntityName(), "org.example.app0.entities.User");
+		
+        verify();
     }
 }



Re: svn commit: r584786 - in /tapestry/tapestry5/trunk/tapestry-hibernate: ./ src/main/java/org/apache/tapestry/hibernate/ src/main/java/org/apache/tapestry/internal/hibernate/ src/test/java/org/apache/tapestry/internal/hibernate/

Posted by Dan Adams <da...@ifactory.com>.
Yeah, I'll do that.

On Mon, 2007-10-15 at 11:57 -0400, D&J Gredler wrote:
> Hey Dan,
> 
> Good stuff! Can you update the docs when you get a chance?
> 
> Take care,
> 
> Daniel
> 
> 
> On 10/15/07, dadams@apache.org <da...@apache.org> wrote:
> >
> > Author: dadams
> > Date: Mon Oct 15 06:34:59 2007
> > New Revision: 584786
> >
> > URL: http://svn.apache.org/viewvc?rev=584786&view=rev
> > Log:
> > Fixed TAPESTRY-1372 : Allow contributions to the Hibernate Configuration
> >
> > Added:
> >
> >     tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateConfigurer.java
> >
> >     tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateEntityPackageManager.java
> >
> >     tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurer.java
> >
> >     tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/PackageNameHibernateConfigurer.java
> >
> >     tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurerFilterTest.java
> > Modified:
> >     tapestry/tapestry5/trunk/tapestry-hibernate/   (props changed)
> >     tapestry/tapestry5/trunk/tapestry-hibernate/pom.xml
> >
> >     tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateModule.java
> >
> >     tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/HibernateSessionSourceImpl.java
> >
> >     tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry/internal/hibernate/HibernateSessionSourceImplTest.java
> >
> > Propchange: tapestry/tapestry5/trunk/tapestry-hibernate/
> >
> > ------------------------------------------------------------------------------
> > --- svn:ignore (original)
> > +++ svn:ignore Mon Oct 15 06:34:59 2007
> > @@ -3,3 +3,4 @@
> > temp-testng-customsuite.xml
> > cobertura.ser
> > null
> > +test-output
> >
> > Modified: tapestry/tapestry5/trunk/tapestry-hibernate/pom.xml
> > URL:
> > http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/pom.xml?rev=584786&r1=584785&r2=584786&view=diff
> >
> > ==============================================================================
> > --- tapestry/tapestry5/trunk/tapestry-hibernate/pom.xml (original)
> > +++ tapestry/tapestry5/trunk/tapestry-hibernate/pom.xml Mon Oct 15
> > 06:34:59 2007
> > @@ -34,6 +34,12 @@
> >        <artifactId>easymock</artifactId>
> >      </dependency>
> >      <dependency>
> > +      <groupId>org.easymock</groupId>
> > +      <artifactId>easymockclassextension</artifactId>
> > +      <version>2.2.2</version>
> > +      <scope>test</scope>
> > +    </dependency>
> > +    <dependency>
> >        <groupId>hsqldb</groupId>
> >        <artifactId>hsqldb</artifactId>
> >        <version>1.8.0.7</version>
> >
> > Added:
> > tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateConfigurer.java
> > URL:
> > http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateConfigurer.java?rev=584786&view=auto
> >
> > ==============================================================================
> > ---
> > tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateConfigurer.java
> > (added)
> > +++
> > tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateConfigurer.java
> > Mon Oct 15 06:34:59 2007
> > @@ -0,0 +1,24 @@
> > +// 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.hibernate;
> > +
> > +import org.hibernate.SessionFactory;
> > +import org.hibernate.cfg.Configuration;
> > +
> > +/** Modifies the Hibernate configuration in some way before the {@link
> > SessionFactory} is created.
> > + */
> > +public interface HibernateConfigurer {
> > +       void configure(Configuration configuration);
> > +}
> >
> > Added:
> > tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateEntityPackageManager.java
> > URL:
> > http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateEntityPackageManager.java?rev=584786&view=auto
> >
> > ==============================================================================
> > ---
> > tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateEntityPackageManager.java
> > (added)
> > +++
> > tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateEntityPackageManager.java
> > Mon Oct 15 06:34:59 2007
> > @@ -0,0 +1,24 @@
> > +// 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.hibernate;
> > +
> > +import java.util.Collection;
> > +
> > +/** Contains a set of contributed package names from which to load
> > entities.
> > + */
> > +public interface HibernateEntityPackageManager {
> > +       /** Returns packages from which read entity classes */
> > +       Collection<String> getPackageNames();
> > +}
> >
> > Modified:
> > tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateModule.java
> > URL:
> > http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateModule.java?rev=584786&r1=584785&r2=584786&view=diff
> >
> > ==============================================================================
> > ---
> > tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateModule.java
> > (original)
> > +++
> > tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateModule.java
> > Mon Oct 15 06:34:59 2007
> > @@ -16,11 +16,17 @@
> >
> > import static org.apache.tapestry.ioc.IOCConstants.PERTHREAD_SCOPE;
> >
> > +import java.util.Collection;
> > +import java.util.List;
> > +
> > import org.apache.tapestry.internal.InternalConstants;
> > +import org.apache.tapestry.internal.hibernate.DefaultHibernateConfigurer;
> > import org.apache.tapestry.internal.hibernate.HibernateSessionManagerImpl;
> > import org.apache.tapestry.internal.hibernate.HibernateSessionSourceImpl;
> > +import
> > org.apache.tapestry.internal.hibernate.PackageNameHibernateConfigurer;
> > +import org.apache.tapestry.internal.services.ClassNameLocator;
> > import org.apache.tapestry.ioc.Configuration;
> > -import org.apache.tapestry.ioc.ServiceBinder;
> > +import org.apache.tapestry.ioc.OrderedConfiguration;
> > import org.apache.tapestry.ioc.annotations.Inject;
> > import org.apache.tapestry.ioc.annotations.InjectService;
> > import org.apache.tapestry.ioc.annotations.Scope;
> > @@ -30,19 +36,24 @@
> > import org.apache.tapestry.services.AliasContribution;
> > import org.hibernate.Session;
> > import org.hibernate.Transaction;
> > +import org.slf4j.Logger;
> >
> > public class HibernateModule
> > {
> > -    public static void bind(ServiceBinder binder)
> > -    {
> > -        binder.bind(HibernateSessionSource.class,
> > HibernateSessionSourceImpl.class);
> > -    }
> >
> > +    public static HibernateEntityPackageManager build(final
> > Collection<String> packageNames) {
> > +       return new HibernateEntityPackageManager() {
> > +                       public Collection<String> getPackageNames() {
> > +                               return packageNames;
> > +                       }
> > +       };
> > +    }
> > +
> >      /**
> >       * Contributes the package "&lt;root&gt;.entities" to the
> > configuration, so that it will be
> >       * scanned for annotated entity classes.
> >       */
> > -    public static void
> > contributeHibernateSessionSource(Configuration<String> configuration,
> > +    public static void
> > contributeHibernateEntityPackageManager(Configuration<String> configuration,
> >
> >      @Inject
> >      @Symbol(InternalConstants.TAPESTRY_APP_PACKAGE_PARAM)
> > @@ -95,4 +106,23 @@
> >      {
> >          configuration.add(AliasContribution.create(Session.class,
> > session));
> >      }
> > +
> > +    public static HibernateSessionSource build(Logger log,
> > List<HibernateConfigurer> config) {
> > +       return new HibernateSessionSourceImpl(log, config);
> > +    }
> > +
> > +    /** Adds the following configurers:
> > +     * <ul>
> > +     * <li>Default - performs default hibernate configuration</li>
> > +     * <li>PackageName - loads entities by package name</li>
> > +     * </ul>
> > +     */
> > +    public static void
> > contributeHibernateSessionSource(OrderedConfiguration<HibernateConfigurer>
> > config,
> > +               final ClassNameLocator classNameLocator,
> > +               final HibernateEntityPackageManager packageManager)
> > +    {
> > +       config.add("Default", new DefaultHibernateConfigurer());
> > +       config.add("PackageName", new
> > PackageNameHibernateConfigurer(packageManager, classNameLocator));
> > +    }
> > +
> > }
> >
> > Added:
> > tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurer.java
> > URL:
> > http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurer.java?rev=584786&view=auto
> >
> > ==============================================================================
> > ---
> > tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurer.java
> > (added)
> > +++
> > tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurer.java
> > Mon Oct 15 06:34:59 2007
> > @@ -0,0 +1,27 @@
> > +// 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 org.apache.tapestry.hibernate.HibernateConfigurer;
> > +import org.hibernate.cfg.Configuration;
> > +
> > +/** Simply calls configure() to do the default Hibernate configuration.
> > + */
> > +public final class DefaultHibernateConfigurer implements
> > HibernateConfigurer {
> > +
> > +       public void configure(Configuration configuration) {
> > +               configuration.configure();
> > +       }
> > +}
> >
> > 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=584786&r1=584785&r2=584786&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
> > Mon Oct 15 06:34:59 2007
> > @@ -14,11 +14,10 @@
> >
> > package org.apache.tapestry.internal.hibernate;
> >
> > -import java.util.Collection;
> > +import java.util.List;
> >
> > +import org.apache.tapestry.hibernate.HibernateConfigurer;
> > import org.apache.tapestry.hibernate.HibernateSessionSource;
> > -import org.apache.tapestry.internal.services.ClassNameLocator;
> > -import org.apache.tapestry.ioc.annotations.InjectService;
> > import org.hibernate.Session;
> > import org.hibernate.SessionFactory;
> > import org.hibernate.cfg.AnnotationConfiguration;
> > @@ -28,38 +27,14 @@
> > {
> >      private SessionFactory _sessionFactory;
> >
> > -    public HibernateSessionSourceImpl(Logger logger, Collection<String>
> > packageNames,
> > -
> > -    @InjectService("ClassNameLocator")
> > -    ClassNameLocator classNameLocator)
> > +    public HibernateSessionSourceImpl(Logger logger,
> > List<HibernateConfigurer> hibernateConfigurers)
> >      {
> > -        ClassLoader contextClassLoader = Thread.currentThread
> > ().getContextClassLoader();
> >          long startTime = System.currentTimeMillis();
> >
> >          AnnotationConfiguration configuration = new
> > AnnotationConfiguration();
> >
> > -        // Perform normal configuration.
> > -
> > -        configuration.configure();
> > -
> > -        for (String packageName : packageNames)
> > -        {
> > -            configuration.addPackage(packageName);
> > -
> > -            for (String className : classNameLocator.locateClassNames
> > (packageName))
> > -            {
> > -                try
> > -                {
> > -                    Class entityClass = contextClassLoader.loadClass
> > (className);
> > -
> > -                    configuration.addAnnotatedClass(entityClass);
> > -                }
> > -                catch (ClassNotFoundException ex)
> > -                {
> > -                    throw new RuntimeException(ex);
> > -                }
> > -            }
> > -        }
> > +        for(HibernateConfigurer configurer : hibernateConfigurers)
> > +               configurer.configure(configuration);
> >
> >          long configurationComplete = System.currentTimeMillis();
> >
> >
> > Added:
> > tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/PackageNameHibernateConfigurer.java
> > URL:
> > http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/PackageNameHibernateConfigurer.java?rev=584786&view=auto
> >
> > ==============================================================================
> > ---
> > tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/PackageNameHibernateConfigurer.java
> > (added)
> > +++
> > tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/PackageNameHibernateConfigurer.java
> > Mon Oct 15 06:34:59 2007
> > @@ -0,0 +1,61 @@
> > +// 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 org.apache.tapestry.hibernate.HibernateConfigurer;
> > +import org.apache.tapestry.hibernate.HibernateEntityPackageManager;
> > +import org.apache.tapestry.internal.services.ClassNameLocator;
> > +import org.apache.tapestry.ioc.internal.util.Defense;
> > +import org.hibernate.cfg.AnnotationConfiguration;
> > +import org.hibernate.cfg.Configuration;
> > +
> > +/** Adds entity classes from a given set of packages to the
> > configuration.
> > + */
> > +public final class PackageNameHibernateConfigurer implements
> > HibernateConfigurer {
> > +       private final HibernateEntityPackageManager _packageManager;
> > +       private final ClassNameLocator _classNameLocator;
> > +
> > +       public
> > PackageNameHibernateConfigurer(HibernateEntityPackageManager packageManager,
> > ClassNameLocator classNameLocator) {
> > +               super();
> > +               _packageManager = packageManager;
> > +               _classNameLocator = classNameLocator;
> > +       }
> > +
> > +       public void configure(Configuration configuration) {
> > +               Defense.cast(configuration, AnnotationConfiguration.class,
> > "configuration");
> > +               AnnotationConfiguration cfg =
> > (AnnotationConfiguration)configuration;
> > +
> > +               ClassLoader contextClassLoader = Thread.currentThread
> > ().getContextClassLoader();
> > +
> > +        for (String packageName : _packageManager.getPackageNames())
> > +        {
> > +            cfg.addPackage(packageName);
> > +
> > +            for (String className :
> > _classNameLocator.locateClassNames(packageName))
> > +            {
> > +                try
> > +                {
> > +                    Class entityClass = contextClassLoader.loadClass
> > (className);
> > +                    cfg.addAnnotatedClass(entityClass);
> > +                }
> > +                catch (ClassNotFoundException ex)
> > +                {
> > +                    throw new RuntimeException(ex);
> > +                }
> > +            }
> > +        }
> > +       }
> > +
> > +}
> >
> > Added:
> > tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurerFilterTest.java
> > URL:
> > http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurerFilterTest.java?rev=584786&view=auto
> >
> > ==============================================================================
> > ---
> > tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurerFilterTest.java
> > (added)
> > +++
> > tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurerFilterTest.java
> > Mon Oct 15 06:34:59 2007
> > @@ -0,0 +1,35 @@
> > +// 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 static org.easymock.EasyMock.expect;
> > +import static org.easymock.classextension.EasyMock.createMock;
> > +import static org.easymock.classextension.EasyMock.replay;
> > +import static org.easymock.classextension.EasyMock.verify;
> > +
> > +import org.hibernate.cfg.Configuration;
> > +import org.testng.annotations.Test;
> > +
> > +@Test
> > +public class DefaultHibernateConfigurerFilterTest {
> > +       public void testConfigure() throws Exception {
> > +               Configuration config = createMock(Configuration.class);
> > +               expect(config.configure()).andReturn(config);
> > +
> > +               replay(config);
> > +               new DefaultHibernateConfigurer().configure(config);
> > +               verify(config);
> > +       }
> > +}
> >
> > 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=584786&r1=584785&r2=584786&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
> > Mon Oct 15 06:34:59 2007
> > @@ -14,12 +14,19 @@
> >
> > package org.apache.tapestry.internal.hibernate;
> >
> > +import java.util.Arrays;
> > import java.util.Collection;
> > +import java.util.List;
> >
> > +import org.apache.tapestry.hibernate.HibernateConfigurer;
> > +import org.apache.tapestry.hibernate.HibernateEntityPackageManager;
> > import org.apache.tapestry.hibernate.HibernateSessionSource;
> > import org.apache.tapestry.internal.services.ClassNameLocatorImpl;
> > import org.apache.tapestry.ioc.internal.util.CollectionFactory;
> > import org.apache.tapestry.test.TapestryTestCase;
> > +import org.example.app0.entities.User;
> > +import org.hibernate.Session;
> > +import org.hibernate.metadata.ClassMetadata;
> > import org.slf4j.Logger;
> > import org.slf4j.LoggerFactory;
> > import org.testng.annotations.Test;
> > @@ -35,10 +42,23 @@
> >          Collection<String> packageNames = CollectionFactory.newList(
> >                  "org.example.myapp.entities",
> >                  "org.example.app0.entities");
> > +       HibernateEntityPackageManager packageManager = newMock(
> > HibernateEntityPackageManager.class);
> > +       expect(packageManager.getPackageNames()).andReturn(packageNames);
> >
> > -        HibernateSessionSource source = new
> > HibernateSessionSourceImpl(_log, packageNames,
> > -                new ClassNameLocatorImpl());
> > +       List<HibernateConfigurer> filters = Arrays.asList(
> > +                       new DefaultHibernateConfigurer(),
> > +                       new PackageNameHibernateConfigurer(packageManager,
> > new ClassNameLocatorImpl()));
> > +
> > +       replay();
> > +        HibernateSessionSource source = new
> > HibernateSessionSourceImpl(_log, filters);
> >
> > -        assertNotNull(source.create());
> > +        Session session = source.create();
> > +               assertNotNull(session);
> > +
> > +               // make sure it found the entity in the package
> > +               ClassMetadata meta = session.getSessionFactory
> > ().getClassMetadata(User.class);
> > +               assertEquals(meta.getEntityName(), "
> > org.example.app0.entities.User");
> > +
> > +        verify();
> >      }
> > }
> >
> >
> >
> 
> 
-- 
Dan Adams
Senior Software Engineer
Interactive Factory
617.235.5857


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tapestry.apache.org
For additional commands, e-mail: dev-help@tapestry.apache.org


Re: svn commit: r584786 - in /tapestry/tapestry5/trunk/tapestry-hibernate: ./ src/main/java/org/apache/tapestry/hibernate/ src/main/java/org/apache/tapestry/internal/hibernate/ src/test/java/org/apache/tapestry/internal/hibernate/

Posted by D&J Gredler <dj...@gmail.com>.
Hey Dan,

Good stuff! Can you update the docs when you get a chance?

Take care,

Daniel


On 10/15/07, dadams@apache.org <da...@apache.org> wrote:
>
> Author: dadams
> Date: Mon Oct 15 06:34:59 2007
> New Revision: 584786
>
> URL: http://svn.apache.org/viewvc?rev=584786&view=rev
> Log:
> Fixed TAPESTRY-1372 : Allow contributions to the Hibernate Configuration
>
> Added:
>
>     tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateConfigurer.java
>
>     tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateEntityPackageManager.java
>
>     tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurer.java
>
>     tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/PackageNameHibernateConfigurer.java
>
>     tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurerFilterTest.java
> Modified:
>     tapestry/tapestry5/trunk/tapestry-hibernate/   (props changed)
>     tapestry/tapestry5/trunk/tapestry-hibernate/pom.xml
>
>     tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateModule.java
>
>     tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/HibernateSessionSourceImpl.java
>
>     tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry/internal/hibernate/HibernateSessionSourceImplTest.java
>
> Propchange: tapestry/tapestry5/trunk/tapestry-hibernate/
>
> ------------------------------------------------------------------------------
> --- svn:ignore (original)
> +++ svn:ignore Mon Oct 15 06:34:59 2007
> @@ -3,3 +3,4 @@
> temp-testng-customsuite.xml
> cobertura.ser
> null
> +test-output
>
> Modified: tapestry/tapestry5/trunk/tapestry-hibernate/pom.xml
> URL:
> http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/pom.xml?rev=584786&r1=584785&r2=584786&view=diff
>
> ==============================================================================
> --- tapestry/tapestry5/trunk/tapestry-hibernate/pom.xml (original)
> +++ tapestry/tapestry5/trunk/tapestry-hibernate/pom.xml Mon Oct 15
> 06:34:59 2007
> @@ -34,6 +34,12 @@
>        <artifactId>easymock</artifactId>
>      </dependency>
>      <dependency>
> +      <groupId>org.easymock</groupId>
> +      <artifactId>easymockclassextension</artifactId>
> +      <version>2.2.2</version>
> +      <scope>test</scope>
> +    </dependency>
> +    <dependency>
>        <groupId>hsqldb</groupId>
>        <artifactId>hsqldb</artifactId>
>        <version>1.8.0.7</version>
>
> Added:
> tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateConfigurer.java
> URL:
> http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateConfigurer.java?rev=584786&view=auto
>
> ==============================================================================
> ---
> tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateConfigurer.java
> (added)
> +++
> tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateConfigurer.java
> Mon Oct 15 06:34:59 2007
> @@ -0,0 +1,24 @@
> +// 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.hibernate;
> +
> +import org.hibernate.SessionFactory;
> +import org.hibernate.cfg.Configuration;
> +
> +/** Modifies the Hibernate configuration in some way before the {@link
> SessionFactory} is created.
> + */
> +public interface HibernateConfigurer {
> +       void configure(Configuration configuration);
> +}
>
> Added:
> tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateEntityPackageManager.java
> URL:
> http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateEntityPackageManager.java?rev=584786&view=auto
>
> ==============================================================================
> ---
> tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateEntityPackageManager.java
> (added)
> +++
> tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateEntityPackageManager.java
> Mon Oct 15 06:34:59 2007
> @@ -0,0 +1,24 @@
> +// 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.hibernate;
> +
> +import java.util.Collection;
> +
> +/** Contains a set of contributed package names from which to load
> entities.
> + */
> +public interface HibernateEntityPackageManager {
> +       /** Returns packages from which read entity classes */
> +       Collection<String> getPackageNames();
> +}
>
> Modified:
> tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateModule.java
> URL:
> http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateModule.java?rev=584786&r1=584785&r2=584786&view=diff
>
> ==============================================================================
> ---
> tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateModule.java
> (original)
> +++
> tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/hibernate/HibernateModule.java
> Mon Oct 15 06:34:59 2007
> @@ -16,11 +16,17 @@
>
> import static org.apache.tapestry.ioc.IOCConstants.PERTHREAD_SCOPE;
>
> +import java.util.Collection;
> +import java.util.List;
> +
> import org.apache.tapestry.internal.InternalConstants;
> +import org.apache.tapestry.internal.hibernate.DefaultHibernateConfigurer;
> import org.apache.tapestry.internal.hibernate.HibernateSessionManagerImpl;
> import org.apache.tapestry.internal.hibernate.HibernateSessionSourceImpl;
> +import
> org.apache.tapestry.internal.hibernate.PackageNameHibernateConfigurer;
> +import org.apache.tapestry.internal.services.ClassNameLocator;
> import org.apache.tapestry.ioc.Configuration;
> -import org.apache.tapestry.ioc.ServiceBinder;
> +import org.apache.tapestry.ioc.OrderedConfiguration;
> import org.apache.tapestry.ioc.annotations.Inject;
> import org.apache.tapestry.ioc.annotations.InjectService;
> import org.apache.tapestry.ioc.annotations.Scope;
> @@ -30,19 +36,24 @@
> import org.apache.tapestry.services.AliasContribution;
> import org.hibernate.Session;
> import org.hibernate.Transaction;
> +import org.slf4j.Logger;
>
> public class HibernateModule
> {
> -    public static void bind(ServiceBinder binder)
> -    {
> -        binder.bind(HibernateSessionSource.class,
> HibernateSessionSourceImpl.class);
> -    }
>
> +    public static HibernateEntityPackageManager build(final
> Collection<String> packageNames) {
> +       return new HibernateEntityPackageManager() {
> +                       public Collection<String> getPackageNames() {
> +                               return packageNames;
> +                       }
> +       };
> +    }
> +
>      /**
>       * Contributes the package "&lt;root&gt;.entities" to the
> configuration, so that it will be
>       * scanned for annotated entity classes.
>       */
> -    public static void
> contributeHibernateSessionSource(Configuration<String> configuration,
> +    public static void
> contributeHibernateEntityPackageManager(Configuration<String> configuration,
>
>      @Inject
>      @Symbol(InternalConstants.TAPESTRY_APP_PACKAGE_PARAM)
> @@ -95,4 +106,23 @@
>      {
>          configuration.add(AliasContribution.create(Session.class,
> session));
>      }
> +
> +    public static HibernateSessionSource build(Logger log,
> List<HibernateConfigurer> config) {
> +       return new HibernateSessionSourceImpl(log, config);
> +    }
> +
> +    /** Adds the following configurers:
> +     * <ul>
> +     * <li>Default - performs default hibernate configuration</li>
> +     * <li>PackageName - loads entities by package name</li>
> +     * </ul>
> +     */
> +    public static void
> contributeHibernateSessionSource(OrderedConfiguration<HibernateConfigurer>
> config,
> +               final ClassNameLocator classNameLocator,
> +               final HibernateEntityPackageManager packageManager)
> +    {
> +       config.add("Default", new DefaultHibernateConfigurer());
> +       config.add("PackageName", new
> PackageNameHibernateConfigurer(packageManager, classNameLocator));
> +    }
> +
> }
>
> Added:
> tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurer.java
> URL:
> http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurer.java?rev=584786&view=auto
>
> ==============================================================================
> ---
> tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurer.java
> (added)
> +++
> tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurer.java
> Mon Oct 15 06:34:59 2007
> @@ -0,0 +1,27 @@
> +// 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 org.apache.tapestry.hibernate.HibernateConfigurer;
> +import org.hibernate.cfg.Configuration;
> +
> +/** Simply calls configure() to do the default Hibernate configuration.
> + */
> +public final class DefaultHibernateConfigurer implements
> HibernateConfigurer {
> +
> +       public void configure(Configuration configuration) {
> +               configuration.configure();
> +       }
> +}
>
> 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=584786&r1=584785&r2=584786&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
> Mon Oct 15 06:34:59 2007
> @@ -14,11 +14,10 @@
>
> package org.apache.tapestry.internal.hibernate;
>
> -import java.util.Collection;
> +import java.util.List;
>
> +import org.apache.tapestry.hibernate.HibernateConfigurer;
> import org.apache.tapestry.hibernate.HibernateSessionSource;
> -import org.apache.tapestry.internal.services.ClassNameLocator;
> -import org.apache.tapestry.ioc.annotations.InjectService;
> import org.hibernate.Session;
> import org.hibernate.SessionFactory;
> import org.hibernate.cfg.AnnotationConfiguration;
> @@ -28,38 +27,14 @@
> {
>      private SessionFactory _sessionFactory;
>
> -    public HibernateSessionSourceImpl(Logger logger, Collection<String>
> packageNames,
> -
> -    @InjectService("ClassNameLocator")
> -    ClassNameLocator classNameLocator)
> +    public HibernateSessionSourceImpl(Logger logger,
> List<HibernateConfigurer> hibernateConfigurers)
>      {
> -        ClassLoader contextClassLoader = Thread.currentThread
> ().getContextClassLoader();
>          long startTime = System.currentTimeMillis();
>
>          AnnotationConfiguration configuration = new
> AnnotationConfiguration();
>
> -        // Perform normal configuration.
> -
> -        configuration.configure();
> -
> -        for (String packageName : packageNames)
> -        {
> -            configuration.addPackage(packageName);
> -
> -            for (String className : classNameLocator.locateClassNames
> (packageName))
> -            {
> -                try
> -                {
> -                    Class entityClass = contextClassLoader.loadClass
> (className);
> -
> -                    configuration.addAnnotatedClass(entityClass);
> -                }
> -                catch (ClassNotFoundException ex)
> -                {
> -                    throw new RuntimeException(ex);
> -                }
> -            }
> -        }
> +        for(HibernateConfigurer configurer : hibernateConfigurers)
> +               configurer.configure(configuration);
>
>          long configurationComplete = System.currentTimeMillis();
>
>
> Added:
> tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/PackageNameHibernateConfigurer.java
> URL:
> http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/PackageNameHibernateConfigurer.java?rev=584786&view=auto
>
> ==============================================================================
> ---
> tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/PackageNameHibernateConfigurer.java
> (added)
> +++
> tapestry/tapestry5/trunk/tapestry-hibernate/src/main/java/org/apache/tapestry/internal/hibernate/PackageNameHibernateConfigurer.java
> Mon Oct 15 06:34:59 2007
> @@ -0,0 +1,61 @@
> +// 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 org.apache.tapestry.hibernate.HibernateConfigurer;
> +import org.apache.tapestry.hibernate.HibernateEntityPackageManager;
> +import org.apache.tapestry.internal.services.ClassNameLocator;
> +import org.apache.tapestry.ioc.internal.util.Defense;
> +import org.hibernate.cfg.AnnotationConfiguration;
> +import org.hibernate.cfg.Configuration;
> +
> +/** Adds entity classes from a given set of packages to the
> configuration.
> + */
> +public final class PackageNameHibernateConfigurer implements
> HibernateConfigurer {
> +       private final HibernateEntityPackageManager _packageManager;
> +       private final ClassNameLocator _classNameLocator;
> +
> +       public
> PackageNameHibernateConfigurer(HibernateEntityPackageManager packageManager,
> ClassNameLocator classNameLocator) {
> +               super();
> +               _packageManager = packageManager;
> +               _classNameLocator = classNameLocator;
> +       }
> +
> +       public void configure(Configuration configuration) {
> +               Defense.cast(configuration, AnnotationConfiguration.class,
> "configuration");
> +               AnnotationConfiguration cfg =
> (AnnotationConfiguration)configuration;
> +
> +               ClassLoader contextClassLoader = Thread.currentThread
> ().getContextClassLoader();
> +
> +        for (String packageName : _packageManager.getPackageNames())
> +        {
> +            cfg.addPackage(packageName);
> +
> +            for (String className :
> _classNameLocator.locateClassNames(packageName))
> +            {
> +                try
> +                {
> +                    Class entityClass = contextClassLoader.loadClass
> (className);
> +                    cfg.addAnnotatedClass(entityClass);
> +                }
> +                catch (ClassNotFoundException ex)
> +                {
> +                    throw new RuntimeException(ex);
> +                }
> +            }
> +        }
> +       }
> +
> +}
>
> Added:
> tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurerFilterTest.java
> URL:
> http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurerFilterTest.java?rev=584786&view=auto
>
> ==============================================================================
> ---
> tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurerFilterTest.java
> (added)
> +++
> tapestry/tapestry5/trunk/tapestry-hibernate/src/test/java/org/apache/tapestry/internal/hibernate/DefaultHibernateConfigurerFilterTest.java
> Mon Oct 15 06:34:59 2007
> @@ -0,0 +1,35 @@
> +// 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 static org.easymock.EasyMock.expect;
> +import static org.easymock.classextension.EasyMock.createMock;
> +import static org.easymock.classextension.EasyMock.replay;
> +import static org.easymock.classextension.EasyMock.verify;
> +
> +import org.hibernate.cfg.Configuration;
> +import org.testng.annotations.Test;
> +
> +@Test
> +public class DefaultHibernateConfigurerFilterTest {
> +       public void testConfigure() throws Exception {
> +               Configuration config = createMock(Configuration.class);
> +               expect(config.configure()).andReturn(config);
> +
> +               replay(config);
> +               new DefaultHibernateConfigurer().configure(config);
> +               verify(config);
> +       }
> +}
>
> 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=584786&r1=584785&r2=584786&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
> Mon Oct 15 06:34:59 2007
> @@ -14,12 +14,19 @@
>
> package org.apache.tapestry.internal.hibernate;
>
> +import java.util.Arrays;
> import java.util.Collection;
> +import java.util.List;
>
> +import org.apache.tapestry.hibernate.HibernateConfigurer;
> +import org.apache.tapestry.hibernate.HibernateEntityPackageManager;
> import org.apache.tapestry.hibernate.HibernateSessionSource;
> import org.apache.tapestry.internal.services.ClassNameLocatorImpl;
> import org.apache.tapestry.ioc.internal.util.CollectionFactory;
> import org.apache.tapestry.test.TapestryTestCase;
> +import org.example.app0.entities.User;
> +import org.hibernate.Session;
> +import org.hibernate.metadata.ClassMetadata;
> import org.slf4j.Logger;
> import org.slf4j.LoggerFactory;
> import org.testng.annotations.Test;
> @@ -35,10 +42,23 @@
>          Collection<String> packageNames = CollectionFactory.newList(
>                  "org.example.myapp.entities",
>                  "org.example.app0.entities");
> +       HibernateEntityPackageManager packageManager = newMock(
> HibernateEntityPackageManager.class);
> +       expect(packageManager.getPackageNames()).andReturn(packageNames);
>
> -        HibernateSessionSource source = new
> HibernateSessionSourceImpl(_log, packageNames,
> -                new ClassNameLocatorImpl());
> +       List<HibernateConfigurer> filters = Arrays.asList(
> +                       new DefaultHibernateConfigurer(),
> +                       new PackageNameHibernateConfigurer(packageManager,
> new ClassNameLocatorImpl()));
> +
> +       replay();
> +        HibernateSessionSource source = new
> HibernateSessionSourceImpl(_log, filters);
>
> -        assertNotNull(source.create());
> +        Session session = source.create();
> +               assertNotNull(session);
> +
> +               // make sure it found the entity in the package
> +               ClassMetadata meta = session.getSessionFactory
> ().getClassMetadata(User.class);
> +               assertEquals(meta.getEntityName(), "
> org.example.app0.entities.User");
> +
> +        verify();
>      }
> }
>
>
>


-- 
Daniel Gredler
http://daniel.gredler.net/