Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PAYARA-2395 Fix Microprofile Config API for 4.181 release #2362

Merged
merged 2 commits into from
Feb 4, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,13 @@
import org.eclipse.microprofile.config.spi.Converter;
import org.glassfish.api.StartupRunLevel;
import org.glassfish.api.admin.ServerEnvironment;
import org.glassfish.api.event.EventListener;
import org.glassfish.api.event.Events;
import org.glassfish.api.invocation.ComponentInvocation;
import org.glassfish.api.invocation.InvocationManager;
import org.glassfish.hk2.runlevel.RunLevel;
import org.glassfish.internal.api.ServerContext;
import org.glassfish.internal.data.ApplicationInfo;
import org.glassfish.internal.data.ApplicationRegistry;
import org.glassfish.internal.data.ModuleInfo;
import org.glassfish.internal.deployment.Deployment;
import org.jvnet.hk2.annotations.Optional;
import org.jvnet.hk2.annotations.Service;

Expand All @@ -110,7 +107,7 @@
*/
@Service(name = "microprofile-config-provider") // this specifies that the classis an HK2 service
@RunLevel(StartupRunLevel.VAL)
public class ConfigProviderResolverImpl extends ConfigProviderResolver implements EventListener {
public class ConfigProviderResolverImpl extends ConfigProviderResolver {

private static final String METADATA_KEY = "MICROPROFILE_APP_CONFIG";
private static final String CUSTOM_SOURCES_KEY = "MICROPROFILE_CUSTOM_SOURCES";
Expand All @@ -127,11 +124,6 @@ public class ConfigProviderResolverImpl extends ConfigProviderResolver implement
@Inject
ApplicationRegistry applicationRegistry;

//Provides access to the event manager to hook into server lifecycle events
// or to raise various events
@Inject
private Events events;

// This injects the configuration from the domain.xml magically
// and for the correct server configuation
@Inject
Expand All @@ -148,39 +140,8 @@ public ConfigProviderResolverImpl() {
@PostConstruct
public void postConstruct() {
ConfigProviderResolver.setInstance(this);

// we need to listen for application deployment events so that we can read customer properties etc.
events.register(this);
}

/**
* Event handler needed to capture application load events
*
* @param event
*/
@Override
public void event(Event event) {
if (event.is(Deployment.APPLICATION_LOADED)) {

ApplicationInfo info = (ApplicationInfo) event.hook();
LinkedList<Properties> appConfigProperties = new LinkedList<>();
info.addTransientAppMetaData(APP_METADATA_KEY, appConfigProperties);
try {
// Read application defined properties and add as transient metadata
Enumeration<URL> resources = info.getAppClassLoader().getResources("META-INF/microprofile-config.properties");
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
Properties p = new Properties();
try (InputStream is = url.openStream()) {
p.load(url.openStream());
}
appConfigProperties.add(p);
}
} catch (IOException ex) {
Logger.getLogger(ConfigProviderResolverImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

public MicroprofileConfigConfiguration getMPConfig() {
if (configuration == null) {
Expand Down Expand Up @@ -261,6 +222,7 @@ Config getConfig(ApplicationInfo appInfo) {
result = appInfo.getTransientAppMetaData(METADATA_KEY, Config.class);
if (result == null) {
// build an application specific configuration
initialiseApplicationConfig(appInfo);
LinkedList<ConfigSource> sources = new LinkedList<>();
LinkedList<Converter> converters = new LinkedList<>();
sources.addAll(getDefaultSources());
Expand Down Expand Up @@ -433,5 +395,24 @@ List<Converter> getDiscoveredConverters(ApplicationInfo appInfo) {
}
return converters;
}

private void initialiseApplicationConfig(ApplicationInfo info) {
LinkedList<Properties> appConfigProperties = new LinkedList<>();
info.addTransientAppMetaData(APP_METADATA_KEY, appConfigProperties);
try {
// Read application defined properties and add as transient metadata
Enumeration<URL> resources = info.getAppClassLoader().getResources("META-INF/microprofile-config.properties");
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
Properties p = new Properties();
try (InputStream is = url.openStream()) {
p.load(url.openStream());
}
appConfigProperties.add(p);
}
} catch (IOException ex) {
Logger.getLogger(ConfigProviderResolverImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}

}