<commit_before>/*
 * 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.sling.installer.core.impl.console;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

import javax.servlet.GenericServlet;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.sling.installer.api.InstallableResource;
import org.apache.sling.installer.api.info.InfoProvider;
import org.apache.sling.installer.api.info.InstallationState;
import org.apache.sling.installer.api.info.Resource;
import org.apache.sling.installer.api.info.ResourceGroup;
import org.apache.sling.installer.api.tasks.RegisteredResource;
import org.apache.sling.installer.api.tasks.ResourceState;

@SuppressWarnings("serial")
public class OsgiInstallerWebConsolePlugin extends GenericServlet {

    private final InfoProvider installer;

    public OsgiInstallerWebConsolePlugin(final InfoProvider installer) {
        this.installer = installer;
    }

    private String getType(final RegisteredResource rsrc) {
        final String type = rsrc.getType();
        if ( type.equals(InstallableResource.TYPE_BUNDLE) ) {
            return "Bundles";
        } else if ( type.equals(InstallableResource.TYPE_CONFIG) ) {
            return "Configurations";
        } else if ( type.equals(InstallableResource.TYPE_FILE) ) {
            return "Files";
        } else if ( type.equals(InstallableResource.TYPE_PROPERTIES) ) {
            return "Properties";
        }
        return type;
    }

    private String getEntityId(final RegisteredResource rsrc, String alias) {
        String id = rsrc.getEntityId();
        final int pos = id.indexOf(':');
        if ( pos != -1 ) {
            id = id.substring(pos + 1);
        }
        return (alias == null ? id : id + '\n' + alias);
    }

    private String getURL(final Resource rsrc) {
        if ( rsrc.getVersion() != null ) {
            return rsrc.getURL() + " (" + rsrc.getVersion() + ")";
        }
        return rsrc.getURL();
    }

    private String getInfo(final RegisteredResource rsrc) {
        return rsrc.getDigest() + '/' + String.valueOf(rsrc.getPriority());
    }

    /** Default date format used. */
    private final DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSS yyyy-MMM-dd");

    /**
     * Format a date
     */
    private synchronized String formatDate(final long time) {
        if ( time == -1 ) {
            return "-";
        }
        final Date d = new Date(time);
        return dateFormat.format(d);
    }

    @Override
    public void service(final ServletRequest req, final ServletResponse res)
    throws IOException {
        final PrintWriter pw = res.getWriter();

        pw.print("<p class='statline ui-state-highlight'>Apache Sling OSGi Installer</p>");
        final InstallationState state = this.installer.getInstallationState();

        String rt = null;
        for(final ResourceGroup group : state.getActiveResources()) {
            final Resource toActivate = group.getResources().get(0);
            if ( !toActivate.getType().equals(rt) ) {
                if ( rt != null ) {
                    pw.println("</tbody></table>");
                }
                pw.println("<div class='ui-widget-header ui-corner-top buttonGroup' style='height: 15px;'>");
                pw.printf("<span style='float: left; margin-left: 1em;'>Active Resources - %s</span>", getType(toActivate));<commit_msg>Print status msg if no resources are registered<commit_after>/*
 * 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.sling.installer.core.impl.console;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

import javax.servlet.GenericServlet;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.sling.installer.api.InstallableResource;
import org.apache.sling.installer.api.info.InfoProvider;
import org.apache.sling.installer.api.info.InstallationState;
import org.apache.sling.installer.api.info.Resource;
import org.apache.sling.installer.api.info.ResourceGroup;
import org.apache.sling.installer.api.tasks.RegisteredResource;
import org.apache.sling.installer.api.tasks.ResourceState;

@SuppressWarnings("serial")
public class OsgiInstallerWebConsolePlugin extends GenericServlet {

    private final InfoProvider installer;

    public OsgiInstallerWebConsolePlugin(final InfoProvider installer) {
        this.installer = installer;
    }

    private String getType(final RegisteredResource rsrc) {
        final String type = rsrc.getType();
        if ( type.equals(InstallableResource.TYPE_BUNDLE) ) {
            return "Bundles";
        } else if ( type.equals(InstallableResource.TYPE_CONFIG) ) {
            return "Configurations";
        } else if ( type.equals(InstallableResource.TYPE_FILE) ) {
            return "Files";
        } else if ( type.equals(InstallableResource.TYPE_PROPERTIES) ) {
            return "Properties";
        }
        return type;
    }

    private String getEntityId(final RegisteredResource rsrc, String alias) {
        String id = rsrc.getEntityId();
        final int pos = id.indexOf(':');
        if ( pos != -1 ) {
            id = id.substring(pos + 1);
        }
        return (alias == null ? id : id + '\n' + alias);
    }

    private String getURL(final Resource rsrc) {
        if ( rsrc.getVersion() != null ) {
            return rsrc.getURL() + " (" + rsrc.getVersion() + ")";
        }
        return rsrc.getURL();
    }

    private String getInfo(final RegisteredResource rsrc) {
        return rsrc.getDigest() + '/' + String.valueOf(rsrc.getPriority());
    }

    /** Default date format used. */
    private final DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSS yyyy-MMM-dd");

    /**
     * Format a date
     */
    private synchronized String formatDate(final long time) {
        if ( time == -1 ) {
            return "-";
        }
        final Date d = new Date(time);
        return dateFormat.format(d);
    }

    @Override
    public void service(final ServletRequest req, final ServletResponse res)
    throws IOException {
        final PrintWriter pw = res.getWriter();

        final InstallationState state = this.installer.getInstallationState();
        pw.print("<p class='statline ui-state-highlight'>Apache Sling OSGi Installer");
        if ( state.getActiveResources().size() == 0 && state.getInstalledResources().size() == 0 && state.getUntransformedResources().size() == 0 ) {
            pw.print(" - no resources registered.");
        }
        pw.print("</p>");

        String rt = null;
        for(final ResourceGroup group : state.getActiveResources()) {
            final Resource toActivate = group.getResources().get(0);
            if ( !toActivate.getType().equals(rt) ) {
                if ( rt != null ) {
                    pw.println("</tbody></table>");
                }
                pw.println("<div class='ui-widget-header ui-corner-top buttonGroup' style='height: 15px;'>");
                pw.printf("<span style='float: left; margin-left: 1em;'>Active Resources - %s</span>", getType(toActivate));
{'fraction_non_alphanumeric': 0.10948169707824919, 'fraction_numerical': 0.0027986118885033025, 'mean_word_length': 2.932218309859155, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7156419', 'n_tokens_mistral': 2495, 'n_tokens_neox': 2432, 'n_words': 678}