Initial Contribution Signed-off-by: Jan Supol <jan.supol@oracle.com>
diff --git a/examples/helloworld-cdi2-se/README.MD b/examples/helloworld-cdi2-se/README.MD new file mode 100644 index 0000000..6e4026f --- /dev/null +++ b/examples/helloworld-cdi2-se/README.MD
@@ -0,0 +1,34 @@ +[//]: # " Copyright (c) 2015, 2018 Oracle and/or its affiliates. All rights reserved. " +[//]: # " " +[//]: # " This program and the accompanying materials are made available under the " +[//]: # " terms of the Eclipse Distribution License v. 1.0, which is available at " +[//]: # " http://www.eclipse.org/org/documents/edl-v10.php. " +[//]: # " " +[//]: # " SPDX-License-Identifier: BSD-3-Clause " + +Hello World and Counter Example +=============================== + +This example demonstrates Hello World and Counter examples with CDI 2 SE implemented by Weld. + +Contents +-------- + +The mapping of the URI path space is presented in the following table: + +URI path | Resource class | HTTP methods | Notes +--------------------------- | ------------------- | ------------ | -------------------------------------------------------- +**_/helloworld/{name}_** | HelloWorldResource | GET | Returns `Hello {name}` +**_/counter/request_** | CounterResource | GET | Returns always `1` (injected always a new instance of counter) +**_/counter/application_** | CounterResource | GET | Returns an incremented number (injected always the same counter). + +Running the Example +------------------- + +Run the example as follows: + +> mvn clean compile exec:java + +This deploys the example using [Grizzly](http://grizzly.java.net/) container. + +- <http://localhost:8080/helloworld>
diff --git a/examples/helloworld-cdi2-se/pom.xml b/examples/helloworld-cdi2-se/pom.xml new file mode 100644 index 0000000..ba3cfaa --- /dev/null +++ b/examples/helloworld-cdi2-se/pom.xml
@@ -0,0 +1,86 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved. + + This program and the accompanying materials are made available under the + terms of the Eclipse Distribution License v. 1.0, which is available at + http://www.eclipse.org/org/documents/edl-v10.php. + + SPDX-License-Identifier: BSD-3-Clause + +--> + +<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/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.glassfish.jersey.examples</groupId> + <artifactId>project</artifactId> + <version>2.28-SNAPSHOT</version> + </parent> + + <artifactId>helloworld-cdi2-se</artifactId> + <packaging>jar</packaging> + <name>jersey-examples-helloworld-cdi2-se</name> + + <description>Jersey "Hello world" example with CDI 2 SE.</description> + + <properties> + <cdi.api.version>2.0</cdi.api.version> + <weld.version>${weld3.version}</weld.version> + </properties> + + <dependencies> + <dependency> + <groupId>org.glassfish.jersey.inject</groupId> + <artifactId>jersey-cdi2-se</artifactId> + <version>${project.version}</version> + </dependency> + + <dependency> + <groupId>org.glassfish.jersey.containers</groupId> + <artifactId>jersey-container-grizzly2-http</artifactId> + </dependency> + + <dependency> + <groupId>org.glassfish.jersey.test-framework</groupId> + <artifactId>jersey-test-framework-util</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.glassfish.jersey.test-framework.providers</groupId> + <artifactId>jersey-test-framework-provider-bundle</artifactId> + <type>pom</type> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>exec-maven-plugin</artifactId> + <configuration> + <mainClass>java.org.glassfish.jersey.examples.helloworld.cdi2se.App</mainClass> + </configuration> + </plugin> + </plugins> + </build> + + <profiles> + <profile> + <id>release</id> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-assembly-plugin</artifactId> + </plugin> + </plugins> + </build> + </profile> + </profiles> + +</project>
diff --git a/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/App.java b/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/App.java new file mode 100644 index 0000000..d35233f --- /dev/null +++ b/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/App.java
@@ -0,0 +1,53 @@ +/* + * Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0, which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.glassfish.jersey.examples.helloworld.cdi2se; + +import java.io.IOException; +import java.net.URI; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; +import org.glassfish.jersey.server.ResourceConfig; + +import org.glassfish.grizzly.http.server.HttpServer; + +/** + * Hello world! + */ +public class App { + + private static final URI BASE_URI = URI.create("http://localhost:8080/base/"); + public static final String ROOT_HELLO_PATH = "helloworld"; + public static final String ROOT_COUNTER_PATH = "counter"; + + public static void main(String[] args) { + try { + System.out.println("\"Hello World\" Jersey Example App"); + + ResourceConfig resourceConfig = new ResourceConfig(HelloWorldResource.class, CounterResource.class); + HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, resourceConfig, false); + Runtime.getRuntime().addShutdownHook(new Thread(server::shutdownNow)); + + server.start(); + + System.out.println("Application started.\nTry out"); + System.out.println(String.format("%s%s", BASE_URI, ROOT_HELLO_PATH)); + System.out.println(String.format("%s%s%s", BASE_URI, ROOT_COUNTER_PATH, "/request")); + System.out.println(String.format("%s%s%s", BASE_URI, ROOT_COUNTER_PATH, "/application")); + System.out.println("Stop the application using CTRL+C"); + + Thread.currentThread().join(); + } catch (IOException | InterruptedException ex) { + Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); + } + } +}
diff --git a/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/ApplicationScopedCounter.java b/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/ApplicationScopedCounter.java new file mode 100644 index 0000000..119aa08 --- /dev/null +++ b/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/ApplicationScopedCounter.java
@@ -0,0 +1,28 @@ +/* + * Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0, which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.glassfish.jersey.examples.helloworld.cdi2se; + +import java.util.concurrent.atomic.AtomicInteger; + +import javax.enterprise.context.ApplicationScoped; + +/** + * Application-scoped counter. + */ +@ApplicationScoped +public class ApplicationScopedCounter { + + private final AtomicInteger counter = new AtomicInteger(); + + public int getNumber() { + return counter.incrementAndGet(); + } +}
diff --git a/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/CounterResource.java b/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/CounterResource.java new file mode 100644 index 0000000..519da94 --- /dev/null +++ b/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/CounterResource.java
@@ -0,0 +1,46 @@ +/* + * Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0, which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.glassfish.jersey.examples.helloworld.cdi2se; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; + +import javax.inject.Inject; + +/** + * Request-scoped resource counter. + * + * @author Petr Bouda + */ +@Path("counter") +public class CounterResource { + + @Inject + private RequestScopedCounter requestScoped; + + @Inject + private ApplicationScopedCounter applicationScoped; + + @GET + @Path("application") + @Produces("text/plain") + public int getAppCounter() { + return applicationScoped.getNumber(); + } + + @GET + @Path("request") + @Produces("text/plain") + public int getReqCounter() { + return requestScoped.getNumber(); + } +}
diff --git a/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/HelloBean.java b/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/HelloBean.java new file mode 100644 index 0000000..7a06759 --- /dev/null +++ b/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/HelloBean.java
@@ -0,0 +1,26 @@ +/* + * Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0, which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.glassfish.jersey.examples.helloworld.cdi2se; + +import javax.enterprise.context.ApplicationScoped; + +/** + * Application-scoped service returning "hello" sentence. + * + * @author Petr Bouda + */ +@ApplicationScoped +public class HelloBean { + + public String hello(String name) { + return "Hello " + name; + } +}
diff --git a/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/HelloWorldResource.java b/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/HelloWorldResource.java new file mode 100644 index 0000000..7799da3 --- /dev/null +++ b/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/HelloWorldResource.java
@@ -0,0 +1,39 @@ +/* + * Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0, which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.glassfish.jersey.examples.helloworld.cdi2se; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; + +import javax.inject.Inject; +import javax.inject.Singleton; + +/** + * Singleton-scoped resource. + * + * @author Petr Bouda + */ +@Singleton +@Path("helloworld") +public class HelloWorldResource { + + @Inject + private HelloBean helloBean; + + @GET + @Path("{name}") + @Produces("text/plain") + public String getHello(@PathParam("name") String name) { + return helloBean.hello(name); + } +}
diff --git a/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/RequestScopedCounter.java b/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/RequestScopedCounter.java new file mode 100644 index 0000000..d457878 --- /dev/null +++ b/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/RequestScopedCounter.java
@@ -0,0 +1,28 @@ +/* + * Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0, which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.glassfish.jersey.examples.helloworld.cdi2se; + +import java.util.concurrent.atomic.AtomicInteger; + +import javax.enterprise.context.RequestScoped; + +/** + * Request-scoped counter. + */ +@RequestScoped +public class RequestScopedCounter { + + private final AtomicInteger counter = new AtomicInteger(); + + public int getNumber() { + return counter.incrementAndGet(); + } +}
diff --git a/examples/helloworld-cdi2-se/src/main/resources/META-INF/beans.xml b/examples/helloworld-cdi2-se/src/main/resources/META-INF/beans.xml new file mode 100644 index 0000000..fc210a0 --- /dev/null +++ b/examples/helloworld-cdi2-se/src/main/resources/META-INF/beans.xml
@@ -0,0 +1,14 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved. + + This program and the accompanying materials are made available under the + terms of the Eclipse Distribution License v. 1.0, which is available at + http://www.eclipse.org/org/documents/edl-v10.php. + + SPDX-License-Identifier: BSD-3-Clause + +--> + +<beans/>
diff --git a/examples/helloworld-cdi2-se/src/test/java/org/glassfish/jersey/examples/helloworld/cdi2se/CounterTest.java b/examples/helloworld-cdi2-se/src/test/java/org/glassfish/jersey/examples/helloworld/cdi2se/CounterTest.java new file mode 100644 index 0000000..9017419 --- /dev/null +++ b/examples/helloworld-cdi2-se/src/test/java/org/glassfish/jersey/examples/helloworld/cdi2se/CounterTest.java
@@ -0,0 +1,44 @@ +/* + * Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0, which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.glassfish.jersey.examples.helloworld.cdi2se; + +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.test.JerseyTest; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +/** + * Tests CDI counter resource. + */ +public class CounterTest extends JerseyTest { + + @Override + protected ResourceConfig configure() { + return new ResourceConfig(CounterResource.class); + } + + @Test + public void testRequestCounter() throws InterruptedException { + Integer response1 = target().path(App.ROOT_COUNTER_PATH).path("request").request().get(Integer.class); + Integer response2 = target().path(App.ROOT_COUNTER_PATH).path("request").request().get(Integer.class); + assertEquals((Integer) 1, response1); + assertEquals((Integer) 1, response2); + } + + @Test + public void testApplicationCounter() throws InterruptedException { + Integer response1 = target().path(App.ROOT_COUNTER_PATH).path("application").request().get(Integer.class); + Integer response2 = target().path(App.ROOT_COUNTER_PATH).path("application").request().get(Integer.class); + assertEquals((Integer) 1, response1); + assertEquals((Integer) 2, response2); + } +}
diff --git a/examples/helloworld-cdi2-se/src/test/java/org/glassfish/jersey/examples/helloworld/cdi2se/HelloWorldTest.java b/examples/helloworld-cdi2-se/src/test/java/org/glassfish/jersey/examples/helloworld/cdi2se/HelloWorldTest.java new file mode 100644 index 0000000..0bba28a --- /dev/null +++ b/examples/helloworld-cdi2-se/src/test/java/org/glassfish/jersey/examples/helloworld/cdi2se/HelloWorldTest.java
@@ -0,0 +1,34 @@ +/* + * Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0, which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.glassfish.jersey.examples.helloworld.cdi2se; + +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.test.JerseyTest; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +/** + * Tests CDI helloworld resource. + */ +public class HelloWorldTest extends JerseyTest { + + @Override + protected ResourceConfig configure() { + return new ResourceConfig(HelloWorldResource.class); + } + + @Test + public void testHello() throws InterruptedException { + String response = target().path(App.ROOT_HELLO_PATH).path("James").request().get(String.class); + assertEquals("Hello James", response); + } +}