2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controlloop.actorserviceprovider.impl;
24 import org.onap.policy.common.capabilities.Configurable;
25 import org.onap.policy.common.capabilities.Startable;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Partial implementation of an object that is both startable and configurable. It
31 * provides the high level methods defined in the interface, while deferring the details
32 * to abstract methods that must be provided by the subclasses. It also manages the
33 * current {@link #state}.
35 * @param <T> type of parameters expected by {@link #configure(Object)}
37 public abstract class StartConfigPartial<T> implements Startable, Configurable<T> {
38 private static final Logger logger = LoggerFactory.getLogger(StartConfigPartial.class);
41 private final String fullName;
44 IDLE, CONFIGURED, ALIVE
47 private State state = State.IDLE;
50 * Constructs the object.
52 * @param fullName full name of this object, used for logging and exception purposes
54 protected StartConfigPartial(String fullName) {
55 this.fullName = fullName;
59 public synchronized boolean isAlive() {
60 return (state == State.ALIVE);
64 * Determines if this object has been configured.
66 * @return {@code true} if this object has been configured, {@code false} otherwise
68 public synchronized boolean isConfigured() {
69 return (state != State.IDLE);
73 public synchronized void configure(T parameters) {
75 throw new IllegalStateException("attempt to reconfigure, but already running " + getFullName());
78 logger.info("initializing {}", getFullName());
80 doConfigure(parameters);
82 state = State.CONFIGURED;
86 public synchronized boolean start() {
89 logger.info("{} is already running", getFullName());
93 logger.info("starting {}", getFullName());
100 throw new IllegalStateException("attempt to start unconfigured " + getFullName());
107 public synchronized boolean stop() {
109 logger.info("stopping {}", getFullName());
110 state = State.CONFIGURED;
114 logger.info("{} is not running", getFullName());
121 public synchronized void shutdown() {
123 logger.info("{} is not running", getFullName());
127 logger.info("shutting down actor {}", getFullName());
128 state = State.CONFIGURED;
133 * Configures this object.
135 * @param parameters configuration parameters
137 protected abstract void doConfigure(T parameters);
140 * Starts this object.
142 protected abstract void doStart();
147 protected abstract void doStop();
150 * Shuts down this object.
152 protected abstract void doShutdown();