cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r439236 - in /incubator/cayenne/sandbox/jpa-webapp-example: ./ src/main/java/cayenne/example/jpa/ src/main/resources/META-INF/ src/main/webapp/ src/main/webapp/WEB-INF/
Date Fri, 01 Sep 2006 08:35:33 GMT
Author: aadamchik
Date: Fri Sep  1 01:35:32 2006
New Revision: 439236

URL: http://svn.apache.org/viewvc?rev=439236&view=rev
Log:
chaging webapp example to use application-managed EntityManager

Added:
    incubator/cayenne/sandbox/jpa-webapp-example/src/main/java/cayenne/example/jpa/CachingEntityManagerFactory.java
    incubator/cayenne/sandbox/jpa-webapp-example/src/main/java/cayenne/example/jpa/JpaFilter.java
    incubator/cayenne/sandbox/jpa-webapp-example/src/main/java/cayenne/example/jpa/ThreadEntityManagers.java
Removed:
    incubator/cayenne/sandbox/jpa-webapp-example/src/main/java/cayenne/example/jpa/JpaHelper.java
Modified:
    incubator/cayenne/sandbox/jpa-webapp-example/pom.xml
    incubator/cayenne/sandbox/jpa-webapp-example/src/main/resources/META-INF/persistence.xml
    incubator/cayenne/sandbox/jpa-webapp-example/src/main/webapp/WEB-INF/web.xml
    incubator/cayenne/sandbox/jpa-webapp-example/src/main/webapp/index.jsp

Modified: incubator/cayenne/sandbox/jpa-webapp-example/pom.xml
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/jpa-webapp-example/pom.xml?rev=439236&r1=439235&r2=439236&view=diff
==============================================================================
--- incubator/cayenne/sandbox/jpa-webapp-example/pom.xml (original)
+++ incubator/cayenne/sandbox/jpa-webapp-example/pom.xml Fri Sep  1 01:35:32 2006
@@ -1,23 +1,19 @@
 <?xml version="1.0"?>
 <project>
-	<modelVersion>4.0.0</modelVersion>
-	
 	<parent>
-		<groupId>org.apache.cayenne.core</groupId>
 		<artifactId>cayenne-core-parent</artifactId>
+		<groupId>org.apache.cayenne.core</groupId>
 		<version>3.0-incubating-SNAPSHOT</version>
 	</parent>
-	
+	<modelVersion>4.0.0</modelVersion>
 	<groupId>org.apache.cayenne.examples</groupId>
 	<artifactId>jpa-webapp-example</artifactId>
 	<packaging>war</packaging>
 	<name>Demonstrating a JPA application using Cayenne provider</name>
-
 	<build>
 		<finalName>app</finalName>
 		<plugins>
 			<plugin>
-				<groupId>org.apache.maven.plugins</groupId>
 				<artifactId>maven-compiler-plugin</artifactId>
 				<configuration>
 					<source>1.5</source>
@@ -45,5 +41,17 @@
 			<artifactId>cayenne-jpa</artifactId>
 			<version>${version}</version>
 		</dependency>
+		<dependency>
+			<groupId>javax.servlet</groupId>
+			<artifactId>servlet-api</artifactId>
+			<version>2.4</version>
+			<scope>provided</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.derby</groupId>
+			<artifactId>derby</artifactId>
+			<version>10.1.1.0</version>
+			<scope>runtime</scope>
+		</dependency>
 	</dependencies>
-</project>
+</project>
\ No newline at end of file

Added: incubator/cayenne/sandbox/jpa-webapp-example/src/main/java/cayenne/example/jpa/CachingEntityManagerFactory.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/jpa-webapp-example/src/main/java/cayenne/example/jpa/CachingEntityManagerFactory.java?rev=439236&view=auto
==============================================================================
--- incubator/cayenne/sandbox/jpa-webapp-example/src/main/java/cayenne/example/jpa/CachingEntityManagerFactory.java
(added)
+++ incubator/cayenne/sandbox/jpa-webapp-example/src/main/java/cayenne/example/jpa/CachingEntityManagerFactory.java
Fri Sep  1 01:35:32 2006
@@ -0,0 +1,79 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package cayenne.example.jpa;
+
+import java.util.Map;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+
+/**
+ * An EntityManagerFactory that reuses an EntityManager on repeat calls, allowing the same
+ * entity manager to be used within transaction. This factory is not thread-safe and is
+ * intended to be used within the same transaction thread.
+ * 
+ * @author Andrus Adamchik
+ */
+class CachingEntityManagerFactory implements EntityManagerFactory {
+
+    private EntityManagerFactory delegate;
+    private EntityManager entityManager;
+
+    CachingEntityManagerFactory(EntityManagerFactory delegate) {
+        this.delegate = delegate;
+    }
+
+    void commit() {
+        if (entityManager != null && entityManager.getTransaction().isActive()) {
+            entityManager.getTransaction().commit();
+            entityManager.close();
+            entityManager = null;
+        }
+    }
+
+    public void close() {
+
+        if (entityManager != null) {
+            entityManager.close();
+            entityManager = null;
+        }
+
+        delegate.close();
+    }
+
+    public EntityManager createEntityManager() {
+        if (entityManager == null) {
+            entityManager = delegate.createEntityManager();
+        }
+
+        return entityManager;
+    }
+
+    public EntityManager createEntityManager(Map properties) {
+        if (entityManager == null) {
+            entityManager = delegate.createEntityManager(properties);
+        }
+
+        return entityManager;
+    }
+
+    public boolean isOpen() {
+        return delegate.isOpen();
+    }
+}

Added: incubator/cayenne/sandbox/jpa-webapp-example/src/main/java/cayenne/example/jpa/JpaFilter.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/jpa-webapp-example/src/main/java/cayenne/example/jpa/JpaFilter.java?rev=439236&view=auto
==============================================================================
--- incubator/cayenne/sandbox/jpa-webapp-example/src/main/java/cayenne/example/jpa/JpaFilter.java
(added)
+++ incubator/cayenne/sandbox/jpa-webapp-example/src/main/java/cayenne/example/jpa/JpaFilter.java
Fri Sep  1 01:35:32 2006
@@ -0,0 +1,84 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package cayenne.example.jpa;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+public class JpaFilter implements Filter {
+
+    static final String DEFAULT_UNIT = "jpa-webapp-example";
+
+    protected Map<String, EntityManagerFactory> factories;
+
+    public void init(FilterConfig filterConfig) throws ServletException {
+        // TODO: andrus, 09/01/2006 - listing units explicitly in the filter is redundant.
+        // We may implement WebPersistenceProvider (or ThreadPersistenceProvider) that
+        // would do the thread magic without extra wrapping and can be accessed via
+        // Persistence class.
+
+        factories = new HashMap<String, EntityManagerFactory>();
+        factories.put(DEFAULT_UNIT, Persistence.createEntityManagerFactory(DEFAULT_UNIT));
+    }
+
+    public void destroy() {
+        for (EntityManagerFactory factory : factories.values()) {
+            factory.close();
+        }
+
+        factories = null;
+    }
+
+    public void doFilter(
+            ServletRequest request,
+            ServletResponse response,
+            FilterChain chain) throws IOException, ServletException {
+
+        // bind factory to a thread
+        Map<String, EntityManagerFactory> threadFactories = new HashMap<String,
EntityManagerFactory>(
+                factories.size() + 1);
+        for (Map.Entry<String, EntityManagerFactory> entry : factories.entrySet())
{
+            threadFactories.put(entry.getKey(), new CachingEntityManagerFactory(entry
+                    .getValue()));
+        }
+
+        ThreadEntityManagers.setEntityManagerFactories(threadFactories);
+
+        try {
+            chain.doFilter(request, response);
+        }
+        finally {
+            ThreadEntityManagers.clear();
+
+            for (EntityManagerFactory factory : threadFactories.values()) {
+                ((CachingEntityManagerFactory) factory).commit();
+            }
+        }
+    }
+}

Added: incubator/cayenne/sandbox/jpa-webapp-example/src/main/java/cayenne/example/jpa/ThreadEntityManagers.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/jpa-webapp-example/src/main/java/cayenne/example/jpa/ThreadEntityManagers.java?rev=439236&view=auto
==============================================================================
--- incubator/cayenne/sandbox/jpa-webapp-example/src/main/java/cayenne/example/jpa/ThreadEntityManagers.java
(added)
+++ incubator/cayenne/sandbox/jpa-webapp-example/src/main/java/cayenne/example/jpa/ThreadEntityManagers.java
Fri Sep  1 01:35:32 2006
@@ -0,0 +1,72 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package cayenne.example.jpa;
+
+import java.util.Map;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+
+/**
+ * Provides access to EntityManagers bound to the current thread.
+ * 
+ * @author Andrus Adamchik
+ */
+public class ThreadEntityManagers {
+
+    protected static ThreadLocal<Map<String, EntityManagerFactory>> entityManagerHolder
= new ThreadLocal<Map<String, EntityManagerFactory>>();
+
+    public static EntityManager getEntityManager() {
+        Map<String, EntityManagerFactory> factories = entityManagerHolder.get();
+        if (factories == null) {
+            throw new IllegalStateException("No thread bound factories exist");
+        }
+
+        if (factories.size() != 1) {
+            throw new IllegalStateException("Expected a single persistence unit, got "
+                    + factories.size());
+        }
+
+        EntityManagerFactory factory = factories.values().iterator().next();
+        return factory.createEntityManager();
+    }
+
+    public static EntityManager getEntityManager(String persistenceUnitName) {
+        Map<String, EntityManagerFactory> factories = entityManagerHolder.get();
+        if (factories == null) {
+            throw new IllegalStateException("No thread bound factories exist");
+        }
+
+        EntityManagerFactory factory = factories.get(persistenceUnitName);
+        if (factory == null) {
+            throw new IllegalArgumentException("Invalid persistence unit: "
+                    + persistenceUnitName);
+        }
+
+        return factory.createEntityManager();
+    }
+
+    public static void setEntityManagerFactories(Map<String, EntityManagerFactory>
map) {
+        entityManagerHolder.set(map);
+    }
+
+    public static void clear() {
+        setEntityManagerFactories(null);
+    }
+}

Modified: incubator/cayenne/sandbox/jpa-webapp-example/src/main/resources/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/jpa-webapp-example/src/main/resources/META-INF/persistence.xml?rev=439236&r1=439235&r2=439236&view=diff
==============================================================================
--- incubator/cayenne/sandbox/jpa-webapp-example/src/main/resources/META-INF/persistence.xml
(original)
+++ incubator/cayenne/sandbox/jpa-webapp-example/src/main/resources/META-INF/persistence.xml
Fri Sep  1 01:35:32 2006
@@ -1,25 +1,40 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!--
-   Licensed to the Apache Software Foundation (ASF) under one
-   or more contributor license agreements.  See the NOTICE file
-   distributed with this work for additional information
-   regarding copyright ownership.  The ASF licenses this file
-   to you under the Apache License, Version 2.0 (the
-   "License"); you may not use this file except in compliance
-   with the License.  You may obtain a copy of the License at
-
-     http://www.apache.org/licenses/LICENSE-2.0
- 
-   Unless required by applicable law or agreed to in writing,
-   software distributed under the License is distributed on an
-   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-   KIND, either express or implied.  See the License for the
-   specific language governing permissions and limitations
-   under the License.
+	Licensed to the Apache Software Foundation (ASF) under one
+	or more contributor license agreements.  See the NOTICE file
+	distributed with this work for additional information
+	regarding copyright ownership.  The ASF licenses this file
+	to you under the Apache License, Version 2.0 (the
+	"License"); you may not use this file except in compliance
+	with the License.  You may obtain a copy of the License at
+	
+	http://www.apache.org/licenses/LICENSE-2.0
+	
+	Unless required by applicable law or agreed to in writing,
+	software distributed under the License is distributed on an
+	"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+	KIND, either express or implied.  See the License for the
+	specific language governing permissions and limitations
+	under the License.
 -->
 
 <persistence>
 	<persistence-unit name="jpa-webapp-example">
+		<non-jta-data-source>local-data-source</non-jta-data-source>
 		<class>cayenne.example.jpa.entity.Entity1</class>
+		
+		<properties>
+			<property name="cayenne.ds.local-data-source.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
+			<property name="cayenne.ds.local-data-source.jdbc.url" value="jdbc:derby:cayenne/jpa-web-example;create=true"/>
+		
+		<!-- uncomment this to use login id and password for DB connection. -->
+		<!--
+			<property name="cayenne.ds.local-data-source.jdbc.username" value="andrus"/>
+			<property name="cayenne.ds.local-data-source.jdbc.password" value="andrus"/>
+		-->
+		
+			<property name="cayenne.ds.local-data-source.jdbc.minConnections" value="1"/>
+			<property name="cayenne.ds.local-data-source.jdbc.maxConnections" value="2"/>
+		</properties>
 	</persistence-unit>
 </persistence>

Modified: incubator/cayenne/sandbox/jpa-webapp-example/src/main/webapp/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/jpa-webapp-example/src/main/webapp/WEB-INF/web.xml?rev=439236&r1=439235&r2=439236&view=diff
==============================================================================
--- incubator/cayenne/sandbox/jpa-webapp-example/src/main/webapp/WEB-INF/web.xml (original)
+++ incubator/cayenne/sandbox/jpa-webapp-example/src/main/webapp/WEB-INF/web.xml Fri Sep 
1 01:35:32 2006
@@ -1,21 +1,21 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!--
-   Licensed to the Apache Software Foundation (ASF) under one
-   or more contributor license agreements.  See the NOTICE file
-   distributed with this work for additional information
-   regarding copyright ownership.  The ASF licenses this file
-   to you under the Apache License, Version 2.0 (the
-   "License"); you may not use this file except in compliance
-   with the License.  You may obtain a copy of the License at
-
-     http://www.apache.org/licenses/LICENSE-2.0
-
-   Unless required by applicable law or agreed to in writing,
-   software distributed under the License is distributed on an
-   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-   KIND, either express or implied.  See the License for the
-   specific language governing permissions and limitations
-   under the License.
+	Licensed to the Apache Software Foundation (ASF) under one
+	or more contributor license agreements.  See the NOTICE file
+	distributed with this work for additional information
+	regarding copyright ownership.  The ASF licenses this file
+	to you under the Apache License, Version 2.0 (the
+	"License"); you may not use this file except in compliance
+	with the License.  You may obtain a copy of the License at
+	
+	http://www.apache.org/licenses/LICENSE-2.0
+	
+	Unless required by applicable law or agreed to in writing,
+	software distributed under the License is distributed on an
+	"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+	KIND, either express or implied.  See the License for the
+	specific language governing permissions and limitations
+	under the License.
 -->
 <!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
@@ -23,5 +23,14 @@
 
 <web-app>
 
+	<filter>
+		<filter-name>jpa-filter</filter-name>
+		<filter-class>cayenne.example.jpa.JpaFilter</filter-class>
+	</filter>
+
+	<filter-mapping>
+		<filter-name>jpa-filter</filter-name>
+		<url-pattern>/*</url-pattern>
+	</filter-mapping>
 
 </web-app>

Modified: incubator/cayenne/sandbox/jpa-webapp-example/src/main/webapp/index.jsp
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/jpa-webapp-example/src/main/webapp/index.jsp?rev=439236&r1=439235&r2=439236&view=diff
==============================================================================
--- incubator/cayenne/sandbox/jpa-webapp-example/src/main/webapp/index.jsp (original)
+++ incubator/cayenne/sandbox/jpa-webapp-example/src/main/webapp/index.jsp Fri Sep  1 01:35:32
2006
@@ -18,15 +18,13 @@
 -->
 
 <%@ page language="java" contentType="text/html"%>
-<%@page import="javax.naming.InitialContext"%>
 <%@ page import="cayenne.example.jpa.*"%>
 <%@ page import="cayenne.example.jpa.entity.*"%>
 <%@ page import="javax.persistence.*"%>
 <%@ page import="java.util.*"%>
 
 <%
-	InitialContext context = new InitialContext();
-	EntityManager em = JpaHelper.getEntityManager();
+	EntityManager em = ThreadEntityManagers.getEntityManager();
 	Entity1 e = em.find(Entity1.class, 1);
 %>
 <html>



Mime
View raw message