Package org.apache.felix.webconsole.plugins.packageadmin.internal

Source Code of org.apache.felix.webconsole.plugins.packageadmin.internal.WebConsolePrinter

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.felix.webconsole.plugins.packageadmin.internal;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.felix.webconsole.ConfigurationPrinter;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.service.packageadmin.ExportedPackage;
import org.osgi.service.packageadmin.PackageAdmin;
import org.osgi.util.tracker.ServiceTracker;

class WebConsolePrinter implements ConfigurationPrinter
{

    private final ServiceTracker tracker;
    private final BundleContext bc;

    WebConsolePrinter(BundleContext bc, ServiceTracker tracker)
    {
        this.bc = bc;
        this.tracker = tracker;
    }

    /**
     * @see org.apache.felix.webconsole.ConfigurationPrinter#printConfiguration(java.io.PrintWriter)
     */
    public void printConfiguration(PrintWriter pw)
    {
        final PackageAdmin pa = (PackageAdmin) tracker.getService();
        if (pa == null)
        {
            pw.println("Status: PackageAdmin Service not registered");
            return;
        }

        try
        {
            Map/*<String, Set<ExportedPackage>>*/exports = WebConsolePlugin.collectExportedPackages(
                pa, bc);

            pw.print("Status: PackageAdmin service reports ");
            pw.print(String.valueOf(exports.size()));
            pw.println(" exported packages.");
            pw.println();

            dumpDuplicatesAsTxt(pw, exports);
        }
        catch (Exception e)
        {
            pw.println("failure ...." + e);
        }
    }

    private void dumpDuplicatesAsTxt(final PrintWriter pw,
        final Map/*<String, Set<ExportedPackage>>*/exports)
    {
        pw.println("Duplicate Exported Packages");
        pw.println("---------------------------");
        final List/*<String[]>*/lines = new ArrayList/*<String[]>*/();
        lines.add(new String[] { "Package", "Exports", "Imports" });

        for (Iterator/*<Entry<String, Set<ExportedPackage>>>*/entriesIter = exports.entrySet().iterator(); entriesIter.hasNext();)
        {
            Entry/*<String, Set<ExportedPackage>>*/exportEntry = (Entry) entriesIter.next();

            final Set/*<ExportedPackage>*/exportSet = (Set) exportEntry.getValue();
            if (exportSet.size() > 1)
            {
                String firstCol = (String) exportEntry.getKey();
                for (Iterator packageIter = exportSet.iterator(); packageIter.hasNext();)
                {
                    ExportedPackage exportedPackage = (ExportedPackage) packageIter.next();
                    final Bundle[] importers = exportedPackage.getImportingBundles();
                    final String secondCol = "version=" + exportedPackage.getVersion()
                        + ", Bundle " + exportedPackage.getExportingBundle();
                    if (importers != null && importers.length > 0)
                    {
                        boolean first = true;
                        for (int j = 0; j < importers.length; j++)
                        {
                            final Bundle bundle = importers[j];
                            if (first)
                            {
                                lines.add(new String[] { firstCol, secondCol,
                                        bundle.toString() });
                                first = false;
                            }
                            else
                            {
                                lines.add(new String[] { "", "", bundle.toString() });

                            }
                        }
                    }
                    else
                    {
                        lines.add(new String[] { firstCol, secondCol, "" });
                    }
                    firstCol = "";
                }
            }
        }
        int maxFirst = 0, maxSecond = 0;
        for (int i = 0; i < lines.size(); i++)
        {
            final String[] entry = (String[]) lines.get(i);
            if (entry[0].length() > maxFirst)
            {
                maxFirst = entry[0].length();
            }
            if (entry[1].length() > maxSecond)
            {
                maxSecond = entry[1].length();
            }
        }
        maxFirst += 2;
        maxSecond += 2;
        for (int i = 0; i < lines.size(); i++)
        {
            final String[] entry = (String[]) lines.get(i);
            padText(pw, entry[0], maxFirst);
            padText(pw, entry[1], maxSecond);
            pw.println(entry[2]);
        }
    }

    private static final void padText(final PrintWriter pw, final String text,
        final int length)
    {
        pw.print(text);
        final int padLength = length - text.length();
        for (int i = 0; i < padLength; i++)
        {
            pw.print(' ');
        }
    }

    /**
     * @see org.apache.felix.webconsole.ConfigurationPrinter#getTitle()
     */
    public String getTitle()
    {
        return "Duplicate Exports";
    }

}
TOP

Related Classes of org.apache.felix.webconsole.plugins.packageadmin.internal.WebConsolePrinter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.