Search in sources :

Example 1 with WebBundle

use of org.apache.karaf.web.WebBundle in project karaf by apache.

the class WebContainerServiceImpl method list.

public List<WebBundle> list() throws Exception {
    Bundle[] bundles = bundleContext.getBundles();
    Map<Long, WebEvent> bundleEvents = webEventHandler.getBundleEvents();
    List<WebBundle> webBundles = new ArrayList<>();
    if (bundles != null) {
        for (Bundle bundle : bundles) {
            // first check if the bundle is a web bundle
            String contextPath = bundle.getHeaders().get("Web-ContextPath");
            if (contextPath == null) {
                // this one used by pax-web but is deprecated
                contextPath = bundle.getHeaders().get("Webapp-Context");
            }
            if (contextPath == null) {
                // the bundle is not a web bundle
                continue;
            }
            WebBundle webBundle = new WebBundle();
            contextPath = contextPath.trim();
            // get the bundle name
            String name = bundle.getHeaders().get(Constants.BUNDLE_NAME);
            // if there is no name, then default to symbolic name
            name = (name == null) ? bundle.getSymbolicName() : name;
            // if there is no symbolic name, resort to location
            name = (name == null) ? bundle.getLocation() : name;
            // get the bundle version
            String version = bundle.getHeaders().get(Constants.BUNDLE_VERSION);
            name = ((version != null)) ? name + " (" + version + ")" : name;
            long bundleId = bundle.getBundleId();
            int level = bundle.adapt(BundleStartLevel.class).getStartLevel();
            if (!contextPath.startsWith("/")) {
                contextPath = "/" + contextPath;
            }
            webBundle.setBundleId(bundleId);
            webBundle.setName(name);
            webBundle.setContextPath(contextPath);
            webBundle.setLevel(level);
            webBundle.setState(getStateString(bundle));
            webBundle.setWebState(state(bundle.getBundleId()));
            webBundles.add(webBundle);
        }
    }
    return webBundles;
}
Also used : BundleStartLevel(org.osgi.framework.startlevel.BundleStartLevel) WebBundle(org.apache.karaf.web.WebBundle) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) WebBundle(org.apache.karaf.web.WebBundle) WebEvent(org.ops4j.pax.web.service.spi.WebEvent)

Example 2 with WebBundle

use of org.apache.karaf.web.WebBundle in project karaf by apache.

the class WebMBeanImpl method getWebBundles.

public TabularData getWebBundles() throws MBeanException {
    try {
        CompositeType webType = new CompositeType("Web Bundle", "An OSGi Web bundle", new String[] { "ID", "State", "Web-State", "Level", "Web-ContextPath", "Name" }, new String[] { "ID of the bundle", "OSGi state of the bundle", "Web state of the bundle", "Start level of the bundle", "Web context path", "Name of the bundle" }, new OpenType[] { SimpleType.LONG, SimpleType.STRING, SimpleType.STRING, SimpleType.INTEGER, SimpleType.STRING, SimpleType.STRING });
        TabularType tableType = new TabularType("Web Bundles", "Table of web bundles", webType, new String[] { "ID" });
        TabularData table = new TabularDataSupport(tableType);
        for (WebBundle webBundle : webContainerService.list()) {
            try {
                CompositeData data = new CompositeDataSupport(webType, new String[] { "ID", "State", "Web-State", "Level", "Web-ContextPath", "Name" }, new Object[] { webBundle.getBundleId(), webBundle.getState(), webBundle.getWebState(), webBundle.getLevel(), webBundle.getContextPath(), webBundle.getName() });
                table.put(data);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return table;
    } catch (Exception e) {
        throw new MBeanException(null, e.toString());
    }
}
Also used : WebBundle(org.apache.karaf.web.WebBundle) MBeanException(javax.management.MBeanException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) MBeanException(javax.management.MBeanException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException)

Example 3 with WebBundle

use of org.apache.karaf.web.WebBundle in project karaf by apache.

the class List method execute.

@Override
public Object execute() throws Exception {
    ShellTable table = new ShellTable();
    table.column(new Col("ID"));
    table.column(new Col("State"));
    table.column(new Col("Web-State"));
    table.column(new Col("Level"));
    table.column(new Col("Web-ContextPath"));
    table.column(new Col("Name"));
    java.util.List<WebBundle> webBundles = webContainerService.list();
    if (webBundles != null && !webBundles.isEmpty()) {
        for (WebBundle webBundle : webBundles) {
            table.addRow().addContent(webBundle.getBundleId(), webBundle.getState(), webBundle.getWebState(), webBundle.getLevel(), webBundle.getContextPath(), webBundle.getName());
        }
    }
    table.print(System.out, !noFormat);
    return null;
}
Also used : Col(org.apache.karaf.shell.support.table.Col) ShellTable(org.apache.karaf.shell.support.table.ShellTable) WebBundle(org.apache.karaf.web.WebBundle)

Aggregations

WebBundle (org.apache.karaf.web.WebBundle)3 ArrayList (java.util.ArrayList)1 MBeanException (javax.management.MBeanException)1 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)1 Col (org.apache.karaf.shell.support.table.Col)1 ShellTable (org.apache.karaf.shell.support.table.ShellTable)1 WebEvent (org.ops4j.pax.web.service.spi.WebEvent)1 Bundle (org.osgi.framework.Bundle)1 BundleStartLevel (org.osgi.framework.startlevel.BundleStartLevel)1