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

radcortez pushed a commit to branch TOMEE-2408
in repository https://gitbox.apache.org/repos/asf/tomee.git

commit 8af6ae2bb4e1b1d362a21586c6bb648767eb2a5b
Author: Roberto Cortez <radcor...@yahoo.com>
AuthorDate: Fri Dec 21 16:58:06 2018 +0000

    TOMEE-2408 - Initial work to make MicroProfile deployment only when 
required.
---
 .../apache/openejb/server/rest/RESTService.java    |   7 +-
 tomee/tomee-microprofile/mp-common/pom.xml         | 177 ++++++++++++++++----
 .../cdi/TomEEMicroProfileExtension.java            | 179 +++++++++++++++++++++
 .../services/javax.enterprise.inject.spi.Extension |  17 ++
 .../tomee-microprofile-webapp/pom.xml              | 131 +--------------
 5 files changed, 345 insertions(+), 166 deletions(-)

diff --git 
a/server/openejb-rest/src/main/java/org/apache/openejb/server/rest/RESTService.java
 
b/server/openejb-rest/src/main/java/org/apache/openejb/server/rest/RESTService.java
index 9da7fd1..43d65b4 100644
--- 
a/server/openejb-rest/src/main/java/org/apache/openejb/server/rest/RESTService.java
+++ 
b/server/openejb-rest/src/main/java/org/apache/openejb/server/rest/RESTService.java
@@ -256,12 +256,7 @@ public abstract class RESTService implements 
ServerService, SelfManaging {
                     }
                 }
 
-                /*
-                boolean isMicroProfileOnlyEndpoints =
-                        webApp.restClass.stream().allMatch(name -> 
name.startsWith("org.apache.geronimo.microprofile"));
-                */
-
-                if (webApp.restApplications.isEmpty() /*&& 
!isMicroProfileOnlyEndpoints*/) {
+                if (webApp.restApplications.isEmpty()) {
                     final Application application = new 
InternalApplication(null);
                     for (final String clazz : webApp.restClass) {
                         try {
diff --git a/tomee/tomee-microprofile/mp-common/pom.xml 
b/tomee/tomee-microprofile/mp-common/pom.xml
index 263af46..da9f024 100644
--- a/tomee/tomee-microprofile/mp-common/pom.xml
+++ b/tomee/tomee-microprofile/mp-common/pom.xml
@@ -19,34 +19,149 @@
 <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";>
-    <parent>
-        <artifactId>tomee-microprofile</artifactId>
-        <groupId>org.apache.tomee</groupId>
-        <version>8.0.0-SNAPSHOT</version>
-    </parent>
-    <modelVersion>4.0.0</modelVersion>
-
-    <artifactId>mp-common</artifactId>
-    <name>OpenEJB :: TomEE :: MicroProfile Common</name>
-
-    <dependencies>
-        <dependency>
-            <groupId>${project.groupId}</groupId>
-            <artifactId>javaee-api</artifactId>
-            <scope>provided</scope>
-        </dependency>
-        <dependency>
-            <groupId>org.eclipse.microprofile.fault-tolerance</groupId>
-            <artifactId>microprofile-fault-tolerance-api</artifactId>
-            <version>${microprofile.fault-tolerance.version}</version>
-            <scope>provided</scope>
-        </dependency>
-
-        <dependency>
-            <groupId>org.apache.geronimo.safeguard</groupId>
-            <artifactId>safeguard-impl</artifactId>
-            <version>${microprofile.fault-tolerance.impl.version}</version>
-            <scope>provided</scope>
-        </dependency>
-    </dependencies>
-</project>
\ No newline at end of file
+  <parent>
+    <artifactId>tomee-microprofile</artifactId>
+    <groupId>org.apache.tomee</groupId>
+    <version>8.0.0-SNAPSHOT</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+
+  <artifactId>mp-common</artifactId>
+  <name>OpenEJB :: TomEE :: MicroProfile Common</name>
+
+  <dependencies>
+    <dependency>
+      <groupId>${project.groupId}</groupId>
+      <artifactId>javaee-api</artifactId>
+      <scope>provided</scope>
+    </dependency>
+
+    <!-- MicroProfile -->
+    <dependency>
+      <groupId>org.eclipse.microprofile.config</groupId>
+      <artifactId>microprofile-config-api</artifactId>
+      <version>${microprofile.config.version}</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.geronimo.config</groupId>
+      <artifactId>geronimo-config-impl</artifactId>
+      <version>${microprofile.config.impl.version}</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.eclipse.microprofile.jwt</groupId>
+      <artifactId>microprofile-jwt-auth-api</artifactId>
+      <version>${microprofile.jwt.version}</version>
+    </dependency>
+
+    <dependency>
+      <groupId>${project.groupId}</groupId>
+      <artifactId>mp-jwt</artifactId>
+      <version>${microprofile.jwt.impl.version}</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.eclipse.microprofile.fault-tolerance</groupId>
+      <artifactId>microprofile-fault-tolerance-api</artifactId>
+      <version>${microprofile.fault-tolerance.version}</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.geronimo.safeguard</groupId>
+      <artifactId>safeguard-impl</artifactId>
+      <version>${microprofile.fault-tolerance.impl.version}</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.eclipse.microprofile.health</groupId>
+      <artifactId>microprofile-health-api</artifactId>
+      <version>${microprofile.health.version}</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.geronimo</groupId>
+      <artifactId>geronimo-health</artifactId>
+      <version>${microprofile.health.impl.version}</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.eclipse.microprofile.metrics</groupId>
+      <artifactId>microprofile-metrics-api</artifactId>
+      <version>${microprofile.metrics.version}</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.geronimo</groupId>
+      <artifactId>geronimo-metrics</artifactId>
+      <version>${microprofile.metrics.impl.version}</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.eclipse.microprofile.rest.client</groupId>
+      <artifactId>microprofile-rest-client-api</artifactId>
+      <version>${microprofile.rest-client.version}</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.cxf</groupId>
+      <artifactId>cxf-rt-rs-mp-client</artifactId>
+      <version>${microprofile.rest-client.impl.version}</version>
+      <exclusions>
+        <exclusion>
+          <groupId>javax.json</groupId>
+          <artifactId>javax.json-api</artifactId>
+        </exclusion>
+      </exclusions>
+    </dependency>
+
+    <dependency>
+      <groupId>org.eclipse.microprofile.openapi</groupId>
+      <artifactId>microprofile-openapi-api</artifactId>
+      <version>${microprofile.openapi.version}</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.geronimo</groupId>
+      <artifactId>geronimo-openapi-impl</artifactId>
+      <version>${microprofile.openapi.impl.version}</version>
+    </dependency>
+
+    <!-- Jackson required by OpenAPI Impl -->
+    <dependency>
+      <groupId>com.fasterxml.jackson.dataformat</groupId>
+      <artifactId>jackson-dataformat-yaml</artifactId>
+      <version>${jackson.version}</version>
+    </dependency>
+
+    <dependency>
+      <groupId>com.fasterxml.jackson.core</groupId>
+      <artifactId>jackson-databind</artifactId>
+      <version>${jackson.version}</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.eclipse.microprofile.opentracing</groupId>
+      <artifactId>microprofile-opentracing-api</artifactId>
+      <version>${microprofile.opentracing.version}</version>
+      <exclusions>
+        <exclusion>
+          <groupId>org.apache.geronimo.specs</groupId>
+          <artifactId>geronimo-atinject_1.0_spec</artifactId>
+        </exclusion>
+      </exclusions>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.geronimo</groupId>
+      <artifactId>geronimo-opentracing</artifactId>
+      <version>${microprofile.opentracing.impl.version}</version>
+    </dependency>
+
+    <dependency>
+      <groupId>io.opentracing</groupId>
+      <artifactId>opentracing-api</artifactId>
+      <version>${opentracing.api}</version>
+    </dependency>
+  </dependencies>
+</project>
diff --git 
a/tomee/tomee-microprofile/mp-common/src/main/java/org/apache/tomee/microprofile/cdi/TomEEMicroProfileExtension.java
 
b/tomee/tomee-microprofile/mp-common/src/main/java/org/apache/tomee/microprofile/cdi/TomEEMicroProfileExtension.java
new file mode 100644
index 0000000..c3e094c
--- /dev/null
+++ 
b/tomee/tomee-microprofile/mp-common/src/main/java/org/apache/tomee/microprofile/cdi/TomEEMicroProfileExtension.java
@@ -0,0 +1,179 @@
+/*
+ * 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.tomee.microprofile.cdi;
+
+import org.apache.geronimo.microprofile.impl.health.jaxrs.HealthChecksEndpoint;
+import org.apache.geronimo.microprofile.metrics.jaxrs.MetricsEndpoints;
+import org.apache.geronimo.microprofile.openapi.jaxrs.OpenAPIEndpoint;
+import org.apache.openejb.assembler.classic.AppInfo;
+import org.apache.openejb.assembler.classic.WebAppInfo;
+import 
org.apache.openejb.assembler.classic.event.AssemblerAfterApplicationCreated;
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.observer.event.BeforeEvent;
+import org.eclipse.microprofile.metrics.annotation.Counted;
+import org.eclipse.microprofile.metrics.annotation.Gauge;
+import org.eclipse.microprofile.metrics.annotation.Metered;
+import org.eclipse.microprofile.metrics.annotation.Metric;
+import org.eclipse.microprofile.metrics.annotation.Timed;
+
+import javax.annotation.Priority;
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.spi.AfterBeanDiscovery;
+import javax.enterprise.inject.spi.Annotated;
+import javax.enterprise.inject.spi.AnnotatedType;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.BeforeBeanDiscovery;
+import javax.enterprise.inject.spi.CDI;
+import javax.enterprise.inject.spi.Extension;
+import javax.enterprise.inject.spi.Interceptor;
+import javax.enterprise.inject.spi.ProcessAnnotatedType;
+import javax.enterprise.inject.spi.ProcessInjectionPoint;
+import javax.enterprise.inject.spi.WithAnnotations;
+import javax.enterprise.util.AnnotationLiteral;
+
+import java.util.List;
+
+import static javax.enterprise.inject.spi.InterceptionType.AROUND_INVOKE;
+import static javax.interceptor.Interceptor.Priority.LIBRARY_BEFORE;
+
+public class TomEEMicroProfileExtension implements Extension {
+    private static final int BEFORE_MICROPROFILE_EXTENSIONS = LIBRARY_BEFORE - 
10;
+
+    private boolean requiresConfig;
+    private boolean requiresJwt;
+    private boolean requiresFaultTolerance;
+    private boolean requiresMetrics;
+    private boolean requiresHealth;
+    private boolean requiresOpenApi;
+    private boolean requiresOpenTracing;
+    private boolean requiresRestClient;
+
+    void beforeBeanDiscovery(@Observes
+                             @Priority(BEFORE_MICROPROFILE_EXTENSIONS) final 
BeforeBeanDiscovery beforeBeanDiscovery) {
+        this.requiresConfig = true;
+        this.requiresJwt = true;
+        this.requiresFaultTolerance = true;
+        // MP Metrics is not required unless specific annotations are found 
(or additional REST enpoints are deployed)
+        this.requiresMetrics = false;
+        this.requiresHealth = true;
+        this.requiresOpenApi = true;
+        this.requiresOpenTracing = true;
+        this.requiresRestClient = true;
+    }
+
+    void processMPAnnotatedTypes(@Observes
+                                 @Priority(BEFORE_MICROPROFILE_EXTENSIONS)
+                                 @WithAnnotations({
+                                         Metric.class,
+                                         Counted.class,
+                                         Gauge.class,
+                                         Metered.class,
+                                         Timed.class
+                                 }) final ProcessAnnotatedType<?> 
processAnnotatedType) {
+
+        final AnnotatedType<?> annotatedType = 
processAnnotatedType.getAnnotatedType();
+        if 
(annotatedType.getJavaClass().getName().startsWith("org.apache.geronimo.microprofile"))
 {
+            return;
+        }
+
+        hasMetricsAnnotations(annotatedType);
+    }
+
+    void processMPInjectionPoints(@Observes
+                                  @Priority(BEFORE_MICROPROFILE_EXTENSIONS)
+                                  final ProcessInjectionPoint<?, ?> 
processInjectionPoint) {
+        
hasMetricsAnnotations(processInjectionPoint.getInjectionPoint().getAnnotated());
+    }
+
+    void processMPBeans(@Observes
+                        @Priority(BEFORE_MICROPROFILE_EXTENSIONS)
+                        final AfterBeanDiscovery afterBeanDiscovery,
+                        final BeanManager beanManager) {
+        final List<Interceptor<?>> interceptors =
+                beanManager.resolveInterceptors(AROUND_INVOKE, new 
AnnotationLiteral<Counted>() {});
+        interceptors.isEmpty();
+    }
+
+    public boolean requiresConfig() {
+        return requiresConfig;
+    }
+
+    public boolean requiresJwt() {
+        return requiresJwt;
+    }
+
+    public boolean requiresFaultTolerance() {
+        return requiresFaultTolerance;
+    }
+
+    public boolean requiresMetrics() {
+        return requiresMetrics;
+    }
+
+    public boolean requiresHealth() {
+        return requiresHealth;
+    }
+
+    public boolean requiresOpenApi() {
+        return requiresOpenApi;
+    }
+
+    public boolean requiresOpenTrakcing() {
+        return requiresOpenTracing;
+    }
+
+    public boolean requiresRestClient() {
+        return requiresRestClient;
+    }
+
+    private void hasMetricsAnnotations(final Annotated annotated) {
+        if (requiresMetrics) {
+            return;
+        }
+
+        requiresMetrics = annotated.isAnnotationPresent(Metric.class) ||
+                          annotated.isAnnotationPresent(Counted.class) ||
+                          annotated.isAnnotationPresent(Gauge.class) ||
+                          annotated.isAnnotationPresent(Metered.class) ||
+                          annotated.isAnnotationPresent(Timed.class);
+    }
+
+    static {
+        SystemInstance.get().addObserver(new 
TomEEMicroProfileAfterApplicationCreated());
+    }
+
+    static class TomEEMicroProfileAfterApplicationCreated {
+        public void processApplication(
+                @org.apache.openejb.observer.Observes
+                final BeforeEvent<AssemblerAfterApplicationCreated> 
afterApplicationCreated) {
+
+            final TomEEMicroProfileExtension microProfileExtension =
+                    
CDI.current().getBeanManager().getExtension(TomEEMicroProfileExtension.class);
+            final AppInfo app = afterApplicationCreated.getEvent().getApp();
+            for (final WebAppInfo webApp : app.webApps) {
+                if (webApp.restApplications.isEmpty()) {
+                    if (!microProfileExtension.requiresMetrics) {
+                        webApp.restClass.removeIf(className -> 
className.equals(MetricsEndpoints.class.getName()));
+                    }
+
+                    webApp.restClass.removeIf(className -> 
className.equals(HealthChecksEndpoint.class.getName()));
+                    webApp.restClass.removeIf(className -> 
className.equals(OpenAPIEndpoint.class.getName()));
+                }
+            }
+        }
+    }
+}
diff --git 
a/tomee/tomee-microprofile/mp-common/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
 
b/tomee/tomee-microprofile/mp-common/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
new file mode 100644
index 0000000..8caea11
--- /dev/null
+++ 
b/tomee/tomee-microprofile/mp-common/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
@@ -0,0 +1,17 @@
+#
+# 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.
+#
+org.apache.tomee.microprofile.cdi.TomEEMicroProfileExtension
diff --git a/tomee/tomee-microprofile/tomee-microprofile-webapp/pom.xml 
b/tomee/tomee-microprofile/tomee-microprofile-webapp/pom.xml
index 7beb65d..a5495cc 100644
--- a/tomee/tomee-microprofile/tomee-microprofile-webapp/pom.xml
+++ b/tomee/tomee-microprofile/tomee-microprofile-webapp/pom.xml
@@ -37,14 +37,8 @@
   <properties>
     
<tomee.build.name>${project.groupId}.tomee.microprofile.webapp</tomee.build.name>
   </properties>
-  <dependencies>
-
-    <dependency>
-      <groupId>${project.groupId}</groupId>
-      <artifactId>mp-common</artifactId>
-      <version>${project.version}</version>
-    </dependency>
 
+  <dependencies>
     <dependency>
       <groupId>${project.groupId}</groupId>
       <artifactId>tomee-webapp</artifactId>
@@ -53,131 +47,10 @@
     </dependency>
 
     <dependency>
-      <groupId>org.eclipse.microprofile.config</groupId>
-      <artifactId>microprofile-config-api</artifactId>
-      <version>${microprofile.config.version}</version>
-    </dependency>
-
-    <dependency>
-      <groupId>org.apache.geronimo.config</groupId>
-      <artifactId>geronimo-config-impl</artifactId>
-      <version>${microprofile.config.impl.version}</version>
-    </dependency>
-
-    <dependency>
-      <groupId>org.eclipse.microprofile.jwt</groupId>
-      <artifactId>microprofile-jwt-auth-api</artifactId>
-      <version>${microprofile.jwt.version}</version>
-    </dependency>
-
-    <dependency>
       <groupId>${project.groupId}</groupId>
-      <artifactId>mp-jwt</artifactId>
+      <artifactId>mp-common</artifactId>
       <version>${project.version}</version>
     </dependency>
-
-    <dependency>
-      <groupId>org.eclipse.microprofile.fault-tolerance</groupId>
-      <artifactId>microprofile-fault-tolerance-api</artifactId>
-      <version>${microprofile.fault-tolerance.version}</version>
-    </dependency>
-
-    <dependency>
-      <groupId>org.apache.geronimo.safeguard</groupId>
-      <artifactId>safeguard-impl</artifactId>
-      <version>${microprofile.fault-tolerance.impl.version}</version>
-    </dependency>
-
-    <dependency>
-      <groupId>org.eclipse.microprofile.health</groupId>
-      <artifactId>microprofile-health-api</artifactId>
-      <version>${microprofile.health.version}</version>
-    </dependency>
-
-    <dependency>
-      <groupId>org.apache.geronimo</groupId>
-      <artifactId>geronimo-health</artifactId>
-      <version>${microprofile.health.impl.version}</version>
-    </dependency>
-
-    <dependency>
-      <groupId>org.eclipse.microprofile.metrics</groupId>
-      <artifactId>microprofile-metrics-api</artifactId>
-      <version>${microprofile.metrics.version}</version>
-    </dependency>
-
-    <dependency>
-      <groupId>org.apache.geronimo</groupId>
-      <artifactId>geronimo-metrics</artifactId>
-      <version>${microprofile.metrics.impl.version}</version>
-    </dependency>
-
-    <dependency>
-      <groupId>org.eclipse.microprofile.rest.client</groupId>
-      <artifactId>microprofile-rest-client-api</artifactId>
-      <version>${microprofile.rest-client.version}</version>
-    </dependency>
-
-    <dependency>
-      <groupId>org.apache.cxf</groupId>
-      <artifactId>cxf-rt-rs-mp-client</artifactId>
-      <version>${microprofile.rest-client.impl.version}</version>
-      <exclusions>
-        <exclusion>
-          <groupId>javax.json</groupId>
-          <artifactId>javax.json-api</artifactId>
-        </exclusion>
-      </exclusions>
-    </dependency>
-
-    <dependency>
-      <groupId>org.eclipse.microprofile.openapi</groupId>
-      <artifactId>microprofile-openapi-api</artifactId>
-      <version>${microprofile.openapi.version}</version>
-    </dependency>
-
-    <dependency>
-      <groupId>org.apache.geronimo</groupId>
-      <artifactId>geronimo-openapi-impl</artifactId>
-      <version>${microprofile.openapi.impl.version}</version>
-    </dependency>
-
-    <!-- Jackson required by OpenAPI Impl -->
-    <dependency>
-      <groupId>com.fasterxml.jackson.dataformat</groupId>
-      <artifactId>jackson-dataformat-yaml</artifactId>
-      <version>${jackson.version}</version>
-    </dependency>
-
-    <dependency>
-      <groupId>com.fasterxml.jackson.core</groupId>
-      <artifactId>jackson-databind</artifactId>
-      <version>${jackson.version}</version>
-    </dependency>
-
-    <dependency>
-      <groupId>org.eclipse.microprofile.opentracing</groupId>
-      <artifactId>microprofile-opentracing-api</artifactId>
-      <version>${microprofile.opentracing.version}</version>
-      <exclusions>
-        <exclusion>
-          <groupId>org.apache.geronimo.specs</groupId>
-          <artifactId>geronimo-atinject_1.0_spec</artifactId>
-        </exclusion>
-      </exclusions>
-    </dependency>
-
-    <dependency>
-      <groupId>org.apache.geronimo</groupId>
-      <artifactId>geronimo-opentracing</artifactId>
-      <version>${microprofile.opentracing.impl.version}</version>
-    </dependency>
-
-    <dependency>
-      <groupId>io.opentracing</groupId>
-      <artifactId>opentracing-api</artifactId>
-      <version>${opentracing.api}</version>
-    </dependency>
   </dependencies>
 
   <build>

Reply via email to