001/* 002 * Copyright 2012-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot; 018 019import java.io.PrintStream; 020import java.nio.charset.Charset; 021import java.nio.charset.StandardCharsets; 022import java.util.ArrayList; 023import java.util.Collections; 024import java.util.HashMap; 025import java.util.List; 026import java.util.Map; 027 028import org.apache.commons.logging.Log; 029import org.apache.commons.logging.LogFactory; 030 031import org.springframework.boot.ansi.AnsiPropertySource; 032import org.springframework.core.env.Environment; 033import org.springframework.core.env.MapPropertySource; 034import org.springframework.core.env.MutablePropertySources; 035import org.springframework.core.env.PropertyResolver; 036import org.springframework.core.env.PropertySourcesPropertyResolver; 037import org.springframework.core.io.Resource; 038import org.springframework.util.Assert; 039import org.springframework.util.StreamUtils; 040 041/** 042 * Banner implementation that prints from a source text {@link Resource}. 043 * 044 * @author Phillip Webb 045 * @author Vedran Pavic 046 * @since 1.2.0 047 */ 048public class ResourceBanner implements Banner { 049 050 private static final Log logger = LogFactory.getLog(ResourceBanner.class); 051 052 private Resource resource; 053 054 public ResourceBanner(Resource resource) { 055 Assert.notNull(resource, "Resource must not be null"); 056 Assert.isTrue(resource.exists(), "Resource must exist"); 057 this.resource = resource; 058 } 059 060 @Override 061 public void printBanner(Environment environment, Class<?> sourceClass, 062 PrintStream out) { 063 try { 064 String banner = StreamUtils.copyToString(this.resource.getInputStream(), 065 environment.getProperty("spring.banner.charset", Charset.class, 066 StandardCharsets.UTF_8)); 067 068 for (PropertyResolver resolver : getPropertyResolvers(environment, 069 sourceClass)) { 070 banner = resolver.resolvePlaceholders(banner); 071 } 072 out.println(banner); 073 } 074 catch (Exception ex) { 075 logger.warn("Banner not printable: " + this.resource + " (" + ex.getClass() 076 + ": '" + ex.getMessage() + "')", ex); 077 } 078 } 079 080 protected List<PropertyResolver> getPropertyResolvers(Environment environment, 081 Class<?> sourceClass) { 082 List<PropertyResolver> resolvers = new ArrayList<>(); 083 resolvers.add(environment); 084 resolvers.add(getVersionResolver(sourceClass)); 085 resolvers.add(getAnsiResolver()); 086 resolvers.add(getTitleResolver(sourceClass)); 087 return resolvers; 088 } 089 090 private PropertyResolver getVersionResolver(Class<?> sourceClass) { 091 MutablePropertySources propertySources = new MutablePropertySources(); 092 propertySources 093 .addLast(new MapPropertySource("version", getVersionsMap(sourceClass))); 094 return new PropertySourcesPropertyResolver(propertySources); 095 } 096 097 private Map<String, Object> getVersionsMap(Class<?> sourceClass) { 098 String appVersion = getApplicationVersion(sourceClass); 099 String bootVersion = getBootVersion(); 100 Map<String, Object> versions = new HashMap<>(); 101 versions.put("application.version", getVersionString(appVersion, false)); 102 versions.put("spring-boot.version", getVersionString(bootVersion, false)); 103 versions.put("application.formatted-version", getVersionString(appVersion, true)); 104 versions.put("spring-boot.formatted-version", 105 getVersionString(bootVersion, true)); 106 return versions; 107 } 108 109 protected String getApplicationVersion(Class<?> sourceClass) { 110 Package sourcePackage = (sourceClass != null) ? sourceClass.getPackage() : null; 111 return (sourcePackage != null) ? sourcePackage.getImplementationVersion() : null; 112 } 113 114 protected String getBootVersion() { 115 return SpringBootVersion.getVersion(); 116 } 117 118 private String getVersionString(String version, boolean format) { 119 if (version == null) { 120 return ""; 121 } 122 return format ? " (v" + version + ")" : version; 123 } 124 125 private PropertyResolver getAnsiResolver() { 126 MutablePropertySources sources = new MutablePropertySources(); 127 sources.addFirst(new AnsiPropertySource("ansi", true)); 128 return new PropertySourcesPropertyResolver(sources); 129 } 130 131 private PropertyResolver getTitleResolver(Class<?> sourceClass) { 132 MutablePropertySources sources = new MutablePropertySources(); 133 String applicationTitle = getApplicationTitle(sourceClass); 134 Map<String, Object> titleMap = Collections.singletonMap("application.title", 135 (applicationTitle != null) ? applicationTitle : ""); 136 sources.addFirst(new MapPropertySource("title", titleMap)); 137 return new PropertySourcesPropertyResolver(sources); 138 } 139 140 protected String getApplicationTitle(Class<?> sourceClass) { 141 Package sourcePackage = (sourceClass != null) ? sourceClass.getPackage() : null; 142 return (sourcePackage != null) ? sourcePackage.getImplementationTitle() : null; 143 } 144 145}