001/* 002 * Copyright 2021 Anyware Services 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 */ 016package org.ametys.core.mbean; 017 018import java.lang.management.ManagementFactory; 019import java.util.Hashtable; 020 021import javax.management.DynamicMBean; 022import javax.management.MBeanServer; 023import javax.management.ObjectName; 024 025import org.apache.commons.lang3.StringUtils; 026 027import org.ametys.runtime.plugin.Init; 028import org.ametys.runtime.plugin.component.AbstractThreadSafeComponentExtensionPoint; 029import org.ametys.runtime.util.AmetysHomeHelper; 030 031/** 032 * Extension point holding all {@link Init} classes loaded after all components and extensions. 033 */ 034public class MBeanExtensionPoint extends AbstractThreadSafeComponentExtensionPoint<DynamicMBean> 035{ 036 /** Avalon Role */ 037 public static final String ROLE = MBeanExtensionPoint.class.getName(); 038 039 @Override 040 public void initializeExtensions() throws Exception 041 { 042 super.initializeExtensions(); 043 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 044 for (String ePId : getExtensionsIds()) 045 { 046 DynamicMBean mbean = getExtension(ePId); 047 String key = AmetysHomeHelper.getAmetysHome().getAbsolutePath(); 048 String className = mbean.getMBeanInfo().getClassName(); 049 Hashtable<String, String> properties = new Hashtable<>(); 050 properties.put("context", StringUtils.replace(StringUtils.remove(key, ":"), "=", "_")); 051 properties.put("type", className); 052 ObjectName name = new ObjectName("org.ametys", properties); 053 // If for any reason, there was already an MBean with the same name, we replace it. 054 if (mbs.isRegistered(name)) 055 { 056 mbs.unregisterMBean(name); 057 } 058 mbs.registerMBean(mbean, name); 059 } 060 } 061}