You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by jk...@apache.org on 2021/03/26 10:13:04 UTC

[unomi] branch useBusServiceRestServer created (now 92e4964)

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

jkevan pushed a change to branch useBusServiceRestServer
in repository https://gitbox.apache.org/repos/asf/unomi.git.


      at 92e4964  UNOMI-451: implement REST server bus as OSGI Service to correctly reload server when it's necessary and avoid duplicated features in the bus

This branch includes the following new commits:

     new 92e4964  UNOMI-451: implement REST server bus as OSGI Service to correctly reload server when it's necessary and avoid duplicated features in the bus

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[unomi] 01/01: UNOMI-451: implement REST server bus as OSGI Service to correctly reload server when it's necessary and avoid duplicated features in the bus

Posted by jk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jkevan pushed a commit to branch useBusServiceRestServer
in repository https://gitbox.apache.org/repos/asf/unomi.git

commit 92e4964c2471ad580ff846f96464fbdc98e43d16
Author: Kevan <ke...@jahia.com>
AuthorDate: Fri Mar 26 11:12:45 2021 +0100

    UNOMI-451: implement REST server bus as OSGI Service to correctly reload server when it's necessary and avoid duplicated features in the bus
---
 .../java/org/apache/unomi/rest/RestServer.java     | 25 ++++++++--------
 .../java/org/apache/unomi/rest/RestServerBus.java  | 33 ++++++++++++++++++++++
 2 files changed, 47 insertions(+), 11 deletions(-)

diff --git a/rest/src/main/java/org/apache/unomi/rest/RestServer.java b/rest/src/main/java/org/apache/unomi/rest/RestServer.java
index 9004aae..a2de0d3 100644
--- a/rest/src/main/java/org/apache/unomi/rest/RestServer.java
+++ b/rest/src/main/java/org/apache/unomi/rest/RestServer.java
@@ -29,16 +29,14 @@ import org.osgi.framework.BundleContext;
 import org.osgi.framework.Filter;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.component.ComponentContext;
-import org.osgi.service.component.annotations.Activate;
-import org.osgi.service.component.annotations.Component;
-import org.osgi.service.component.annotations.Deactivate;
-import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.*;
 import org.osgi.util.tracker.ServiceTracker;
 import org.osgi.util.tracker.ServiceTrackerCustomizer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import javax.ws.rs.ext.ExceptionMapper;
+import javax.xml.namespace.QName;
 import java.util.*;
 import java.util.concurrent.CopyOnWriteArrayList;
 
@@ -50,6 +48,7 @@ public class RestServer {
     private Server server;
     private BundleContext bundleContext;
     private ServiceTracker jaxRSServiceTracker;
+    private Bus serverBus;
     private List<ExceptionMapper> exceptionMappers = new ArrayList<>();
     private long timeOfLastUpdate = System.currentTimeMillis();
     private Timer refreshTimer = null;
@@ -57,6 +56,13 @@ public class RestServer {
 
     final List<Object> serviceBeans = new CopyOnWriteArrayList<>();
 
+    private static final QName UNOMI_REST_SERVER_END_POINT_NAME = new QName("http://rest.unomi.apache.org/", "UnomiRestServerEndPoint");
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    public void setServerBus(Bus serverBus) {
+        this.serverBus = serverBus;
+    }
+
     @Reference
     public void addExceptionMapper(ExceptionMapper exceptionMapper) {
         this.exceptionMappers.add(exceptionMapper);
@@ -151,11 +157,7 @@ public class RestServer {
 
         JAXRSServerFactoryBean jaxrsServerFactoryBean = new JAXRSServerFactoryBean();
         jaxrsServerFactoryBean.setAddress("/");
-        Bus bus = BusFactory.getDefaultBus();
-        // bus.getFeatures().add(new LoggingFeature());
-        bus.getFeatures().add(new LoggingFeature());
-        bus.getFeatures().add(new org.apache.cxf.metrics.MetricsFeature());
-        jaxrsServerFactoryBean.setBus(bus);
+        jaxrsServerFactoryBean.setBus(serverBus);
         jaxrsServerFactoryBean.setProvider(
                 new JacksonJaxbJsonProvider(
                         new org.apache.unomi.persistence.spi.CustomObjectMapper(),
@@ -171,10 +173,11 @@ public class RestServer {
         }
         jaxrsServerFactoryBean.setServiceBeans(serviceBeans);
         jaxrsServerFactoryBean.getFeatures().add(openApiFeature);
+
         if (serviceBeans.size() > 0) {
             logger.info("Starting JAX RS Endpoint...");
             server = jaxrsServerFactoryBean.create();
+            server.getEndpoint().getEndpointInfo().setName(UNOMI_REST_SERVER_END_POINT_NAME);
         }
     }
-
-}
+}
\ No newline at end of file
diff --git a/rest/src/main/java/org/apache/unomi/rest/RestServerBus.java b/rest/src/main/java/org/apache/unomi/rest/RestServerBus.java
new file mode 100644
index 0000000..6ceb6ed
--- /dev/null
+++ b/rest/src/main/java/org/apache/unomi/rest/RestServerBus.java
@@ -0,0 +1,33 @@
+/*
+ * 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.unomi.rest;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.bus.extension.ExtensionManagerBus;
+import org.apache.cxf.feature.LoggingFeature;
+import org.osgi.service.component.annotations.Component;
+
+/**
+ * Rest server bus
+ */
+@Component(service = Bus.class)
+public class RestServerBus extends ExtensionManagerBus implements Bus {
+    public RestServerBus() {
+        this.getFeatures().add(new LoggingFeature());
+        this.getFeatures().add(new org.apache.cxf.metrics.MetricsFeature());
+    }
+}