2 * ========================LICENSE_START=================================
5 * Copyright (C) 2024: OpenInfra Foundation Europe
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 com.demo.producer.repository;
23 import java.util.HashMap;
25 import java.util.Vector;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.stereotype.Component;
31 import com.demo.producer.messages.PropertiesHelper;
32 import com.demo.producer.repository.Job.Parameters;
33 import com.google.gson.Gson;
34 import com.google.gson.GsonBuilder;
38 private static final Logger logger = LoggerFactory.getLogger(Jobs.class);
40 private Map<String, Job> allJobs = new HashMap<>();
45 public synchronized Job getJob(String id) throws Exception {
46 Job job = allJobs.get(id);
48 throw new Exception("Could not find job: " + id);
53 public synchronized Job get(String id) {
54 return allJobs.get(id);
57 public void addJob(String id, InfoType type, String owner, Parameters parameters) {
58 Job job = new Job(id, type, owner, parameters);
59 setKafkaServersEnvironment(job);
63 private void setKafkaServersEnvironment(Job job) {
64 String kafkaServers = job.getParameters().getDeliveryInfo().getBootStrapServers();
65 if (kafkaServers != null && !kafkaServers.isEmpty()) {
66 PropertiesHelper.setKafkaServers(kafkaServers);
67 logger.info("Setting variable bootStrapServers: {}", kafkaServers);
69 logger.warn("bootStrapServers is not set for job: {}", job.getId());
73 private synchronized void put(Job job) {
74 logger.debug("Put job: {}", job.getId());
75 allJobs.put(job.getId(), job);
78 public synchronized Iterable<Job> getAll() {
79 return new Vector<>(allJobs.values());
82 public synchronized int size() {
83 return allJobs.size();
86 public synchronized Job delete(String id) {
87 return allJobs.remove(id);
91 public String toString() {
92 Gson gson = new GsonBuilder().create();
93 return gson.toJson(allJobs);