Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.internal.misc;
 
 
 import java.io.Writer;
 import java.util.Date;
 
 
 
 
 public class ConfigurationRender extends BaseWebConsolePlugin
 {
 
     public static final String LABEL = "config";
 
     public static final String TITLE = "Configuration Status";
 
     private static final String[] CSS_REFS =
         { "res/ui/configurationrender.css" };

    
Formatter pattern to generate a relative path for the generation of the plain text or zip file representation of the status. The file name consists of a base name and the current time of status generation.
 
     private static final SimpleDateFormat FILE_NAME_FORMAT = new SimpleDateFormat"'" + 
         + "/configuration-status-'yyyyMMdd'-'HHmmZ" );

    
Formatter pattern to render the current time of status generation.
 
     private static final DateFormat DISPLAY_DATE_FORMAT = SimpleDateFormat.getDateTimeInstance.,
         .. );
 
     private ServiceTracker cfgPrinterTracker;
 
     private int cfgPrinterTrackerCount;
 
     private SortedMap configurationPrinters = new TreeMap();
 
 
     public String getTitle()
     {
         return ;
     }
 
 
     public String getLabel()
     {
         return ;
     }
 
 
     protected void doGetHttpServletRequest requestHttpServletResponse response ) throws ServletException,
         IOException
     {
         if ( request.getPathInfo().endsWith".txt" ) )
         {
             response.setContentType"text/plain; charset=utf-8" );
             ConfigurationWriter pw = new PlainTextConfigurationWriterresponse.getWriter() );
            printConfigurationStatuspw );
            pw.flush();
        }
        else if ( request.getPathInfo().endsWith".zip" ) )
        {
            String type = getServletContext().getMimeTyperequest.getPathInfo() );
            if ( type == null )
            {
                type = "application/x-zip";
            }
            response.setContentTypetype );
            ZipOutputStream zip = new ZipOutputStreamresponse.getOutputStream() );
            zip.setLevel( 9 );
            zip.setMethod. );
            ConfigurationWriter pw = new ZipConfigurationWriterzip );
            printConfigurationStatuspw );
            pw.flush();
            zip.finish();
        }
        else
        {
            super.doGetrequestresponse );
        }
    }
    protected String[] getCssReferences()
    {
        return ;
    }
    protected void renderContentHttpServletRequest requestHttpServletResponse response ) throws IOException
    {
        ConfigurationWriter pw = new HtmlConfigurationWriterresponse.getWriter() );
        String appRoot = ( String ) request.getAttribute. );
        Util.scriptpwappRoot"tw-1.1.js" );
        Util.startScriptpw );
        pw.println"    $(document).ready(function(){" );
        //                   set up the tabs (still hidden)
        pw.println"        $('#cfgprttabs').tabworld({speed:0});" );
        //                   show the finished tabs
        pw.println"        $('#divcfgprttabs').removeClass('divcfgprttabshidden');" );
        //                   hide the "please wait" message
        pw.println"        $('#divcfgprttabswait').addClass('divcfgprttabshidden');" );
        pw.println"    });" );
        Util.endScriptpw );
        final Date currentTime = new Date();
        synchronized (  )
        {
            pw.println"<p>Date: " + .formatcurrentTime ) + "</p>" );
        }
        synchronized (  )
        {
            String fileName = .formatcurrentTime );
            pw.println"<p>Download as <a href='" + fileName + ".txt'>[Single File]</a> or as <a href='" + fileName
                + ".zip'>[ZIP]</a></p>" );
        }
        // display some information while the data is loading
        pw.println"<div id='divcfgprttabswait'>Loading status information. Please wait....</div>" );
        // load the data (hidden to begin with)
        pw.println"<div id='divcfgprttabs' class='divcfgprttabshidden'>" );
        pw.println"<ul id='cfgprttabs'>" );
        printConfigurationStatuspw );
        pw.println"</ul>" );
        pw.println"</div>" );
        pw.flush();
    }
    private void printConfigurationStatusConfigurationWriter pw )
    {
        this.printSystemPropertiespw );
        this.printThreadspw );
        for ( Iterator cpi = getConfigurationPrinters().iterator(); cpi.hasNext(); )
        {
            printConfigurationPrinterpw, ( ConfigurationPrinter ) cpi.next() );
        }
    }
    {
        if (  == null )
        {
             = new ServiceTrackergetBundleContext(), .null );
            .open();
             = -1;
        }
        {
            SortedMap cp = new TreeMap();
            Object[] services = .getServices();
            if ( services != null )
            {
                for ( int i = 0; i < services.lengthi++ )
                {
                    Object srv = services[i];
                    ConfigurationPrinter cfgPrinter = ( ConfigurationPrinter ) srv;
                    cp.putcfgPrinter.getTitle(), cfgPrinter );
                }
            }
             = cp;
        }
        return .values();
    }
    private void printSystemPropertiesConfigurationWriter pw )
    {
        pw.title"System properties" );
        Properties props = System.getProperties();
        SortedSet keys = new TreeSetprops.keySet() );
        for ( Iterator ki = keys.iterator(); ki.hasNext(); )
        {
            Object key = ki.next();
            infoLinepwnull, ( String ) keyprops.getkey ) );
        }
        pw.end();
    }
    // This is Sling stuff, we comment it out for now
    //    private void printRawFrameworkProperties(PrintWriter pw) {
    //        pw.println("*** Raw Framework properties:");
    //
    //        File file = new File(getBundleContext().getProperty("sling.home"),
    //            "sling.properties");
    //        if (file.exists()) {
    //            Properties props = new Properties();
    //            InputStream ins = null;
    //            try {
    //                ins = new FileInputStream(file);
    //                props.load(ins);
    //            } catch (IOException ioe) {
    //                // handle or ignore
    //            } finally {
    //                IOUtils.closeQuietly(ins);
    //            }
    //
    //            SortedSet keys = new TreeSet(props.keySet());
    //            for (Iterator ki = keys.iterator(); ki.hasNext();) {
    //                Object key = ki.next();
    //                infoLine(pw, null, (String) key, props.get(key));
    //            }
    //
    //        } else {
    //            pw.println("  No Framework properties in " + file);
    //        }
    //
    //        pw.println();
    //    }
    {
        pw.title(  cp.getTitle() );
        cp.printConfigurationpw );
        pw.end();
    }
    public static void infoLinePrintWriter pwString indentString labelObject value )
    {
        if ( indent != null )
        {
            pw.printindent );
        }
        if ( label != null )
        {
            pw.printlabel );
            pw.print" = " );
        }
        pw.printasStringvalue ) );
        pw.println();
    }
    private static String asStringfinal Object value )
    {
        if ( value == null )
        {
            return "n/a";
        }
        else if ( value.getClass().isArray() )
        {
            StringBuffer dest = new StringBuffer();
            Object[] values = ( Object[] ) value;
            for ( int j = 0; j < values.lengthj++ )
            {
                if ( j > 0 )
                    dest.append", " );
                dest.appendvalues[j] );
            }
            return dest.toString();
        }
        else
        {
            return value.toString();
        }
    }
    private void printThreadsConfigurationWriter pw )
    {
        // first get the root thread group
        ThreadGroup rootGroup = Thread.currentThread().getThreadGroup();
        while ( rootGroup.getParent() != null )
        {
            rootGroup = rootGroup.getParent();
        }
        pw.title(  "Threads" );
        printThreadGrouppwrootGroup );
        int numGroups = rootGroup.activeGroupCount();
        ThreadGroup[] groups = new ThreadGroup[2 * numGroups];
        rootGroup.enumerategroups );
        for ( int i = 0; i < groups.lengthi++ )
        {
            printThreadGrouppwgroups[i] );
        }
        pw.end();
    }
    private void printThreadGroupPrintWriter pwThreadGroup group )
    {
        if ( group != null )
        {
            StringBuffer info = new StringBuffer();
            info.append("ThreadGroup ").append(group.getName());
            info.append" [" );
            info.append"maxprio=" ).appendgroup.getMaxPriority() );
            info.append", parent=" );
            if ( group.getParent() != null )
            {
                info.appendgroup.getParent().getName() );
            }
            else
            {
                info.append'-' );
            }
            info.append", isDaemon=" ).appendgroup.isDaemon() );
            info.append", isDestroyed=" ).appendgroup.isDestroyed() );
            info.append']' );
            infoLinepwnullnullinfo.toString() );
            int numThreads = group.activeCount();
            Thread[] threads = new Thread[numThreads * 2];
            group.enumeratethreadsfalse );
            for ( int i = 0; i < threads.lengthi++ )
            {
                printThreadpwthreads[i] );
            }
            pw.println();
        }
    }
    private void printThreadPrintWriter pwThread thread )
    {
        if ( thread != null )
        {
            StringBuffer info = new StringBuffer();
            info.append("Thread ").appendthread.getName() );
            info.append" [" );
            info.append"priority=" ).appendthread.getPriority() );
            info.append", alive=" ).appendthread.isAlive() );
            info.append", daemon=" ).appendthread.isDaemon() );
            info.append", interrupted=" ).appendthread.isInterrupted() );
            info.append", loader=" ).appendthread.getContextClassLoader() );
            info.append']' );
            infoLinepw"  "nullinfo.toString() );
        }
    }
    private abstract static class ConfigurationWriter extends PrintWriter
    {
        ConfigurationWriterWriter delegatee )
        {
            superdelegatee );
        }
        abstract void titleString title );
        abstract void end();
    }
    private static class HtmlConfigurationWriter extends ConfigurationWriter
    {
        // whether or not to filter "<" signs in the output
        private boolean doFilter;
        HtmlConfigurationWriterWriter delegatee )
        {
            superdelegatee );
        }
        public void titleString title )
        {
            println"<li>" );
            printlntitle );
            println"<q>" );
             = true;
        }
        public void end()
        {
             = false;
            println"</q>" );
            println"</li>" );
        }
        // IE has an issue with white-space:pre in our case so, we write
        // <br/> instead of [CR]LF to get the line break. This also works
        // in other browsers.
        public void println()
        {
            if (  )
            {
                super.write"<br/>", 0, 5 );
            }
            else
            {
                super.println();
            }
        }
        // write the character unmodified unless filtering is enabled and
        // the character is a "<" in which case &lt; is written
        public void writefinal int character )
        {
            if (  && character == '<' )
            {
                super.write"&lt;" );
            }
            else
            {
                super.writecharacter );
            }
        }
        // write the characters unmodified unless filtering is enabled in
        // which case the writeFiltered(String) method is called for filtering
        public void writefinal char[] charsfinal int offfinal int len )
        {
            if (  )
            {
                writeFilterednew Stringcharsofflen ) );
            }
            else
            {
                super.writecharsofflen );
            }
        }
        // write the string unmodified unless filtering is enabled in
        // which case the writeFiltered(String) method is called for filtering
        public void writefinal String stringfinal int offfinal int len )
        {
            if (  )
            {
                writeFilteredstring.substringofflen ) );
            }
            else
            {
                super.writestringofflen );
            }
        }
        // helper method filter the string for "<" before writing
        private void writeFilteredfinal String string )
        {
            if ( string.indexOf'<' ) >= 0 )
            {
                super.writestring.replaceAll"<""&lt;" ) );
            }
            else
            {
                // no filtering needed write as is
                super.writestring, 0, string.length() );
            }
        }
    }
    private static class PlainTextConfigurationWriter extends ConfigurationWriter
    {
        PlainTextConfigurationWriterWriter delegatee )
        {
            superdelegatee );
        }
        public void titleString title )
        {
            print"*** " );
            printtitle );
            println":" );
        }
        public void end()
        {
            println();
        }
    }
    private static class ZipConfigurationWriter extends ConfigurationWriter
    {
        private final ZipOutputStream zip;
        private int counter;
        ZipConfigurationWriterZipOutputStream zip )
        {
            supernew OutputStreamWriterzip ) );
            this. = zip;
        }
        public void titleString title )
        {
            String name = MessageFormat.format"{0,number,000}-{1}.txt"new Object[]
                { new Integer ), title } );
            ++;
            ZipEntry entry = new ZipEntryname );
            try
            {
                .putNextEntryentry );
            }
            catch ( IOException ioe )
            {
                // should handle
            }
        }
        public void end()
        {
            flush();
            try
            {
                .closeEntry();
            }
            catch ( IOException ioe )
            {
                // should handle
            }
        }
    }
New to GrepCode? Check out our FAQ X