ApplicationRegistry.java 5.1 KB
Newer Older
Johannes Stelzer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright 2014 the original author or authors.
 *
 * 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 de.codecentric.boot.admin.registry;

import java.net.MalformedURLException;
import java.net.URL;
20
import java.util.Collection;
Johannes Stelzer committed
21 22 23

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
24 25
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
26 27
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
Johannes Stelzer committed
28

29
import de.codecentric.boot.admin.event.ClientApplicationDeregisteredEvent;
30
import de.codecentric.boot.admin.event.ClientApplicationRegisteredEvent;
Johannes Stelzer committed
31
import de.codecentric.boot.admin.model.Application;
32
import de.codecentric.boot.admin.model.StatusInfo;
Johannes Stelzer committed
33 34 35
import de.codecentric.boot.admin.registry.store.ApplicationStore;

/**
36 37 38
 * Registry for all applications that should be managed/administrated by the Spring Boot Admin
 * application. Backed by an ApplicationStore for persistence and an ApplicationIdGenerator for id
 * generation.
Johannes Stelzer committed
39
 */
40
public class ApplicationRegistry implements ApplicationEventPublisherAware {
Johannes Stelzer committed
41 42 43 44
	private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationRegistry.class);

	private final ApplicationStore store;
	private final ApplicationIdGenerator generator;
45
	private ApplicationEventPublisher publisher;
Johannes Stelzer committed
46

47
	public ApplicationRegistry(ApplicationStore store, ApplicationIdGenerator generator) {
Johannes Stelzer committed
48 49 50 51 52 53
		this.store = store;
		this.generator = generator;
	}

	/**
	 * Register application.
54
	 *
55
	 * @param application application to be registered.
56
	 * @return the registered application.
Johannes Stelzer committed
57
	 */
58 59 60 61 62
	public Application register(Application application) {
		Assert.notNull(application, "Application must not be null");
		Assert.hasText(application.getName(), "Name must not be null");
		Assert.hasText(application.getHealthUrl(), "Health-URL must not be null");
		Assert.isTrue(checkUrl(application.getHealthUrl()), "Health-URL is not valid");
63
		Assert.isTrue(
64
				StringUtils.isEmpty(application.getManagementUrl())
65
						|| checkUrl(application.getManagementUrl()), "URL is not valid");
66
		Assert.isTrue(
67 68
				StringUtils.isEmpty(application.getServiceUrl())
						|| checkUrl(application.getServiceUrl()), "URL is not valid");
Johannes Stelzer committed
69

70 71
		String applicationId = generator.generateId(application);
		Assert.notNull(applicationId, "ID must not be null");
Johannes Stelzer committed
72

73
		StatusInfo existingStatusInfo = getExistingStatusInfo(applicationId);
Johannes Stelzer committed
74

75 76
		Application registering = Application.create(application).withId(applicationId)
				.withStatusInfo(existingStatusInfo).build();
77 78 79 80 81

		Application replaced = store.save(registering);

		if (replaced == null) {
			LOGGER.info("New Application {} registered ", registering);
82
			publisher.publishEvent(new ClientApplicationRegisteredEvent(registering));
Johannes Stelzer committed
83
		} else {
84 85
			if (registering.getId().equals(replaced.getId())) {
				LOGGER.debug("Application {} refreshed", registering);
Johannes Stelzer committed
86
			} else {
87
				LOGGER.warn("Application {} replaced by Application {}", registering, replaced);
Johannes Stelzer committed
88 89
			}
		}
90 91 92 93 94
		return registering;
	}

	private StatusInfo getExistingStatusInfo(String applicationId) {
		Application existing = getApplication(applicationId);
95
		if (existing != null) {
96 97
			return existing.getStatusInfo();
		}
Johannes Edmeier committed
98
		return StatusInfo.ofUnknown();
Johannes Stelzer committed
99 100 101 102
	}

	/**
	 * Checks the syntax of the given URL.
103
	 *
Johannes Stelzer committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117
	 * @param url The URL.
	 * @return true, if valid.
	 */
	private boolean checkUrl(String url) {
		try {
			new URL(url);
		} catch (MalformedURLException e) {
			return false;
		}
		return true;
	}

	/**
	 * Get a list of all registered applications.
118
	 *
119
	 * @return List of all applications.
Johannes Stelzer committed
120
	 */
121 122 123 124 125 126
	public Collection<Application> getApplications() {
		return store.findAll();
	}

	/**
	 * Get a list of all registered applications.
127
	 *
128 129
	 * @param name the name to search for.
	 * @return List of applications with the given name.
130 131 132
	 */
	public Collection<Application> getApplicationsByName(String name) {
		return store.findByName(name);
Johannes Stelzer committed
133 134 135 136
	}

	/**
	 * Get a specific application inside the registry.
137
	 *
Johannes Stelzer committed
138 139 140 141
	 * @param id Id.
	 * @return Application.
	 */
	public Application getApplication(String id) {
142
		return store.find(id);
Johannes Stelzer committed
143 144 145 146
	}

	/**
	 * Remove a specific application from registry
147
	 *
148
	 * @param id the applications id to unregister
Johannes Stelzer committed
149 150
	 * @return the unregistered Application
	 */
151
	public Application deregister(String id) {
152
		Application app = store.delete(id);
153 154
		if (app != null) {
			LOGGER.info("Application {} unregistered ", app);
155
			publisher.publishEvent(new ClientApplicationDeregisteredEvent(app));
156
		}
Johannes Stelzer committed
157 158
		return app;
	}
159 160

	@Override
161
	public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
162
		publisher = applicationEventPublisher;
163
	}
Johannes Stelzer committed
164
}