Skip to content

Commit

Permalink
SHIBUI-836: wip... property replacer API
Browse files Browse the repository at this point in the history
  • Loading branch information
dima767 committed Sep 11, 2018
1 parent 0cd4254 commit 7c8ef09
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Profile;
import org.springframework.context.event.EventListener;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.PropertyResolver;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -40,12 +42,22 @@ public static class MetadataResolversResourceIdEmitter {
@Value("${idp.home}")
String idpHome;

@Autowired
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer;

@Autowired
PropertyResolver propertyResolver;

@EventListener
void showMetadataResolversResourceIds(ApplicationStartedEvent e) {
metadataResolverRepository.findAll()
.forEach(it -> System.out.println(String.format("MetadataResolver [%s: %s]", it.getName(), it.getResourceId())));

System.out.println("IDP HOME: " + idpHome);
String rawValue = "%{idp.home}/metadata/file.xml";
boolean hasToken = rawValue.contains("%{idp.home}");
String resolvedIdpHome = propertyResolver.resolvePlaceholders("${idp.home}");
String fullyResolvedDir = rawValue.replace("%{idp.home}", resolvedIdpHome);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import java.util.Map;

/**
* Spring Boot Environment Post Processor setting the value for idp.home property to an abstract temp directory.
* Spring Boot Environment Post Processor setting the value for idp.home property to an abstract temp directory
* if no IDP_HOME environment variable has been set already.
*
* @author Dmitriy Kopylenko
*/
Expand Down Expand Up @@ -47,7 +48,11 @@ public void postProcessEnvironment(ConfigurableEnvironment environment, SpringAp

Map<String, Object> map = new HashMap<>(1);
try {
map.put(IDP_HOME_PROP, Files.createTempDirectory(null).toAbsolutePath().toString());
Path tempDir = Files.createTempDirectory(String.format("%s.%s.", "shibui", IDP_HOME_PROP));
String tempDirName = tempDir.toAbsolutePath().toString();
Path tempMetadataSubDir = Paths.get(tempDirName, METADATA_DIR);
Files.createDirectories(tempMetadataSubDir);
map.put(IDP_HOME_PROP, tempDirName);
} catch (IOException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package edu.internet2.tier.shibboleth.admin.ui.domain.resolvers;

/**
* An SPI to resolve and convert data for different types of {@link MetadataResolver}s from possible provided '%{}' placeholders
* <p>
* Typical usage is - multiple replacers for concrete type of resolvers are configured in Spring Application Context,
* aggregated by {@link MetadataResolverPropertyPlaceholderValuesResolvingService} facade and then that facade is injected into upstream consumers of it
* such as REST controllers, etc.
*
* @author Dmitriy Kopylenko
*/
public interface MetadataResolverPropertyPlaceholderValuesReplacer<T extends MetadataResolver> {

boolean supports(T metadataResolver);

void replacePlaceholderValuesIfResolvableOrFail(T metadataResolver);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package edu.internet2.tier.shibboleth.admin.ui.domain.resolvers;

import java.util.ArrayList;
import java.util.List;

/**
* A facade that aggregates {@link MetadataResolverPropertyPlaceholderValuesReplacer}s available to call just one of them supporting the type of a given resolver.
* If no {@link MetadataResolverPropertyPlaceholderValuesReplacer}s are configured, considers a noop.
*
* Uses chain-of-responsibility design pattern
*
* @author Dmitriy Kopylenko
*/
public class MetadataResolverPropertyPlaceholderValuesResolvingService<T extends MetadataResolver> {

private List<MetadataResolverPropertyPlaceholderValuesReplacer<T>> replacers;

public MetadataResolverPropertyPlaceholderValuesResolvingService(List<MetadataResolverPropertyPlaceholderValuesReplacer<T>> replacers) {
this.replacers = replacers != null ? replacers : new ArrayList<>();
}

public void replacePlacehoderValuesOrFail(T metadataResolver) {
this.replacers
.stream()
.filter(r -> r.supports(metadataResolver))
.findFirst()
.ifPresent(r -> r.replacePlaceholderValuesIfResolvableOrFail(metadataResolver));
}
}

0 comments on commit 7c8ef09

Please sign in to comment.