Author: dblevins
Date: Tue Apr 12 00:43:02 2011
New Revision: 1091264

URL: http://svn.apache.org/viewvc?rev=1091264&view=rev
Log:
More combined meta-data example

Added:
    openejb/trunk/openejb3/examples/movies-complete/
      - copied from r1088039, 
openejb/trunk/openejb3/examples/testing-transactions/
    openejb/trunk/openejb3/examples/movies-complete/build.xml
      - copied unchanged from r1090810, 
openejb/trunk/openejb3/examples/testing-transactions/build.xml
    openejb/trunk/openejb3/examples/movies-complete/pom.xml
      - copied, changed from r1090810, 
openejb/trunk/openejb3/examples/testing-transactions/pom.xml
    
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/DeleteInterceptor.java
   (with props)
    
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/Movies.java
      - copied, changed from r1091016, 
openejb/trunk/openejb3/examples/testing-transactions/src/main/java/org/superbiz/injection/tx/Movies.java
    
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/MoviesMeta.java
   (with props)
    
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/
    
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/Add.java
   (with props)
    
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/Delete.java
   (with props)
    
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/Metatype.java
   (with props)
    
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/Read.java
   (with props)
    
openejb/trunk/openejb3/examples/movies-complete/src/main/resources/META-INF/persistence.xml
      - copied unchanged from r1090810, 
openejb/trunk/openejb3/examples/testing-transactions/src/main/resources/META-INF/persistence.xml
    
openejb/trunk/openejb3/examples/movies-complete/src/test/java/org/superbiz/injection/tx/MoviesTest.java
   (with props)
Removed:
    
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/MoviesImpl.java
    
openejb/trunk/openejb3/examples/movies-complete/src/main/resources/META-INF/ejb-jar.xml
    openejb/trunk/openejb3/examples/movies-complete/src/test/resources/

Copied: openejb/trunk/openejb3/examples/movies-complete/pom.xml (from r1090810, 
openejb/trunk/openejb3/examples/testing-transactions/pom.xml)
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/movies-complete/pom.xml?p2=openejb/trunk/openejb3/examples/movies-complete/pom.xml&p1=openejb/trunk/openejb3/examples/testing-transactions/pom.xml&r1=1090810&r2=1091264&rev=1091264&view=diff
==============================================================================
--- openejb/trunk/openejb3/examples/testing-transactions/pom.xml (original)
+++ openejb/trunk/openejb3/examples/movies-complete/pom.xml Tue Apr 12 00:43:02 
2011
@@ -23,10 +23,10 @@
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
   <modelVersion>4.0.0</modelVersion>
   <groupId>org.superbiz</groupId>
-  <artifactId>testing-transactions</artifactId>
+  <artifactId>movies-complete</artifactId>
   <packaging>jar</packaging>
   <version>1.1-SNAPSHOT</version>
-  <name>OpenEJB :: Examples :: Testing Transactions</name>
+  <name>OpenEJB :: Examples :: Movies Complete</name>
   <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>

Added: 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/DeleteInterceptor.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/DeleteInterceptor.java?rev=1091264&view=auto
==============================================================================
--- 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/DeleteInterceptor.java
 (added)
+++ 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/DeleteInterceptor.java
 Tue Apr 12 00:43:02 2011
@@ -0,0 +1,32 @@
+/**
+ * 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 org.superbiz.injection.tx;
+
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.InvocationContext;
+
+/**
+ * @version $Revision$ $Date$
+ */
+public class DeleteInterceptor {
+
+    @AroundInvoke
+    public Object invoke(InvocationContext context) throws Exception {
+        // Log Delete
+        return context.proceed();
+    }
+}

Propchange: 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/DeleteInterceptor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Copied: 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/Movies.java
 (from r1091016, 
openejb/trunk/openejb3/examples/testing-transactions/src/main/java/org/superbiz/injection/tx/Movies.java)
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/Movies.java?p2=openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/Movies.java&p1=openejb/trunk/openejb3/examples/testing-transactions/src/main/java/org/superbiz/injection/tx/Movies.java&r1=1091016&r2=1091264&rev=1091264&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/examples/testing-transactions/src/main/java/org/superbiz/injection/tx/Movies.java
 (original)
+++ 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/Movies.java
 Tue Apr 12 00:43:02 2011
@@ -16,8 +16,12 @@
  */
 package org.superbiz.injection.tx;
 
+import javax.annotation.security.PermitAll;
+import javax.annotation.security.RolesAllowed;
 import javax.ejb.Stateful;
 import javax.ejb.TransactionAttribute;
+import javax.ejb.TransactionAttributeType;
+import javax.interceptor.Interceptors;
 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceContext;
 import javax.persistence.PersistenceContextType;
@@ -27,21 +31,28 @@ import java.util.List;
 import static javax.ejb.TransactionAttributeType.MANDATORY;
 
 //START SNIPPET: code
-@Stateful(name = "Movies")
-@TransactionAttribute(MANDATORY)
+@Stateful
 public class Movies {
 
     @PersistenceContext(unitName = "movie-unit", type = 
PersistenceContextType.TRANSACTION)
     private EntityManager entityManager;
 
+    @RolesAllowed({"Employee", "Manager"})
+    @TransactionAttribute(TransactionAttributeType.REQUIRED)
+    @Interceptors(AddInterceptor.class)
     public void addMovie(Movie movie) throws Exception {
         entityManager.persist(movie);
     }
 
+    @RolesAllowed({"Manager"})
+    @TransactionAttribute(TransactionAttributeType.MANDATORY)
+    @Interceptors(DeleteInterceptor.class)
     public void deleteMovie(Movie movie) throws Exception {
         entityManager.remove(movie);
     }
 
+    @PermitAll
+    @TransactionAttribute(TransactionAttributeType.SUPPORTS)
     public List<Movie> getMovies() throws Exception {
         Query query = entityManager.createQuery("SELECT m from Movie as m");
         return query.getResultList();

Added: 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/MoviesMeta.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/MoviesMeta.java?rev=1091264&view=auto
==============================================================================
--- 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/MoviesMeta.java
 (added)
+++ 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/MoviesMeta.java
 Tue Apr 12 00:43:02 2011
@@ -0,0 +1,60 @@
+/**
+ * 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 org.superbiz.injection.tx;
+
+import org.superbiz.injection.tx.api.Add;
+import org.superbiz.injection.tx.api.Delete;
+import org.superbiz.injection.tx.api.MovieUnit;
+import org.superbiz.injection.tx.api.Read;
+
+import javax.annotation.security.PermitAll;
+import javax.annotation.security.RolesAllowed;
+import javax.ejb.Stateful;
+import javax.ejb.TransactionAttribute;
+import javax.ejb.TransactionAttributeType;
+import javax.interceptor.Interceptors;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceContextType;
+import javax.persistence.Query;
+import java.util.List;
+
+//END SNIPPET: code
+
+//START SNIPPET: code
+@Stateful
+public class MoviesMeta {
+
+    @MovieUnit
+    private EntityManager entityManager;
+
+    @Add
+    public void addMovie(Movie movie) throws Exception {
+        entityManager.persist(movie);
+    }
+
+    @Delete
+    public void deleteMovie(Movie movie) throws Exception {
+        entityManager.remove(movie);
+    }
+
+    @Read
+    public List<Movie> getMovies() throws Exception {
+        Query query = entityManager.createQuery("SELECT m from Movie as m");
+        return query.getResultList();
+    }
+}
\ No newline at end of file

Propchange: 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/MoviesMeta.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/Add.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/Add.java?rev=1091264&view=auto
==============================================================================
--- 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/Add.java
 (added)
+++ 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/Add.java
 Tue Apr 12 00:43:02 2011
@@ -0,0 +1,27 @@
+package org.superbiz.injection.tx.api;
+
+import org.superbiz.injection.tx.AddInterceptor;
+
+import javax.annotation.security.RolesAllowed;
+import javax.ejb.TransactionAttribute;
+import javax.ejb.TransactionAttributeType;
+import javax.interceptor.Interceptors;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Metatype
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+
+public @interface Add {
+    public interface $ {
+
+        @Add
+        @RolesAllowed({"Employee", "Manager"})
+        @TransactionAttribute(TransactionAttributeType.REQUIRED)
+        @Interceptors(AddInterceptor.class)
+        public void method();
+    }
+}
\ No newline at end of file

Propchange: 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/Add.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/Delete.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/Delete.java?rev=1091264&view=auto
==============================================================================
--- 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/Delete.java
 (added)
+++ 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/Delete.java
 Tue Apr 12 00:43:02 2011
@@ -0,0 +1,28 @@
+package org.superbiz.injection.tx.api;
+
+import org.superbiz.injection.tx.DeleteInterceptor;
+
+import javax.annotation.security.PermitAll;
+import javax.annotation.security.RolesAllowed;
+import javax.ejb.TransactionAttribute;
+import javax.ejb.TransactionAttributeType;
+import javax.interceptor.Interceptors;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Metatype
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+
+public @interface Delete {
+    public interface $ {
+
+        @Delete
+        @RolesAllowed({"Manager"})
+        @TransactionAttribute(TransactionAttributeType.MANDATORY)
+        @Interceptors(DeleteInterceptor.class)
+        public void method();
+    }
+}
\ No newline at end of file

Propchange: 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/Delete.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/Metatype.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/Metatype.java?rev=1091264&view=auto
==============================================================================
--- 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/Metatype.java
 (added)
+++ 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/Metatype.java
 Tue Apr 12 00:43:02 2011
@@ -0,0 +1,25 @@
+/**
+ * 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 org.superbiz.injection.tx.api;
+
+import java.lang.annotation.*;
+
+@Metatype
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.ANNOTATION_TYPE)
+public @interface Metatype {
+}

Propchange: 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/Metatype.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/Read.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/Read.java?rev=1091264&view=auto
==============================================================================
--- 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/Read.java
 (added)
+++ 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/Read.java
 Tue Apr 12 00:43:02 2011
@@ -0,0 +1,23 @@
+package org.superbiz.injection.tx.api;
+
+import javax.annotation.security.PermitAll;
+import javax.ejb.TransactionAttribute;
+import javax.ejb.TransactionAttributeType;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Metatype
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+
+public @interface Read {
+    public interface $ {
+
+        @Read
+        @PermitAll
+        @TransactionAttribute(TransactionAttributeType.SUPPORTS)
+        public void method();
+    }
+}

Propchange: 
openejb/trunk/openejb3/examples/movies-complete/src/main/java/org/superbiz/injection/tx/api/Read.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
openejb/trunk/openejb3/examples/movies-complete/src/test/java/org/superbiz/injection/tx/MoviesTest.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/movies-complete/src/test/java/org/superbiz/injection/tx/MoviesTest.java?rev=1091264&view=auto
==============================================================================
--- 
openejb/trunk/openejb3/examples/movies-complete/src/test/java/org/superbiz/injection/tx/MoviesTest.java
 (added)
+++ 
openejb/trunk/openejb3/examples/movies-complete/src/test/java/org/superbiz/injection/tx/MoviesTest.java
 Tue Apr 12 00:43:02 2011
@@ -0,0 +1,129 @@
+/**
+ * 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 org.superbiz.injection.tx;
+
+import junit.framework.TestCase;
+
+import javax.annotation.security.RunAs;
+import javax.ejb.EJB;
+import javax.ejb.Stateless;
+import javax.ejb.TransactionAttribute;
+import javax.ejb.TransactionAttributeType;
+import javax.ejb.embeddable.EJBContainer;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.Callable;
+
+import static javax.ejb.TransactionAttributeType.REQUIRES_NEW;
+
+/**
+ * See the transaction-rollback example as it does the same thing
+ * via UserTransaction and shows more techniques for rollback 
+ */
+//START SNIPPET: code
+public class MoviesTest extends TestCase {
+
+    @EJB
+    private Movies movies;
+
+    @EJB(beanName = "TransactionBean")
+    private Caller transactionalCaller;
+
+    @EJB(beanName = "NoTransactionBean")
+    private Caller nonTransactionalCaller;
+
+    protected void setUp() throws Exception {
+        final Properties p = new Properties();
+        p.put("movieDatabase", "new://Resource?type=DataSource");
+        p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
+        p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
+
+        EJBContainer.createEJBContainer(p).getContext().bind("inject", this);
+    }
+
+    private void doWork() throws Exception {
+
+        movies.addMovie(new Movie("Quentin Tarantino", "Reservoir Dogs", 
1992));
+        movies.addMovie(new Movie("Joel Coen", "Fargo", 1996));
+        movies.addMovie(new Movie("Joel Coen", "The Big Lebowski", 1998));
+
+        List<Movie> list = movies.getMovies();
+        assertEquals("List.size()", 3, list.size());
+
+        for (Movie movie : list) {
+            movies.deleteMovie(movie);
+        }
+
+        assertEquals("Movies.getMovies()", 0, movies.getMovies().size());
+    }
+
+    public void testWithTransaction() throws Exception {
+        transactionalCaller.call(new Callable() {
+            public Object call() throws Exception {
+                doWork();
+                return null;
+            }
+        });
+    }
+
+    public void testWithoutTransaction() throws Exception {
+        try {
+            nonTransactionalCaller.call(new Callable() {
+                public Object call() throws Exception {
+                    doWork();
+                    return null;
+                }
+            });
+            fail("The Movies bean should be using 
TransactionAttributeType.MANDATORY");
+        } catch (javax.ejb.EJBException e) {
+            // good, our Movies bean is using 
TransactionAttributeType.MANDATORY as we want
+        }
+    }
+
+
+    public static interface Caller {
+        public <V> V call(Callable<V> callable) throws Exception;
+    }
+
+    /**
+     * This little bit of magic allows our test code to execute in
+     * the scope of a container controlled transaction.
+     */
+    @Stateless
+    @RunAs("Manager")
+    @TransactionAttribute(REQUIRES_NEW)
+    public static class TransactionBean implements Caller {
+
+        public <V> V call(Callable<V> callable) throws Exception {
+            return callable.call();
+        }
+
+    }
+
+    @Stateless
+    @RunAs("Manager")
+    @TransactionAttribute(TransactionAttributeType.NEVER)
+    public static class NoTransactionBean implements Caller {
+
+        public <V> V call(Callable<V> callable) throws Exception {
+            return callable.call();
+        }
+
+    }
+
+}
+//END SNIPPET: code

Propchange: 
openejb/trunk/openejb3/examples/movies-complete/src/test/java/org/superbiz/injection/tx/MoviesTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to