You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by cs...@apache.org on 2019/04/04 15:16:54 UTC

[karaf] branch master updated: Use streams in ScrBundleStateService

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

cschneider pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/karaf.git


The following commit(s) were added to refs/heads/master by this push:
     new e7201d4  Use streams in ScrBundleStateService
e7201d4 is described below

commit e7201d42fc4e9a13b32c9f42dda5ae40a9648be5
Author: Christian Schneider <cs...@adobe.com>
AuthorDate: Thu Apr 4 17:16:40 2019 +0200

    Use streams in ScrBundleStateService
---
 scr/state/pom.xml                                  | 13 ++--
 .../java/org/apache/karaf/scr/state/Activator.java | 38 ---------
 .../karaf/scr/state/ScrBundleStateService.java     | 91 ++++++++++------------
 3 files changed, 45 insertions(+), 97 deletions(-)

diff --git a/scr/state/pom.xml b/scr/state/pom.xml
index 36fe268..5c18452 100644
--- a/scr/state/pom.xml
+++ b/scr/state/pom.xml
@@ -60,6 +60,11 @@
             <artifactId>org.apache.karaf.bundle.core</artifactId>
         </dependency>
         <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.service.component.annotations</artifactId>
+            <version>1.3.0</version>
+        </dependency>
+        <dependency>
             <groupId>org.apache.felix</groupId>
             <artifactId>org.apache.felix.scr</artifactId>
         </dependency>
@@ -83,16 +88,8 @@
         </resources>
         <plugins>
             <plugin>
-                <groupId>org.apache.karaf.tooling</groupId>
-                <artifactId>karaf-services-maven-plugin</artifactId>
-            </plugin>
-            <plugin>
                 <groupId>org.apache.felix</groupId>
                 <artifactId>maven-bundle-plugin</artifactId>
-                <configuration>
-                    <instructions>
-                    </instructions>
-                </configuration>
             </plugin>
         </plugins>
     </build>
diff --git a/scr/state/src/main/java/org/apache/karaf/scr/state/Activator.java b/scr/state/src/main/java/org/apache/karaf/scr/state/Activator.java
deleted file mode 100644
index a5c9d35..0000000
--- a/scr/state/src/main/java/org/apache/karaf/scr/state/Activator.java
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * 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.karaf.scr.state;
-
-import org.apache.karaf.bundle.core.BundleStateService;
-import org.apache.karaf.util.tracker.BaseActivator;
-import org.apache.karaf.util.tracker.annotation.ProvideService;
-import org.apache.karaf.util.tracker.annotation.RequireService;
-import org.apache.karaf.util.tracker.annotation.Services;
-import org.osgi.service.component.runtime.ServiceComponentRuntime;
-
-@Services(requires = @RequireService(ServiceComponentRuntime.class),
-          provides = @ProvideService(BundleStateService.class))
-public class Activator extends BaseActivator {
-
-    @Override
-    protected void doStart() throws Exception {
-        ServiceComponentRuntime scr = getTrackedService(ServiceComponentRuntime.class);
-        if (scr != null) {
-            register(BundleStateService.class, new ScrBundleStateService(scr));
-        }
-    }
-
-}
diff --git a/scr/state/src/main/java/org/apache/karaf/scr/state/ScrBundleStateService.java b/scr/state/src/main/java/org/apache/karaf/scr/state/ScrBundleStateService.java
index 03485a4..8d4b1b7 100644
--- a/scr/state/src/main/java/org/apache/karaf/scr/state/ScrBundleStateService.java
+++ b/scr/state/src/main/java/org/apache/karaf/scr/state/ScrBundleStateService.java
@@ -16,21 +16,25 @@
  */
 package org.apache.karaf.scr.state;
 
+import static java.lang.String.join;
+import static java.util.Arrays.asList;
+import static java.util.stream.Collectors.joining;
+
+import java.util.stream.Stream;
+
 import org.apache.karaf.bundle.core.BundleState;
 import org.apache.karaf.bundle.core.BundleStateService;
 import org.osgi.framework.Bundle;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
 import org.osgi.service.component.runtime.ServiceComponentRuntime;
 import org.osgi.service.component.runtime.dto.ComponentConfigurationDTO;
-import org.osgi.service.component.runtime.dto.ComponentDescriptionDTO;
-import org.osgi.service.component.runtime.dto.UnsatisfiedReferenceDTO;
 
+@Component
 public class ScrBundleStateService implements BundleStateService {
 
-    ServiceComponentRuntime scr;
-
-    public ScrBundleStateService(ServiceComponentRuntime scr) {
-        this.scr = scr;
-    }
+    @Reference
+    private ServiceComponentRuntime scr;
 
     @Override
     public String getName() {
@@ -39,54 +43,39 @@ public class ScrBundleStateService implements BundleStateService {
 
     @Override
     public String getDiag(Bundle bundle) {
-        StringBuilder sb = new StringBuilder();
-        for (ComponentDescriptionDTO desc : scr.getComponentDescriptionDTOs(bundle)) {
-            for (ComponentConfigurationDTO cfg : scr.getComponentConfigurationDTOs(desc)) {
-                if (cfg.state != ComponentConfigurationDTO.ACTIVE
-                        && cfg.state != ComponentConfigurationDTO.SATISFIED) {
-                    sb.append(cfg.description.name).append(" (").append(cfg.id).append(")\n");
-                    if ((cfg.state & ComponentConfigurationDTO.UNSATISFIED_CONFIGURATION) != 0) {
-                        sb.append("  missing configurations: ");
-                        boolean first = true;
-                        for (String s : cfg.description.configurationPid) {
-                            if (!first) {
-                                sb.append(", ");
-                            }
-                            sb.append(s);
-                            first = false;
-                        }
-                        sb.append("\n");
-                    }
-                    if ((cfg.state & ComponentConfigurationDTO.UNSATISFIED_REFERENCE) != 0) {
-                        sb.append("  missing references: ");
-                        boolean first = true;
-                        for (UnsatisfiedReferenceDTO ur : cfg.unsatisfiedReferences) {
-                            if (!first) {
-                                sb.append(", ");
-                            }
-                            sb.append(ur.name);
-                            first = false;
-                        }
-                        sb.append("\n");
-                    }
-                }
-            }
-        }
-        return sb.toString();
+        return allCompConfigs(bundle).map(this::diagComponent).collect(joining());
     }
 
     @Override
     public BundleState getState(Bundle bundle) {
-        if (bundle.getState() == Bundle.ACTIVE) {
-            for (ComponentDescriptionDTO desc : scr.getComponentDescriptionDTOs(bundle)) {
-                for (ComponentConfigurationDTO cfg : scr.getComponentConfigurationDTOs(desc)) {
-                    if (cfg.state != ComponentConfigurationDTO.ACTIVE
-                            && cfg.state != ComponentConfigurationDTO.SATISFIED) {
-                        return BundleState.Waiting;
-                    }
-                }
-            }
+        boolean waiting = bundle.getState() == Bundle.ACTIVE && allCompConfigs(bundle).anyMatch(this::unsatisfied);
+        return waiting ? BundleState.Waiting : BundleState.Unknown;
+    }
+
+    private String diagComponent(ComponentConfigurationDTO cfg) {
+        StringBuilder sb = new StringBuilder();
+        sb.append(cfg.description.name).append(" (").append(cfg.id).append(")\n");
+        if ((cfg.state & ComponentConfigurationDTO.UNSATISFIED_CONFIGURATION) != 0) {
+            sb.append("  missing configurations: ");
+            sb.append(join(", ", cfg.description.configurationPid));
+            sb.append("\n");
         }
-        return BundleState.Unknown;
+        if ((cfg.state & ComponentConfigurationDTO.UNSATISFIED_REFERENCE) != 0) {
+            sb.append("  missing references: ");
+            String references = asList(cfg.unsatisfiedReferences).stream().map(ref -> ref.name).collect(joining(", "));
+            sb.append(references);
+            sb.append("\n");
+        }
+        return sb.toString();
+    }
+
+    private Stream<ComponentConfigurationDTO> allCompConfigs(Bundle bundle) {
+        return scr.getComponentDescriptionDTOs(bundle).stream()
+            .flatMap(desc -> scr.getComponentConfigurationDTOs(desc).stream());
+    }
+
+    private boolean unsatisfied(ComponentConfigurationDTO cfg) {
+        return cfg.state != ComponentConfigurationDTO.ACTIVE
+                && cfg.state != ComponentConfigurationDTO.SATISFIED;
     }
 }