You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2011/07/26 00:15:30 UTC

svn commit: r1150934 - in /openejb/trunk/openejb3/examples/rest-on-ejb: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/superbiz/ src/main/java/org/superbiz/rest/ src/main/resources/ src/main/resources/META-INF/ src/test/ src/test...

Author: rmannibucau
Date: Mon Jul 25 22:15:29 2011
New Revision: 1150934

URL: http://svn.apache.org/viewvc?rev=1150934&view=rev
Log:
adding rest-on-ejb example - v1

Added:
    openejb/trunk/openejb3/examples/rest-on-ejb/
    openejb/trunk/openejb3/examples/rest-on-ejb/pom.xml
    openejb/trunk/openejb3/examples/rest-on-ejb/src/
    openejb/trunk/openejb3/examples/rest-on-ejb/src/main/
    openejb/trunk/openejb3/examples/rest-on-ejb/src/main/java/
    openejb/trunk/openejb3/examples/rest-on-ejb/src/main/java/org/
    openejb/trunk/openejb3/examples/rest-on-ejb/src/main/java/org/superbiz/
    openejb/trunk/openejb3/examples/rest-on-ejb/src/main/java/org/superbiz/rest/
    openejb/trunk/openejb3/examples/rest-on-ejb/src/main/java/org/superbiz/rest/User.java
    openejb/trunk/openejb3/examples/rest-on-ejb/src/main/java/org/superbiz/rest/UserService.java
    openejb/trunk/openejb3/examples/rest-on-ejb/src/main/resources/
    openejb/trunk/openejb3/examples/rest-on-ejb/src/main/resources/META-INF/
    openejb/trunk/openejb3/examples/rest-on-ejb/src/main/resources/META-INF/persistence.xml
    openejb/trunk/openejb3/examples/rest-on-ejb/src/test/
    openejb/trunk/openejb3/examples/rest-on-ejb/src/test/java/
    openejb/trunk/openejb3/examples/rest-on-ejb/src/test/java/org/
    openejb/trunk/openejb3/examples/rest-on-ejb/src/test/java/org/superbiz/
    openejb/trunk/openejb3/examples/rest-on-ejb/src/test/java/org/superbiz/rest/
    openejb/trunk/openejb3/examples/rest-on-ejb/src/test/java/org/superbiz/rest/UserServiceTest.java

Added: openejb/trunk/openejb3/examples/rest-on-ejb/pom.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/rest-on-ejb/pom.xml?rev=1150934&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/rest-on-ejb/pom.xml (added)
+++ openejb/trunk/openejb3/examples/rest-on-ejb/pom.xml Mon Jul 25 22:15:29 2011
@@ -0,0 +1,99 @@
+<?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.
+-->
+
+<!-- $Rev: 1090810 $ $Date: 2011-04-10 16:49:26 +0200 (dim., 10 avr. 2011) $ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         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>rest-on-ejb</artifactId>
+  <packaging>jar</packaging>
+  <version>1.1-SNAPSHOT</version>
+  <name>OpenEJB :: Examples :: REST and EJB</name>
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+  <build>
+    <defaultGoal>install</defaultGoal>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>2.3.2</version>
+        <configuration>
+          <source>1.6</source>
+          <target>1.6</target>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+  <repositories>
+    <repository>
+      <id>apache-m2-snapshot</id>
+      <name>Apache Snapshot Repository</name>
+      <url>http://repository.apache.org/snapshots</url>
+    </repository>
+  </repositories>
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.openejb</groupId>
+      <artifactId>javaee-api</artifactId>
+      <version>6.0-SNAPSHOT</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.8.1</version>
+      <scope>test</scope>
+    </dependency>
+    <!--
+    The <scope>test</scope> guarantees that non of your runtime
+    code is dependent on any OpenEJB classes.
+    -->
+    <dependency>
+      <groupId>org.apache.openejb</groupId>
+      <artifactId>openejb-core</artifactId>
+      <version>4.0.0-SNAPSHOT</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.openejb</groupId>
+      <artifactId>openejb-cxf-rs</artifactId>
+      <version>4.0.0-SNAPSHOT</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+  <!--
+  This section allows you to configure where to publish libraries for sharing.
+  It is not required and may be deleted.  For more information see:
+  http://maven.apache.org/plugins/maven-deploy-plugin/
+  -->
+  <distributionManagement>
+    <repository>
+      <id>localhost</id>
+      <url>file://${basedir}/target/repo/</url>
+    </repository>
+    <snapshotRepository>
+      <id>localhost</id>
+      <url>file://${basedir}/target/snapshot-repo/</url>
+    </snapshotRepository>
+  </distributionManagement>
+</project>
\ No newline at end of file

Added: openejb/trunk/openejb3/examples/rest-on-ejb/src/main/java/org/superbiz/rest/User.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/rest-on-ejb/src/main/java/org/superbiz/rest/User.java?rev=1150934&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/rest-on-ejb/src/main/java/org/superbiz/rest/User.java (added)
+++ openejb/trunk/openejb3/examples/rest-on-ejb/src/main/java/org/superbiz/rest/User.java Mon Jul 25 22:15:29 2011
@@ -0,0 +1,81 @@
+/*
+ * 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.rest;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
+import javax.xml.bind.annotation.XmlRootElement;
+
+/**
+ * @author Romain Manni-Bucau
+ */
+@Entity
+@NamedQueries({
+    @NamedQuery(name = "user.list", query = "select u from User u")
+})
+@XmlRootElement(name = "user")
+public class User implements Cloneable{
+    @Id @GeneratedValue private long id;
+    private String fullname;
+    private String password;
+    private String email;
+
+    public void setFullname(final String fullname) {
+        this.fullname = fullname;
+    }
+
+    public void setPassword(final String password) {
+        this.password = password;
+    }
+
+    public void setEmail(final String email) {
+        this.email = email;
+    }
+
+    public String getFullname() {
+        return fullname;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+
+    public String getEmail() {
+        return email;
+    }
+
+    public long getId() {
+        return id;
+    }
+
+    public void setId(long id) {
+        this.id = id;
+    }
+
+    public User copy(){
+        User user = new User();
+        user.setEmail(getEmail());
+        user.setFullname(getFullname());
+        user.setPassword(getPassword());
+        user.setId(getId());
+        return user;
+    }
+}

Added: openejb/trunk/openejb3/examples/rest-on-ejb/src/main/java/org/superbiz/rest/UserService.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/rest-on-ejb/src/main/java/org/superbiz/rest/UserService.java?rev=1150934&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/rest-on-ejb/src/main/java/org/superbiz/rest/UserService.java (added)
+++ openejb/trunk/openejb3/examples/rest-on-ejb/src/main/java/org/superbiz/rest/UserService.java Mon Jul 25 22:15:29 2011
@@ -0,0 +1,89 @@
+/*
+ * 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.rest;
+
+import javax.ejb.Lock;
+import javax.ejb.LockType;
+import javax.ejb.Singleton;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.DefaultValue;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.MediaType;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Outputs are copied because of the enhancement of OpenJPA.
+ *
+ * @author Romain Manni-Bucau
+ */
+@Singleton
+@Lock(LockType.WRITE)
+@Path("/user")
+@Produces(MediaType.APPLICATION_XML)
+public class UserService {
+    @PersistenceContext private EntityManager em;
+
+    @Path("/create") @PUT public User create(@QueryParam("name") String name,
+                                             @QueryParam("pwd") String pwd,
+                                             @QueryParam("mail") String mail) {
+        User user = new User();
+        user.setFullname(name);
+        user.setPassword(pwd);
+        user.setEmail(mail);
+        em.persist(user);
+        return user;
+    }
+
+    @Path("/list") @GET public List<User> list(@QueryParam("first") @DefaultValue("0") int first,
+                           @QueryParam("max") @DefaultValue("20") int max) {
+        List<User> users = new ArrayList<User>();
+        List<User> found = em.createNamedQuery("user.list", User.class).setFirstResult(first).setMaxResults(max).getResultList();
+        for (User u : found) {
+            users.add(u.copy());
+        }
+        return users;
+    }
+
+    @Path("/show/{id}") @GET public User find(@PathParam("id") long id) {
+        return em.find(User.class, id).copy();
+    }
+
+    @Path("/delete") @DELETE public void delete(long id) {
+        em.remove(id);
+    }
+
+    @Path("/update") @POST public User update(long id, String name, String pwd, String mail) {
+        User user = em.find(User.class, id);
+        if (user == null) {
+            throw  new IllegalArgumentException("setUser id " + id + " not found");
+        }
+
+        user.setFullname(name);
+        user.setPassword(pwd);
+        user.setEmail(mail);
+        return em.merge(user);
+    }
+}

Added: openejb/trunk/openejb3/examples/rest-on-ejb/src/main/resources/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/rest-on-ejb/src/main/resources/META-INF/persistence.xml?rev=1150934&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/rest-on-ejb/src/main/resources/META-INF/persistence.xml (added)
+++ openejb/trunk/openejb3/examples/rest-on-ejb/src/main/resources/META-INF/persistence.xml Mon Jul 25 22:15:29 2011
@@ -0,0 +1,31 @@
+<?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.
+  -->
+<persistence version="2.0"
+   xmlns="http://java.sun.com/xml/ns/persistence"
+   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
+                       http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
+  <persistence-unit name="user">
+    <jta-data-source>My DataSource</jta-data-source>
+    <non-jta-data-source>My Unmanaged DataSource</non-jta-data-source>
+    <class>org.superbiz.rest.User</class>
+    <properties>
+      <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
+    </properties>
+  </persistence-unit>
+</persistence>
\ No newline at end of file

Added: openejb/trunk/openejb3/examples/rest-on-ejb/src/test/java/org/superbiz/rest/UserServiceTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/rest-on-ejb/src/test/java/org/superbiz/rest/UserServiceTest.java?rev=1150934&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/rest-on-ejb/src/test/java/org/superbiz/rest/UserServiceTest.java (added)
+++ openejb/trunk/openejb3/examples/rest-on-ejb/src/test/java/org/superbiz/rest/UserServiceTest.java Mon Jul 25 22:15:29 2011
@@ -0,0 +1,91 @@
+/*
+ * 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.rest;
+
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.openejb.OpenEjbContainer;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import sun.awt.geom.AreaOp;
+
+import javax.ejb.embeddable.EJBContainer;
+import javax.naming.Context;
+import javax.naming.NamingException;
+import javax.xml.bind.JAXBContext;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+import static junit.framework.Assert.assertEquals;
+
+/**
+ * In standalone only GET and POST are managed.
+ *
+ * @author Romain Manni-Bucau
+ */
+public class UserServiceTest {
+    private static Context context;
+
+    @BeforeClass public static void start() throws NamingException {
+        Properties properties = new Properties();
+        properties.setProperty(OpenEjbContainer.OPENEJB_EMBEDDED_REMOTABLE, "true");
+        context = EJBContainer.createEJBContainer(properties).getContext();
+
+        // create some records
+        UserService service = (UserService) context.lookup("java:global/rest-on-ejb/UserService");
+        service.create("foo", "foopwd", "foo@foo.com");
+        service.create("bar", "barpwd", "bar@bar.com");
+    }
+
+    @AfterClass public static void close() throws NamingException {
+        if (context != null) {
+            context.close();
+        }
+    }
+
+    @Test public void show() {
+        User user = WebClient.create("http://localhost:4204")
+                .path("/user/show/1")
+                .get(User.class);
+        assertEquals("foo", user.getFullname());
+        assertEquals("foopwd", user.getPassword());
+        assertEquals("foo@foo.com", user.getEmail());
+    }
+
+    @Test public void list() throws Exception {
+        String users = WebClient.create("http://localhost:4204")
+                .path("/user/list")
+                .get(String.class);
+        assertEquals(
+            "<users>" +
+                "<user>" +
+                    "<email>foo@foo.com</email>" +
+                    "<fullname>foo</fullname>" +
+                    "<id>1</id>" +
+                    "<password>foopwd</password>" +
+                "</user>" +
+                "<user>" +
+                    "<email>bar@bar.com</email>" +
+                    "<fullname>bar</fullname>" +
+                    "<id>2</id>" +
+                    "<password>barpwd</password>" +
+                "</user>" +
+            "</users>", users);
+    }
+}