TransientMessageServiceImpl.java

/*
 * Copyright 2021 Global Crop Diversity Trust
 *
 * 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.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gringlobal.service.impl;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.UUID;
import java.util.stream.Collectors;

import org.gringlobal.api.exception.NotFoundElement;
import org.gringlobal.service.TransientMessageService;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

@Service
@Validated
public class TransientMessageServiceImpl implements TransientMessageService, InitializingBean {

	private final Queue<TransientMessage> alerts = new LinkedList<>();
	
	@Value("${messaging.admin.maxMessages:100}")
	private int maxQueueSize;

	@Override
	public void afterPropertiesSet() throws Exception {
		addAdminAlert("SERVER-STARTED", "API Server started.");
	}

	@Override
	public TransientMessage addAdminAlert(String key, String message, Object... args) {
		var ft = MessageFormatter.arrayFormat(message, args);
		TransientMessage newMessage;
		synchronized (alerts) {
			var existing = alerts.stream().filter((msg) -> key.equals(msg.key)).findFirst().orElse(null);
			if (existing != null) {
				existing.count ++;
				return existing;
			}

			newMessage = new TransientMessage(key, ft.getMessage(), new Date());
			if (alerts.size() >= maxQueueSize) {
				alerts.poll();
			}
			alerts.add(newMessage);
		}
		return newMessage;
	}

	@Override
	public Page<TransientMessage> listAdminAlerts(Pageable page) {
		List<TransientMessage> content = alerts.stream().skip(page.getOffset()).limit(page.getPageSize()).collect(Collectors.toList());
		return new PageImpl<>(content, page, alerts.size());
	}

	@Override
	public TransientMessage removeAdminAlert(UUID alertUuid) {
		var message = alerts.stream().filter((msg) -> alertUuid.equals(msg.uuid)).findFirst().orElseThrow(() -> new NotFoundElement("Not found alert by UUID=" + alertUuid));
		if (alerts.remove(message)) {
			return message;
		} else {
			return null;
		}
	}

}