/*
* 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.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import osgijmx.BundleMXBean;
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
/**
*
* @author kiev
*/
class BundleMXBeanImpl implements BundleMXBean{
private Bundle bundle;
public BundleMXBeanImpl(Bundle bundle) {
this.bundle = bundle;
}
public long getBundleId() {
return bundle.getBundleId();
}
public int getState() {
return bundle.getState();
}
public String getSymbolicName() {
return bundle.getSymbolicName();
}
public String getLocation() {
return bundle.getLocation();
}
public void start() throws Exception {
bundle.start();
}
public void stop() throws Exception {
bundle.stop();
}
public void update() throws Exception {
bundle.update();
}
public void uninstall() throws Exception {
bundle.uninstall();
}
public Map getBundleHeaders() {
Enumeration e = bundle.getHeaders().keys();
HashMap map = new HashMap();
while (e.hasMoreElements()) {
String key = (String)e.nextElement();
map.put(key, (String)bundle.getHeaders().get(key));
}
return map;
}
public Map> getRegisteredServices() {
ServiceReference[] refs = bundle.getRegisteredServices();
Map[] result = new Map[refs == null ? 0 : refs.length];
Map hash = new TreeMap();
for (int i = 0; i < result.length; i++) {
hash.put(refs[i].getProperty(Constants.SERVICE_ID).toString(), getProperties(refs[i]));
result[i] = getProperties(refs[i]);
}
return hash;
}
private Map getProperties(ServiceReference ref) {
String[] keys = ref.getPropertyKeys();
Map hash = new TreeMap();
for (String key : keys) {
Object propValue = ref.getProperty(key);
System.out.println(key);
if (propValue instanceof Object[]) {
Object[] property = (Object[])propValue;
System.out.println(property.getClass() + " size: " + property.length);
StringBuilder sb = new StringBuilder();
for (Object subproperty : property) {
System.out.println(" " + subproperty.getClass());
sb.append(subproperty.toString());
sb.append(",");
}
if (sb.length() > 0) {
propValue = sb.substring(0, sb.length() - 1);
}
}
hash.put(key,propValue == null ? "" :propValue.toString());
}
return hash;
}
public List getAllEntries() {
ArrayList result = new ArrayList();
try {
Enumeration e = (bundle.findEntries("/","*",true));
if (e != null) {
while (e.hasMoreElements()) {
String path = ((URL) e.nextElement()).getPath();
if (path != null && path.length() > 0 && !path.endsWith("/")) {
result.add(path);
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
}