/* * This class is part of the OSGiJMX plugin for the VisualVM, allowing * to have some basic administration functionality over an OSGi platform * * Copyright (C) 2009 Kiev Gama (kiev.gama at gmail.com) * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * **/ package osgijmx.impl; import java.lang.management.ManagementFactory; import javax.management.ObjectName; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.BundleEvent; import org.osgi.framework.Bundle; import org.osgi.framework.BundleListener; /** * * @author kiev */ public class Activator implements BundleActivator, BundleListener { private OSGiPlatformMXBeanImpl mxBean; private static final String PLATFORM_TYPE = "osgijmx:type=framework"; private static final String BUNDLE_TYPE = "osgijmx:type=bundle,id="; public void start(BundleContext ctx) throws Exception { this.mxBean = new OSGiPlatformMXBeanImpl(ctx); ObjectName name = new ObjectName(PLATFORM_TYPE); ManagementFactory.getPlatformMBeanServer().registerMBean(this.mxBean, name); Bundle[] bundles = ctx.getBundles(); for (Bundle bundle : bundles) { registerBundleMXBean(bundle); } ctx.addBundleListener(this); } public void stop(BundleContext ctx) throws Exception { ObjectName name = new ObjectName(PLATFORM_TYPE); ManagementFactory.getPlatformMBeanServer().unregisterMBean(name); Bundle[] bundles = ctx.getBundles(); ctx.removeBundleListener(this); for (Bundle bundle : bundles) { unregisterBundleMXBean(bundle); } } private void registerBundleMXBean(Bundle bundle) { BundleMXBeanImpl bean = new BundleMXBeanImpl(bundle); try { ObjectName name = new ObjectName(BUNDLE_TYPE + bundle.getBundleId()); ManagementFactory.getPlatformMBeanServer().registerMBean(bean, name); this.mxBean.addBundleMXBean(bean); } catch (Exception ex) { ex.printStackTrace(); } } private void unregisterBundleMXBean(Bundle bundle) { try { long bundleId = bundle.getBundleId(); ObjectName name = new ObjectName(BUNDLE_TYPE + bundleId); ManagementFactory.getPlatformMBeanServer().unregisterMBean(name); this.mxBean.removeBundleMXBean(this.mxBean.getBundle(bundleId)); } catch (Exception ex) { ex.printStackTrace(); } } public void bundleChanged(BundleEvent event) { if (event.getType() == BundleEvent.INSTALLED) { registerBundleMXBean(event.getBundle()); this.mxBean.sendNotification(event.getBundle().getBundleId()); } else if (event.getType() == BundleEvent.UNINSTALLED) { unregisterBundleMXBean(event.getBundle()); this.mxBean.sendNotification(event.getBundle().getBundleId()); } else { this.mxBean.sendNotification(event.getBundle().getBundleId()); } } }