Repository: deltaspike Updated Branches: refs/heads/master a82f6ad27 -> 5c04cec3b
DELTASPIKE-1245 expose config via JMX bean initial version - needs review Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/0a8160f7 Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/0a8160f7 Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/0a8160f7 Branch: refs/heads/master Commit: 0a8160f7b721493db8f6728182fab252ce2916c6 Parents: 135c59f Author: Mark Struberg <[email protected]> Authored: Tue Apr 25 20:23:59 2017 +0200 Committer: Mark Struberg <[email protected]> Committed: Tue Apr 25 20:23:59 2017 +0200 ---------------------------------------------------------------------- .../core/api/config/ConfigResolver.java | 6 ++ deltaspike/core/impl/pom.xml | 1 + .../impl/config/ConfigurationExtension.java | 52 +++++++++++++ .../core/impl/config/DeltaSpikeConfigMBean.java | 63 ++++++++++++++++ deltaspike/modules/servlet/api/pom.xml | 2 +- deltaspike/modules/servlet/impl/pom.xml | 2 +- .../impl/config/ServletConfigListener.java | 79 ++++++++++++++++++++ .../impl/config/ServletConfigSource.java | 72 ++++++++++++++++++ deltaspike/modules/servlet/pom.xml | 4 +- .../test/utils/ShrinkWrapArchiveUtil.java | 9 ++- 10 files changed, 285 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/0a8160f7/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java index 2963ad8..5d0bf47 100644 --- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java +++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java @@ -62,6 +62,12 @@ import org.apache.deltaspike.core.util.ServiceUtils; @Typed() public final class ConfigResolver { + /** + * Can be used to tweak the application name. + * This will e.g. used in the JMX MBean to differentiate between applications. + */ + public static final String DELTASPIKE_APP_NAME_CONFIG = "deltaspike.application.name"; + private static final Logger LOG = Logger.getLogger(ConfigResolver.class.getName()); http://git-wip-us.apache.org/repos/asf/deltaspike/blob/0a8160f7/deltaspike/core/impl/pom.xml ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/pom.xml b/deltaspike/core/impl/pom.xml index a509370..e7259a4 100644 --- a/deltaspike/core/impl/pom.xml +++ b/deltaspike/core/impl/pom.xml @@ -56,6 +56,7 @@ <artifactId>deltaspike-core-api</artifactId> <version>${project.version}</version> </dependency> + <dependency> <groupId>org.apache.deltaspike.test</groupId> <artifactId>test-utils</artifactId> http://git-wip-us.apache.org/repos/asf/deltaspike/blob/0a8160f7/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigurationExtension.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigurationExtension.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigurationExtension.java index deb08e1..ea0dd38 100644 --- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigurationExtension.java +++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigurationExtension.java @@ -33,8 +33,11 @@ import javax.enterprise.inject.spi.Extension; import javax.enterprise.inject.spi.InjectionPoint; import javax.enterprise.inject.spi.ProcessAnnotatedType; import javax.enterprise.inject.spi.ProcessBean; +import javax.management.MBeanServer; +import javax.management.ObjectName; import java.lang.annotation.Annotation; +import java.lang.management.ManagementFactory; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.HashSet; @@ -101,6 +104,52 @@ public class ConfigurationExtension implements Extension, Deactivatable protected void init(@Observes BeforeBeanDiscovery beforeBeanDiscovery) { isActivated = ClassDeactivationUtils.isActivated(getClass()); + + if (isActivated) + { + registerConfigMBean(); + } + } + + public static void registerConfigMBean() + { + String appName = ConfigResolver.getPropertyValue(ConfigResolver.DELTASPIKE_APP_NAME_CONFIG); + if (appName != null && appName.length() > 0) + { + try + { + MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); + + ClassLoader tccl = ClassUtils.getClassLoader(ConfigurationExtension.class); + DeltaSpikeConfigMBean cfgMBean = new DeltaSpikeConfigMBean(tccl); + + ObjectName name = new ObjectName("deltaspike.config." + appName + ":type=DeltaSpikeConfig"); + mBeanServer.registerMBean(cfgMBean, name); + } + catch (Exception e) + { + throw new RuntimeException(e); + } + } + } + + public static void unRegisterConfigMBean(String appName) + { + if (appName != null && appName.length() > 0) + { + try + { + MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); + + ObjectName name = new ObjectName("deltaspike.config." + appName + ":type=DeltaSpikeConfig"); + + mBeanServer.unregisterMBean(name); + } + catch (Exception e) + { + throw new RuntimeException(e); + } + } } @SuppressWarnings("UnusedDeclaration") @@ -284,6 +333,9 @@ public class ConfigurationExtension implements Extension, Deactivatable @SuppressWarnings("UnusedDeclaration") public void freeConfigSources(@Observes BeforeShutdown bs) { + String appName = ConfigResolver.getPropertyValue(ConfigResolver.DELTASPIKE_APP_NAME_CONFIG); + unRegisterConfigMBean(appName); + ConfigResolver.freeConfigSources(); detectedParentPropertyFileConfigs.remove(ClassUtils.getClassLoader(null)); } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/0a8160f7/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DeltaSpikeConfigMBean.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DeltaSpikeConfigMBean.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DeltaSpikeConfigMBean.java new file mode 100644 index 0000000..a91d0e8 --- /dev/null +++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DeltaSpikeConfigMBean.java @@ -0,0 +1,63 @@ +/* + * 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.ConfigResolver; +import org.apache.deltaspike.core.spi.config.ConfigSource; + +import java.util.ArrayList; +import java.util.List; + +/** + * JMX MBean for DeltaSpike + */ +public class DeltaSpikeConfigMBean +{ + private final ClassLoader appConfigClassLoader; + + public DeltaSpikeConfigMBean(ClassLoader appConfigClassLoader) + { + this.appConfigClassLoader = appConfigClassLoader; + } + + public List<String> getConfigSources() + { + ClassLoader originalCl = Thread.currentThread().getContextClassLoader(); + try + { + Thread.currentThread().setContextClassLoader(appConfigClassLoader); + List<String> configSourceInfo = new ArrayList<String>(); + ConfigSource[] configSources = ConfigResolver.getConfigSources(); + for (ConfigSource configSource : configSources) + { + configSourceInfo.add(Integer.toString(configSource.getOrdinal()) + + " - " + configSource.getConfigName()); + } + + return configSourceInfo; + } + finally + { + // set back the original TCCL + Thread.currentThread().setContextClassLoader(originalCl); + } + } + + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/0a8160f7/deltaspike/modules/servlet/api/pom.xml ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/api/pom.xml b/deltaspike/modules/servlet/api/pom.xml index ace0f9b..0c1695e 100644 --- a/deltaspike/modules/servlet/api/pom.xml +++ b/deltaspike/modules/servlet/api/pom.xml @@ -39,7 +39,7 @@ <dependency> <groupId>org.apache.geronimo.specs</groupId> - <artifactId>geronimo-servlet_2.5_spec</artifactId> + <artifactId>geronimo-servlet_3.0_spec</artifactId> <scope>provided</scope> </dependency> http://git-wip-us.apache.org/repos/asf/deltaspike/blob/0a8160f7/deltaspike/modules/servlet/impl/pom.xml ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/impl/pom.xml b/deltaspike/modules/servlet/impl/pom.xml index 25b94e3..bd839c3 100644 --- a/deltaspike/modules/servlet/impl/pom.xml +++ b/deltaspike/modules/servlet/impl/pom.xml @@ -49,7 +49,7 @@ <dependency> <groupId>org.apache.geronimo.specs</groupId> - <artifactId>geronimo-servlet_2.5_spec</artifactId> + <artifactId>geronimo-servlet_3.0_spec</artifactId> <scope>provided</scope> </dependency> http://git-wip-us.apache.org/repos/asf/deltaspike/blob/0a8160f7/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/config/ServletConfigListener.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/config/ServletConfigListener.java b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/config/ServletConfigListener.java new file mode 100644 index 0000000..9c1dee4 --- /dev/null +++ b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/config/ServletConfigListener.java @@ -0,0 +1,79 @@ +/* + * 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.servlet.impl.config; + +import org.apache.deltaspike.core.api.config.ConfigResolver; +import org.apache.deltaspike.core.api.exclude.Exclude; +import org.apache.deltaspike.core.impl.config.ConfigurationExtension; +import org.apache.deltaspike.core.spi.config.ConfigSource; + +import javax.enterprise.inject.Typed; +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; +import javax.servlet.annotation.WebListener; + +/** + * Provide the application name in servlet environments. + */ +@WebListener +@Typed +@Exclude +public class ServletConfigListener implements ServletContextListener +{ + @Override + public void contextInitialized(ServletContextEvent sce) + { + ConfigSource[] configSources = ConfigResolver.getConfigSources(); + for (ConfigSource configSource : configSources) + { + if (configSource instanceof ServletConfigSource) + { + setServletConfig((ServletConfigSource) configSource, sce); + return; + } + } + } + + private void setServletConfig(ServletConfigSource configSource, ServletContextEvent sce) + { + ServletContext servletContext = sce.getServletContext(); + String servletContextName = servletContext.getServletContextName(); + if (servletContextName != null && servletContextName.length() > 0) + { + String oldAppName = ConfigResolver.getPropertyValue(ConfigResolver.DELTASPIKE_APP_NAME_CONFIG); + + // we first need to unregister the old MBean + // as we don't know whether the CDI Extension or the Servlet Listener comes first. + // It's simply not defined by the spec :/ + ConfigurationExtension.unRegisterConfigMBean(oldAppName); + + configSource.setPropertyValue(ConfigResolver.DELTASPIKE_APP_NAME_CONFIG, servletContextName); + + // and as we now did set the new name -> register again: + ConfigurationExtension.registerConfigMBean(); + } + } + + @Override + public void contextDestroyed(ServletContextEvent sce) + { + // nothing to do + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/0a8160f7/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/config/ServletConfigSource.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/config/ServletConfigSource.java b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/config/ServletConfigSource.java new file mode 100644 index 0000000..c54d8f0 --- /dev/null +++ b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/config/ServletConfigSource.java @@ -0,0 +1,72 @@ +/* + * 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.servlet.impl.config; + +import org.apache.deltaspike.core.impl.config.BaseConfigSource; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +/** + * This is an _optional_ ConfigSource! + * It will only provide information if running in a Servlet container! + */ +public class ServletConfigSource extends BaseConfigSource +{ + private final ConcurrentMap<String, String> servletProperties; + + public ServletConfigSource() + { + servletProperties = new ConcurrentHashMap<String, String>(); + initOrdinal(50); + } + + + + public void setPropertyValue(String key, String value) + { + servletProperties.put(key, value); + } + + @Override + public Map<String, String> getProperties() + { + return servletProperties; + } + + @Override + public String getPropertyValue(String key) + { + return servletProperties.get(key); + } + + @Override + public String getConfigName() + { + return "servletconfig-properties"; + } + + @Override + public boolean isScannable() + { + return true; + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/0a8160f7/deltaspike/modules/servlet/pom.xml ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/pom.xml b/deltaspike/modules/servlet/pom.xml index cb17eae..8f3666b 100644 --- a/deltaspike/modules/servlet/pom.xml +++ b/deltaspike/modules/servlet/pom.xml @@ -42,8 +42,8 @@ <dependencies> <dependency> <groupId>org.apache.geronimo.specs</groupId> - <artifactId>geronimo-servlet_2.5_spec</artifactId> - <version>1.2</version> + <artifactId>geronimo-servlet_3.0_spec</artifactId> + <version>1.0</version> <scope>provided</scope> </dependency> </dependencies> http://git-wip-us.apache.org/repos/asf/deltaspike/blob/0a8160f7/deltaspike/test-utils/src/main/java/org/apache/deltaspike/test/utils/ShrinkWrapArchiveUtil.java ---------------------------------------------------------------------- diff --git a/deltaspike/test-utils/src/main/java/org/apache/deltaspike/test/utils/ShrinkWrapArchiveUtil.java b/deltaspike/test-utils/src/main/java/org/apache/deltaspike/test/utils/ShrinkWrapArchiveUtil.java index d2d4043..e978fe3 100644 --- a/deltaspike/test-utils/src/main/java/org/apache/deltaspike/test/utils/ShrinkWrapArchiveUtil.java +++ b/deltaspike/test-utils/src/main/java/org/apache/deltaspike/test/utils/ShrinkWrapArchiveUtil.java @@ -207,7 +207,14 @@ public class ShrinkWrapArchiveUtil String className = pathToClassName(entryName.substring(0, entryName.length() - (".class".length()))); - javaArchive.addClass(className); + try + { + javaArchive.addClass(className); + } + catch (Throwable t) + { + LOG.info("Ignoring class " + className + " due to " + t.getMessage()); + } } else {
