<commit_before>package io.subutai.core.karaf.manager.impl;


import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.felix.service.command.CommandProcessor;
import org.apache.felix.service.command.CommandSession;

import io.subutai.common.mdc.SubutaiExecutors;
import io.subutai.common.security.utils.SafeCloseUtil;
import io.subutai.common.settings.Common;
import io.subutai.core.identity.api.IdentityManager;
import io.subutai.core.karaf.manager.api.KarafManager;


/**
 * Manages/Executes Karaf Commands
 */
@PermitAll
public class KarafManagerImpl implements KarafManager
{

    private static final Logger LOG = LoggerFactory.getLogger( KarafManagerImpl.class.getName() );


    private CommandProcessor commandProcessor = null;
    protected ExecutorService executor = SubutaiExecutors.newCachedThreadPool();


    /* ***********************************************
     */
    @RolesAllowed( {
            "Karaf-Server-Administration|Write", "Karaf-Server-Administration|Read", "System-Management|Write",
            "System-Management|Update"
    } )
    @Override
    public String executeShellCommand( final String commandStr )
    {
        String response = "";
        final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        final PrintStream printStream = new PrintStream( byteArrayOutputStream );
        final CommandSession commandSession = commandProcessor.createSession( System.in, printStream, printStream);


        //************************************************
        FutureTask<String> commandFuture = new FutureTask<>( new Callable<String>()
        {
            @Override
            public String call()
            {
                try
                {
                    commandSession.execute( commandStr );
                }
                catch ( Exception e )
                {
                    LOG.error( e.getMessage() );
                }

                printStream.flush();
                return byteArrayOutputStream.toString();
            }
        } );
        //************************************************

        try
        {
            executor.submit( commandFuture );

            do
            {
                response += commandFuture.get( Common.DEFAULT_EXECUTOR_REQUEST_TIMEOUT_SEC, TimeUnit.SECONDS );
            }
            while ( !commandFuture.isDone() );
        }
        catch ( Exception e )
        {
            response += "Command Timeout: ";

            response += byteArrayOutputStream.toString();
        }

        return response;
    }


    /* ***********************************************
     */
    @RolesAllowed( {
            "Karaf-Server-Administration|Write", "Karaf-Server-Administration|Read", "System-Management|Write",
            "System-Management|Update"
    } )
    @Override
    public String executeJMXCommand( final String commandStr )
    {
        String result = "No Result";

        JMXConnector connector = null;
        try
        {
            HashMap<String, String[]> environment = new HashMap<>();
            String[] credentials = new String[] { IdentityManager.ADMIN_USERNAME, IdentityManager.ADMIN_DEFAULT_PWD };
            environment.put( JMXConnector.CREDENTIALS, credentials );

            JMXServiceURL url = new JMXServiceURL( "service:jmx:rmi:///jndi/rmi://localhost:1099/karaf-root" );
            connector = JMXConnectorFactory.connect( url, environment );
            MBeanServerConnection mbeanServer = connector.getMBeanServerConnection();
            ObjectName systemMBean = new ObjectName( "org.apache.karaf:type=bundle,name=root" );
            mbeanServer.invoke( systemMBean, commandStr, null, null );
        }
        catch ( Exception e )
        {
            LOG.warn( e.getMessage() );
        }
        finally
        {
            SafeCloseUtil.close( connector );
        }

        return result;
    }


    public CommandProcessor getCommandProcessor()
    {
        return commandProcessor;
    }


    public void setCommandProcessor( final CommandProcessor commandProcessor )
    {
        this.commandProcessor = commandProcessor;
    }
}
<commit_msg>exposing error in Karaf UI Console<commit_after>package io.subutai.core.karaf.manager.impl;


import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.felix.service.command.CommandProcessor;
import org.apache.felix.service.command.CommandSession;

import io.subutai.common.mdc.SubutaiExecutors;
import io.subutai.common.security.utils.SafeCloseUtil;
import io.subutai.common.settings.Common;
import io.subutai.core.identity.api.IdentityManager;
import io.subutai.core.karaf.manager.api.KarafManager;


/**
 * Manages/Executes Karaf Commands
 */
@PermitAll
public class KarafManagerImpl implements KarafManager
{

    private static final Logger LOG = LoggerFactory.getLogger( KarafManagerImpl.class.getName() );


    private CommandProcessor commandProcessor = null;
    protected ExecutorService executor = SubutaiExecutors.newCachedThreadPool();


    /* ***********************************************
     */
    @RolesAllowed( {
            "Karaf-Server-Administration|Write", "Karaf-Server-Administration|Read", "System-Management|Write",
            "System-Management|Update"
    } )
    @Override
    public String executeShellCommand( final String commandStr )
    {
        String response = "";
        final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        final PrintStream printStream = new PrintStream( byteArrayOutputStream );
        final CommandSession commandSession = commandProcessor.createSession( System.in, printStream, System.err );


        //************************************************
        FutureTask<String> commandFuture = new FutureTask<>( new Callable<String>()
        {
            @Override
            public String call()
            {
                try
                {
                    commandSession.execute( commandStr );
                }
                catch ( Exception e )
                {
                    LOG.error( e.getMessage() );
                    printStream.println( e.toString() );
                }

                printStream.flush();
                return byteArrayOutputStream.toString();
            }
        } );
        //************************************************

        try
        {
            executor.submit( commandFuture );

            do
            {
                response += commandFuture.get( Common.DEFAULT_EXECUTOR_REQUEST_TIMEOUT_SEC, TimeUnit.SECONDS );
            }
            while ( !commandFuture.isDone() );
        }
        catch ( Exception e )
        {
            response += "Command Timeout: ";

            response += byteArrayOutputStream.toString();
        }

        return response;
    }


    /* ***********************************************
     */
    @RolesAllowed( {
            "Karaf-Server-Administration|Write", "Karaf-Server-Administration|Read", "System-Management|Write",
            "System-Management|Update"
    } )
    @Override
    public String executeJMXCommand( final String commandStr )
    {
        String result = "No Result";

        JMXConnector connector = null;
        try
        {
            HashMap<String, String[]> environment = new HashMap<>();
            String[] credentials = new String[] { IdentityManager.ADMIN_USERNAME, IdentityManager.ADMIN_DEFAULT_PWD };
            environment.put( JMXConnector.CREDENTIALS, credentials );

            JMXServiceURL url = new JMXServiceURL( "service:jmx:rmi:///jndi/rmi://localhost:1099/karaf-root" );
            connector = JMXConnectorFactory.connect( url, environment );
            MBeanServerConnection mbeanServer = connector.getMBeanServerConnection();
            ObjectName systemMBean = new ObjectName( "org.apache.karaf:type=bundle,name=root" );
            mbeanServer.invoke( systemMBean, commandStr, null, null );
        }
        catch ( Exception e )
        {
            LOG.warn( e.getMessage() );
        }
        finally
        {
            SafeCloseUtil.close( connector );
        }

        return result;
    }


    public CommandProcessor getCommandProcessor()
    {
        return commandProcessor;
    }


    public void setCommandProcessor( final CommandProcessor commandProcessor )
    {
        this.commandProcessor = commandProcessor;
    }
}

{'fraction_non_alphanumeric': 0.1295369211514393, 'fraction_numerical': 0.0012515644555694619, 'mean_word_length': 2.5927313600599478, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1925492', 'n_tokens_mistral': 2488, 'n_tokens_neox': 2414, 'n_words': 455}