ApplicationRegistry.java 4.98 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 32 33 34
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.registry.store.ApplicationStore;

/**
35 36 37
 * 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
38
 */
39
public class ApplicationRegistry implements ApplicationEventPublisherAware {
Johannes Stelzer committed
40 41 42 43
	private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationRegistry.class);

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

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

	/**
	 * Register application.
53
	 *
54
	 * @param application application to be registered.
55
	 * @return the registered application.
Johannes Stelzer committed
56
	 */
57 58 59 60 61
	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");
62
		Assert.isTrue(
63
				StringUtils.isEmpty(application.getManagementUrl())
64
						|| checkUrl(application.getManagementUrl()), "URL is not valid");
65
		Assert.isTrue(
66 67
				StringUtils.isEmpty(application.getServiceUrl())
						|| checkUrl(application.getServiceUrl()), "URL is not valid");
Johannes Stelzer committed
68

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

72 73 74 75 76 77 78
		Application.Builder builder = Application.copyOf(application).withId(applicationId);
		Application existing = getApplication(applicationId);
		if (existing != null) {
			// Copy Status and Info from existing registration.
			builder.withStatusInfo(existing.getStatusInfo()).withInfo(existing.getInfo());
		}
		Application registering = builder.build();
79 80 81 82

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

Johannes Stelzer committed
94 95 96

	/**
	 * Checks the syntax of the given URL.
97
	 *
Johannes Stelzer committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111
	 * @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.
112
	 *
113
	 * @return List of all applications.
Johannes Stelzer committed
114
	 */
115 116 117 118 119 120
	public Collection<Application> getApplications() {
		return store.findAll();
	}

	/**
	 * Get a list of all registered applications.
121
	 *
122 123
	 * @param name the name to search for.
	 * @return List of applications with the given name.
124 125 126
	 */
	public Collection<Application> getApplicationsByName(String name) {
		return store.findByName(name);
Johannes Stelzer committed
127 128 129 130
	}

	/**
	 * Get a specific application inside the registry.
131
	 *
Johannes Stelzer committed
132 133 134 135
	 * @param id Id.
	 * @return Application.
	 */
	public Application getApplication(String id) {
136
		return store.find(id);
Johannes Stelzer committed
137 138 139 140
	}

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

	@Override
155
	public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
156
		publisher = applicationEventPublisher;
157
	}
Johannes Stelzer committed
158
}