aboutsummaryrefslogtreecommitdiffstats
path: root/utils/src/main/java/org/onap/policy/common/utils/services
diff options
context:
space:
mode:
Diffstat (limited to 'utils/src/main/java/org/onap/policy/common/utils/services')
-rw-r--r--utils/src/main/java/org/onap/policy/common/utils/services/FeatureApiUtils.java13
-rw-r--r--utils/src/main/java/org/onap/policy/common/utils/services/OrderedServiceImpl.java19
-rw-r--r--utils/src/main/java/org/onap/policy/common/utils/services/Registry.java12
-rw-r--r--utils/src/main/java/org/onap/policy/common/utils/services/ServiceManager.java43
4 files changed, 41 insertions, 46 deletions
diff --git a/utils/src/main/java/org/onap/policy/common/utils/services/FeatureApiUtils.java b/utils/src/main/java/org/onap/policy/common/utils/services/FeatureApiUtils.java
index e88361ea..042ee937 100644
--- a/utils/src/main/java/org/onap/policy/common/utils/services/FeatureApiUtils.java
+++ b/utils/src/main/java/org/onap/policy/common/utils/services/FeatureApiUtils.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,15 +23,14 @@ package org.onap.policy.common.utils.services;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
/**
* Utilities for use with "feature APIs".
*/
-public class FeatureApiUtils {
-
- private FeatureApiUtils() {
- // do nothing
- }
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class FeatureApiUtils {
/**
* Applies a function on each feature provider, stopping as soon as one returns true.
@@ -44,7 +43,7 @@ public class FeatureApiUtils {
* otherwise
*/
public static <T> boolean apply(List<T> providers, Predicate<T> predicate,
- BiConsumer<T,Exception> handleEx) {
+ BiConsumer<T, Exception> handleEx) {
for (T feature : providers) {
try {
diff --git a/utils/src/main/java/org/onap/policy/common/utils/services/OrderedServiceImpl.java b/utils/src/main/java/org/onap/policy/common/utils/services/OrderedServiceImpl.java
index bbd30220..998d6742 100644
--- a/utils/src/main/java/org/onap/policy/common/utils/services/OrderedServiceImpl.java
+++ b/utils/src/main/java/org/onap/policy/common/utils/services/OrderedServiceImpl.java
@@ -2,14 +2,14 @@
* ============LICENSE_START=======================================================
* utils
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed 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.
@@ -24,18 +24,18 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
+import java.util.Map;
import java.util.ServiceLoader;
-
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class is a template for building a sorted list of service instances,
- * which are discovered and created using 'ServiceLoader'.
+ * which are discovered and created using 'ServiceLoader'.
*/
public class OrderedServiceImpl<T extends OrderedService> {
// logger
- private static Logger logger = LoggerFactory.getLogger(OrderedServiceImpl.class);
+ private static Logger logger = LoggerFactory.getLogger(OrderedServiceImpl.class);
// sorted list of instances implementing the service
private List<T> implementers = null;
@@ -44,8 +44,7 @@ public class OrderedServiceImpl<T extends OrderedService> {
private ServiceLoader<T> serviceLoader = null;
// use this to ensure that we only use one unique instance of each class
- @SuppressWarnings("rawtypes")
- private static HashMap<Class,OrderedService> classToSingleton = new HashMap<>();
+ private static Map<Class<?>, OrderedService> classToSingleton = new HashMap<>();
/**
* Constructor - create the 'ServiceLoader' instance.
@@ -60,7 +59,7 @@ public class OrderedServiceImpl<T extends OrderedService> {
/**
* Get List of implementers.
- *
+ *
* @return the sorted list of services implementing interface 'T' discovered
* by 'ServiceLoader'.
*/
@@ -87,7 +86,7 @@ public class OrderedServiceImpl<T extends OrderedService> {
// build a list of all of the current implementors
List<T> tmp = new LinkedList<>();
for (T service : serviceLoader) {
- tmp.add((T)getSingleton(service));
+ tmp.add((T) getSingleton(service));
}
// Sort the list according to sequence number, and then alphabetically
diff --git a/utils/src/main/java/org/onap/policy/common/utils/services/Registry.java b/utils/src/main/java/org/onap/policy/common/utils/services/Registry.java
index c3eabe8e..8765ba8b 100644
--- a/utils/src/main/java/org/onap/policy/common/utils/services/Registry.java
+++ b/utils/src/main/java/org/onap/policy/common/utils/services/Registry.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +22,8 @@ package org.onap.policy.common.utils.services;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -29,6 +31,7 @@ import org.slf4j.LoggerFactory;
* This is a simple object registry, similar in spirit to JNDI, but suitable for use in a
* stand-alone JVM.
*/
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Registry {
private static final Logger logger = LoggerFactory.getLogger(Registry.class);
@@ -40,13 +43,6 @@ public class Registry {
private Map<String, Object> name2object = new ConcurrentHashMap<>();
/**
- * Constructs the object.
- */
- private Registry() {
- super();
- }
-
- /**
* Registers an object.
*
* @param name name by which the object is known
diff --git a/utils/src/main/java/org/onap/policy/common/utils/services/ServiceManager.java b/utils/src/main/java/org/onap/policy/common/utils/services/ServiceManager.java
index 5c8c01df..08994912 100644
--- a/utils/src/main/java/org/onap/policy/common/utils/services/ServiceManager.java
+++ b/utils/src/main/java/org/onap/policy/common/utils/services/ServiceManager.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP PAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,6 +23,9 @@ package org.onap.policy.common.utils.services;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
+import java.util.concurrent.atomic.AtomicBoolean;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
import org.onap.policy.common.capabilities.Startable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -37,6 +40,7 @@ public class ServiceManager implements Startable {
/**
* Manager name.
*/
+ @Getter
private final String name;
/**
@@ -47,7 +51,7 @@ public class ServiceManager implements Startable {
/**
* {@code True} if the services are currently running, {@code false} otherwise.
*/
- private boolean running;
+ private final AtomicBoolean running = new AtomicBoolean(false);
/**
* Constructs the object, with a default name.
@@ -58,16 +62,13 @@ public class ServiceManager implements Startable {
/**
* Constructs the object.
+ *
* @param name the manager's name, used for logging purposes
*/
public ServiceManager(String name) {
this.name = name;
}
- public String getName() {
- return name;
- }
-
/**
* Adds a pair of service actions to the manager.
*
@@ -77,7 +78,7 @@ public class ServiceManager implements Startable {
* @return this manager
*/
public synchronized ServiceManager addAction(String stepName, RunnableWithEx starter, RunnableWithEx stopper) {
- if (running) {
+ if (isAlive()) {
throw new IllegalStateException(name + " is already running; cannot add " + stepName);
}
@@ -94,7 +95,7 @@ public class ServiceManager implements Startable {
* @return this manager
*/
public synchronized ServiceManager addService(String stepName, Startable service) {
- if (running) {
+ if (isAlive()) {
throw new IllegalStateException(name + " is already running; cannot add " + stepName);
}
@@ -103,13 +104,13 @@ public class ServiceManager implements Startable {
}
@Override
- public synchronized boolean isAlive() {
- return running;
+ public boolean isAlive() {
+ return running.get();
}
@Override
public synchronized boolean start() {
- if (running) {
+ if (isAlive()) {
throw new IllegalStateException(name + " is already running");
}
@@ -134,7 +135,7 @@ public class ServiceManager implements Startable {
if (ex == null) {
logger.info("{} started", name);
- running = true;
+ running.set(true);
return true;
}
@@ -151,11 +152,11 @@ public class ServiceManager implements Startable {
@Override
public synchronized boolean stop() {
- if (!running) {
+ if (!isAlive()) {
throw new IllegalStateException(name + " is not running");
}
- running = false;
+ running.set(false);
rewind(items);
return true;
@@ -203,20 +204,20 @@ public class ServiceManager implements Startable {
/**
* Service information.
*/
+ @AllArgsConstructor
private static class Service {
private String stepName;
private RunnableWithEx starter;
private RunnableWithEx stopper;
-
- public Service(String stepName, RunnableWithEx starter, RunnableWithEx stopper) {
- this.stepName = stepName;
- this.starter = starter;
- this.stopper = stopper;
- }
}
+ /*
+ * Cannot use a plain Runnable, because it can't throw exceptions. Could use a
+ * Callable, instead, but then all the lambda expressions become rather messy, thus
+ * we'll stick with RunnableWithEx, and just disable the sonar warning.
+ */
@FunctionalInterface
public static interface RunnableWithEx {
- void run() throws Exception;
+ void run() throws Exception; // NOSONAR
}
}