/*
* 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.util.ArrayList;
import java.util.List;
import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;
import javax.management.ObjectName;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import osgijmx.BundleMXBean;
import osgijmx.OSGiPlatformMXBean;
/**
*
* @author kiev
*/
class OSGiPlatformMXBeanImpl extends NotificationBroadcasterSupport implements OSGiPlatformMXBean {
private ArrayList bundles = new ArrayList();
private BundleContext ctx;
private static int sequenceNumber;
public OSGiPlatformMXBeanImpl(BundleContext ctx) {
this.ctx = ctx;
}
void addBundleMXBean(BundleMXBean bean) {
synchronized (bundles) {
bundles.add(bean);
}
}
void removeBundleMXBean(BundleMXBean bean) {
synchronized (bundles) {
bundles.remove(bean);
}
}
public List getBundles() {
return bundles;
}
public BundleMXBean getBundle(long bundleID) {
for (BundleMXBean bundleBean : bundles) {
if (bundleBean.getBundleId() == bundleID)
return bundleBean;
}
return null;
}
public String getFrameworkVersion() {
return ctx.getProperty(Constants.FRAMEWORK_VERSION);
}
public String getFrameworkVendor() {
return ctx.getProperty(Constants.FRAMEWORK_VENDOR);
}
public String getFrameworkLanguage() {
return ctx.getProperty(Constants.FRAMEWORK_LANGUAGE);
}
public void installBundle(String location) throws Exception {
ctx.installBundle(location);
}
synchronized void sendNotification(long bundleId) {
try {
//a really simple notification. maybe add more information to it later...
Notification n = new Notification("Bundle-Changed",new ObjectName("osgijmx:type=bundle,id=" + bundleId),sequenceNumber++);
n.setUserData(bundleId);
sendNotification(n);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}