You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by bd...@apache.org on 2022/12/07 22:51:19 UTC

[directory-scimple] 01/01: Configure and run the ITs on the Jersey example

This is an automated email from the ASF dual-hosted git repository.

bdemers pushed a commit to branch run-its-jersey-example
in repository https://gitbox.apache.org/repos/asf/directory-scimple.git

commit a78d0325d1e498fd7a700a4f83b0b753e185562f
Author: Brian Demers <bd...@apache.org>
AuthorDate: Wed Dec 7 17:51:12 2022 -0500

    Configure and run the ITs on the Jersey example
    
    Update example runner to use standard Jakarta bootstraping instead of implementation specific code
---
 .../compliance/junit/EmbeddedServerExtension.java  |  4 +-
 scim-server-examples/scim-server-jersey/pom.xml    | 11 ++++
 .../scim/example/jersey/JerseyApplication.java     | 46 ++++++---------
 .../src/main/resources/beans.xml                   |  2 +-
 .../scim/example/jersey/JerseyTestServer.java      | 66 ++++++++++++++++++++++
 ...ce.junit.EmbeddedServerExtension$ScimTestServer |  1 +
 .../directory/scim/server/it/JerseyTestServer.java | 47 ++++++++-------
 7 files changed, 126 insertions(+), 51 deletions(-)

diff --git a/scim-compliance-tests/src/main/java/org/apache/directory/scim/compliance/junit/EmbeddedServerExtension.java b/scim-compliance-tests/src/main/java/org/apache/directory/scim/compliance/junit/EmbeddedServerExtension.java
index 2cbb0c27..9b65d3d0 100644
--- a/scim-compliance-tests/src/main/java/org/apache/directory/scim/compliance/junit/EmbeddedServerExtension.java
+++ b/scim-compliance-tests/src/main/java/org/apache/directory/scim/compliance/junit/EmbeddedServerExtension.java
@@ -86,7 +86,7 @@ public class EmbeddedServerExtension implements BeforeAllCallback, BeforeEachCal
   }
 
   public interface ScimTestServer {
-    URI start(int port);
-    void shutdown();
+    URI start(int port) throws Exception;
+    void shutdown() throws Exception;
   }
 }
diff --git a/scim-server-examples/scim-server-jersey/pom.xml b/scim-server-examples/scim-server-jersey/pom.xml
index ba08789b..3649c766 100644
--- a/scim-server-examples/scim-server-jersey/pom.xml
+++ b/scim-server-examples/scim-server-jersey/pom.xml
@@ -35,22 +35,27 @@
     <dependency>
       <groupId>org.glassfish.jersey.containers</groupId>
       <artifactId>jersey-container-grizzly2-http</artifactId>
+      <scope>runtime</scope>
     </dependency>
     <dependency>
       <groupId>org.glassfish.jersey.inject</groupId>
       <artifactId>jersey-hk2</artifactId>
+      <scope>runtime</scope>
     </dependency>
     <dependency>
       <groupId>org.glassfish.jersey.ext.cdi</groupId>
       <artifactId>jersey-weld2-se</artifactId>
+      <scope>runtime</scope>
     </dependency>
     <dependency>
       <groupId>org.jboss.weld.se</groupId>
       <artifactId>weld-se-core</artifactId>
+      <scope>runtime</scope>
     </dependency>
     <dependency>
       <groupId>jakarta.servlet</groupId>
       <artifactId>jakarta.servlet-api</artifactId>
+      <scope>runtime</scope>
     </dependency>
 
     <dependency>
@@ -76,6 +81,12 @@
       <artifactId>lombok</artifactId>
       <scope>provided</scope>
     </dependency>
+
+    <dependency>
+      <groupId>org.apache.directory.scim</groupId>
+      <artifactId>scim-compliance-tests</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <build>
diff --git a/scim-server-examples/scim-server-jersey/src/main/java/org/apache/directory/scim/example/jersey/JerseyApplication.java b/scim-server-examples/scim-server-jersey/src/main/java/org/apache/directory/scim/example/jersey/JerseyApplication.java
index 3e8eb59d..fb4a94ba 100644
--- a/scim-server-examples/scim-server-jersey/src/main/java/org/apache/directory/scim/example/jersey/JerseyApplication.java
+++ b/scim-server-examples/scim-server-jersey/src/main/java/org/apache/directory/scim/example/jersey/JerseyApplication.java
@@ -19,7 +19,11 @@
 
 package org.apache.directory.scim.example.jersey;
 
+import jakarta.enterprise.context.ApplicationScoped;
 import jakarta.enterprise.inject.Produces;
+import jakarta.enterprise.inject.se.SeContainer;
+import jakarta.enterprise.inject.se.SeContainerInitializer;
+import jakarta.ws.rs.SeBootstrap;
 import jakarta.ws.rs.core.UriBuilder;
 import org.apache.directory.scim.server.configuration.ServerConfiguration;
 
@@ -28,20 +32,17 @@ import java.util.Set;
 
 import jakarta.ws.rs.core.Application;
 import org.apache.directory.scim.server.rest.ScimpleFeature;
-import org.glassfish.grizzly.http.server.HttpServer;
-import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
-import org.glassfish.jersey.server.ResourceConfig;
-import org.jboss.weld.environment.se.Weld;
 import org.slf4j.LoggerFactory;
 import org.slf4j.bridge.SLF4JBridgeHandler;
 
+import static org.apache.directory.scim.spec.schema.ServiceProviderConfiguration.AuthenticationSchema.oauthBearer;
+
 // @ApplicationPath("v2")
 // Embedded Jersey + Jetty ignores the ApplicationPath annotation
 // https://github.com/eclipse-ee4j/jersey/issues/3222
+@ApplicationScoped
 public class JerseyApplication extends Application {
 
-  private static final String BASE_URI = "http://localhost:8080/";
-
   private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(JerseyApplication.class);
   
   @Override
@@ -54,10 +55,9 @@ public class JerseyApplication extends Application {
     return new ServerConfiguration()
       // Set any unique configuration bits
       .setId("scimple-jersey-example")
-      .setDocumentationUri("https://github.com/apache/directory-scimple");
-
+      .setDocumentationUri("https://github.com/apache/directory-scimple")
     // set the auth scheme too
-    // .addAuthenticationSchema(oauthBearer());
+     .addAuthenticationSchema(oauthBearer());
   }
 
   public static void main(String[] args) {
@@ -66,27 +66,17 @@ public class JerseyApplication extends Application {
     SLF4JBridgeHandler.install();
 
     try {
-      Weld weld = new Weld();
-      // ensure Weld discovers the beans in this project
-      weld.addPackages(true, JerseyApplication.class.getPackage());
-      weld.initialize();
 
-      ResourceConfig resourceConfig = ResourceConfig.forApplication(new JerseyApplication());
+      SeContainer container = SeContainerInitializer.newInstance()
+        .addPackages(true, JerseyApplication.class)
+        .initialize();
+
+      JerseyApplication app = new JerseyApplication();
+      SeBootstrap.start(app, SeBootstrap.Configuration.builder().port(8080).build())
+        .thenAccept(instance -> instance.stopOnShutdown(stopResult -> container.close()));
       URI uri = UriBuilder.fromUri("http://localhost/").port(8080).build();
-      final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(uri, resourceConfig);
-
-      Runtime.getRuntime().addShutdownHook(new Thread(() -> {
-        try {
-          System.out.println("Shutting down the application...");
-          server.shutdown();
-          weld.shutdown();
-          System.out.println("Done, exit.");
-        } catch (Exception e) {
-          LOG.error("Failed to shutdown service", e);
-        }
-      }));
-
-      System.out.printf("Application started: %s\nStop the application using CTRL+C%n", BASE_URI);
+
+      System.out.printf("Application started: %s\nStop the application using CTRL+C%n", uri.toString());
 
       // block and wait shut down signal, like CTRL+C
       Thread.currentThread().join();
diff --git a/scim-server-examples/scim-server-jersey/src/main/resources/beans.xml b/scim-server-examples/scim-server-jersey/src/main/resources/beans.xml
index a47f43b0..92294177 100644
--- a/scim-server-examples/scim-server-jersey/src/main/resources/beans.xml
+++ b/scim-server-examples/scim-server-jersey/src/main/resources/beans.xml
@@ -17,6 +17,6 @@
 <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
-       version="1.1" bean-discovery-mode="all">
+       version="4.0" bean-discovery-mode="annotated">
        
 </beans>
diff --git a/scim-server-examples/scim-server-jersey/src/test/java/org/apache/directory/scim/example/jersey/JerseyTestServer.java b/scim-server-examples/scim-server-jersey/src/test/java/org/apache/directory/scim/example/jersey/JerseyTestServer.java
new file mode 100644
index 00000000..fc2fc2c6
--- /dev/null
+++ b/scim-server-examples/scim-server-jersey/src/test/java/org/apache/directory/scim/example/jersey/JerseyTestServer.java
@@ -0,0 +1,66 @@
+/*
+ * 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.apache.directory.scim.example.jersey;
+
+import jakarta.enterprise.inject.se.SeContainer;
+import jakarta.enterprise.inject.se.SeContainerInitializer;
+import jakarta.ws.rs.SeBootstrap;
+import jakarta.ws.rs.core.UriBuilder;
+import org.apache.directory.scim.compliance.junit.EmbeddedServerExtension;
+
+import java.net.URI;
+import java.util.concurrent.TimeUnit;
+
+public class JerseyTestServer implements EmbeddedServerExtension.ScimTestServer {
+
+  private SeContainer container;
+  private SeBootstrap.Instance server;
+
+  @Override
+  public URI start(int port) throws Exception {
+
+    // It doesn't look like Weld finds the beans in src/main/java, so enable implicit scanning
+    // NOTE: this isn't an issue for the scim-server tests, but those beans are located in src/test/java
+    container = SeContainerInitializer.newInstance()
+      .addPackages(true, JerseyApplication.class)
+      .initialize();
+
+    JerseyApplication app = new JerseyApplication();
+    server = SeBootstrap.start(app, SeBootstrap.Configuration.builder().port(port).build())
+      .toCompletableFuture().get(1, TimeUnit.MINUTES);
+
+    // shut down CDI container on stop
+    server.stopOnShutdown(stopResult -> container.close());
+
+    return UriBuilder.fromUri("http://localhost/").port(port).build();
+  }
+
+  @Override
+  public void shutdown() throws Exception {
+    if (server != null) {
+      server.stop().toCompletableFuture()
+        .get(10, TimeUnit.SECONDS);
+    }
+
+    if (container != null) {
+      container.close();
+    }
+  }
+}
diff --git a/scim-server-examples/scim-server-jersey/src/test/resources/META-INF/services/org.apache.directory.scim.compliance.junit.EmbeddedServerExtension$ScimTestServer b/scim-server-examples/scim-server-jersey/src/test/resources/META-INF/services/org.apache.directory.scim.compliance.junit.EmbeddedServerExtension$ScimTestServer
new file mode 100644
index 00000000..4997cd08
--- /dev/null
+++ b/scim-server-examples/scim-server-jersey/src/test/resources/META-INF/services/org.apache.directory.scim.compliance.junit.EmbeddedServerExtension$ScimTestServer
@@ -0,0 +1 @@
+org.apache.directory.scim.example.jersey.JerseyTestServer
diff --git a/scim-server/src/test/java/org/apache/directory/scim/server/it/JerseyTestServer.java b/scim-server/src/test/java/org/apache/directory/scim/server/it/JerseyTestServer.java
index 9cd6676c..52559ed3 100644
--- a/scim-server/src/test/java/org/apache/directory/scim/server/it/JerseyTestServer.java
+++ b/scim-server/src/test/java/org/apache/directory/scim/server/it/JerseyTestServer.java
@@ -19,43 +19,50 @@
 
 package org.apache.directory.scim.server.it;
 
+import jakarta.enterprise.inject.se.SeContainer;
+import jakarta.enterprise.inject.se.SeContainerInitializer;
+import jakarta.ws.rs.SeBootstrap;
 import jakarta.ws.rs.core.UriBuilder;
 import org.apache.directory.scim.compliance.junit.EmbeddedServerExtension;
-import org.glassfish.grizzly.http.server.HttpServer;
-import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
-import org.glassfish.jersey.server.ResourceConfig;
-import org.jboss.weld.environment.se.Weld;
-
 import org.apache.directory.scim.server.it.testapp.App;
+import org.glassfish.jersey.server.JerseySeBootstrapConfiguration;
+import org.glassfish.jersey.server.internal.RuntimeDelegateImpl;
 
 import java.net.URI;
+import java.util.concurrent.TimeUnit;
 
 public class JerseyTestServer implements EmbeddedServerExtension.ScimTestServer {
 
-  private HttpServer server;
-  private Weld weld;
+  private SeBootstrap.Instance server;
+
+  private SeContainer container;
 
   @Override
-  public URI start(int port) {
-    weld = new Weld();
-    // ensure Weld discovers the beans in this project
-    weld.addPackages(true, App.class.getPackage());
-    weld.initialize();
+  public URI start(int port) throws Exception {
+    container = SeContainerInitializer.newInstance()
+      .addPackages(true, App.class.getPackage())
+      .initialize();
 
-    ResourceConfig rc = ResourceConfig.forApplication(new App());
-    URI uri = UriBuilder.fromUri("http://localhost/").port(port).build();
-    server = GrizzlyHttpServerFactory.createHttpServer(uri, rc);
+    // There are multiple JAX-RS implementations on the classpath, Jersey for the server and RestEasy for testing
+    // explicitly use Jersey so the test implementation is not use to start the server
+     server = new RuntimeDelegateImpl().bootstrap(new App(), JerseySeBootstrapConfiguration.builder().port(port).build())
+      .toCompletableFuture().get(1, TimeUnit.MINUTES);
 
-    return uri;
+    // shut down CDI container on stop
+    server.stopOnShutdown(stopResult -> container.close());
+
+    return UriBuilder.fromUri("http://localhost/").port(port).build();
   }
 
   @Override
-  public void shutdown() {
+  public void shutdown() throws Exception {
     if (server != null) {
-      server.shutdown();
+      server.stop().toCompletableFuture()
+        .get(10, TimeUnit.SECONDS);
     }
-    if (weld != null) {
-      weld.shutdown();
+
+    if (container != null) {
+      container.close();
     }
   }
 }