Repository: deltaspike Updated Branches: refs/heads/master 8242a4c17 -> 11e1cf724
DELTASPIKE-1325 use AutoCloseable to free resources Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/11e1cf72 Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/11e1cf72 Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/11e1cf72 Branch: refs/heads/master Commit: 11e1cf724a0e40949882ab09bd5f57ad50a509c2 Parents: 8242a4c Author: Mark Struberg <strub...@apache.org> Authored: Wed Mar 7 18:01:40 2018 +0100 Committer: Mark Struberg <strub...@apache.org> Committed: Wed Mar 7 18:01:40 2018 +0100 ---------------------------------------------------------------------- .../deltaspike/core/impl/config/ConfigImpl.java | 33 ++++ .../core/impl/config/ConfigProviderImpl.java | 197 ++++++++++--------- 2 files changed, 134 insertions(+), 96 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/11e1cf72/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java index af0e414..eefc612 100644 --- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java +++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java @@ -86,6 +86,39 @@ public class ConfigImpl implements Config this.configFilters = new CopyOnWriteArrayList<>(configFilters); } + /** + * Shuts down the Config. + * This will also close all ConfigSources and ConfigFilters which + * implment the {@link java.lang.AutoCloseable} interface. + */ + void release() + { + for (ConfigSource configSource : configSources) + { + close(configSource); + } + + for (ConfigFilter configFilter : configFilters) + { + close(configFilter); + } + } + + private void close(Object o) + { + if (o instanceof AutoCloseable) + { + try + { + ((AutoCloseable) o).close(); + } + catch (Exception e) + { + LOG.log(Level.INFO, "Exception while closing " + o.toString(), e); + } + } + } + @Override public ConfigSource[] getConfigSources() http://git-wip-us.apache.org/repos/asf/deltaspike/blob/11e1cf72/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigProviderImpl.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigProviderImpl.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigProviderImpl.java index e788f8d..ea1b32f 100644 --- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigProviderImpl.java +++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigProviderImpl.java @@ -1,96 +1,101 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 org.apache.deltaspike.core.impl.config; - -import org.apache.deltaspike.core.api.config.Config; -import org.apache.deltaspike.core.api.config.ConfigResolver; -import org.apache.deltaspike.core.util.ClassUtils; - -import java.util.Iterator; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -/** - */ -public class ConfigProviderImpl implements ConfigResolver.ConfigProvider -{ - /** - * The content of this map will get lazily initiated and will hold the - * Configs for each WebApp/EAR, etc (thus the ClassLoader). - */ - private static Map<ClassLoader, ConfigImpl> configs = new ConcurrentHashMap<>(); - - @Override - public Config getConfig() - { - ClassLoader cl = ClassUtils.getClassLoader(null); - return getConfig(cl); - } - - @Override - public Config getConfig(ClassLoader cl) - { - ConfigImpl config = configs.get(cl); - if (config == null) - { - config = new ConfigImpl(cl); - config.init(); - ConfigImpl oldConfig = configs.put(cl, config); - if (oldConfig != null) - { - config = oldConfig; - } - } - return config; - } - - @Override - public void releaseConfig(ClassLoader cl) - { - configs.remove(cl); - - // And remove all the children as well. - // This will e.g happen in EAR scenarios - Iterator<Map.Entry<ClassLoader, ConfigImpl>> it = configs.entrySet().iterator(); - while (it.hasNext()) - { - Map.Entry<ClassLoader, ConfigImpl> cfgEntry = it.next(); - if (isChildClassLoader(cl, cfgEntry.getKey())) - { - it.remove(); - } - } - } - - private boolean isChildClassLoader(ClassLoader configClassLoader, ClassLoader suspect) - { - ClassLoader suspectParentCl = suspect.getParent(); - if (suspectParentCl == null) - { - return false; - } - - if (suspectParentCl == configClassLoader) - { - return true; - } - - return isChildClassLoader(configClassLoader, suspectParentCl); - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.deltaspike.core.impl.config; + +import org.apache.deltaspike.core.api.config.Config; +import org.apache.deltaspike.core.api.config.ConfigResolver; +import org.apache.deltaspike.core.util.ClassUtils; + +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + */ +public class ConfigProviderImpl implements ConfigResolver.ConfigProvider +{ + /** + * The content of this map will get lazily initiated and will hold the + * Configs for each WebApp/EAR, etc (thus the ClassLoader). + */ + private static Map<ClassLoader, ConfigImpl> configs = new ConcurrentHashMap<>(); + + @Override + public Config getConfig() + { + ClassLoader cl = ClassUtils.getClassLoader(null); + return getConfig(cl); + } + + @Override + public Config getConfig(ClassLoader cl) + { + ConfigImpl config = configs.get(cl); + if (config == null) + { + config = new ConfigImpl(cl); + config.init(); + ConfigImpl oldConfig = configs.put(cl, config); + if (oldConfig != null) + { + config = oldConfig; + } + } + return config; + } + + @Override + public void releaseConfig(ClassLoader cl) + { + ConfigImpl oldConfig = configs.remove(cl); + if (oldConfig != null) + { + oldConfig.release(); + } + + // And remove all the children as well. + // This will e.g happen in EAR scenarios + Iterator<Map.Entry<ClassLoader, ConfigImpl>> it = configs.entrySet().iterator(); + while (it.hasNext()) + { + Map.Entry<ClassLoader, ConfigImpl> cfgEntry = it.next(); + if (isChildClassLoader(cl, cfgEntry.getKey())) + { + cfgEntry.getValue().release(); + it.remove(); + } + } + } + + private boolean isChildClassLoader(ClassLoader configClassLoader, ClassLoader suspect) + { + ClassLoader suspectParentCl = suspect.getParent(); + if (suspectParentCl == null) + { + return false; + } + + if (suspectParentCl == configClassLoader) + { + return true; + } + + return isChildClassLoader(configClassLoader, suspectParentCl); + } +}