Package org.apache.sling.ide.eclipse.ui.internal.console

Source Code of org.apache.sling.ide.eclipse.ui.internal.console.SlingConsoleEventListener

/*
* 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.ide.eclipse.ui.internal.console;

import java.io.IOException;
import java.io.PrintStream;
import java.text.DateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;

import org.apache.commons.io.IOUtils;
import org.apache.sling.ide.eclipse.ui.console.SlingConsoleFactory;
import org.apache.sling.ide.eclipse.ui.internal.Activator;
import org.apache.sling.ide.transport.CommandExecutionProperties;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleListener;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;

public class SlingConsoleEventListener implements EventHandler {

    private final Object sync = new Object();

    private final Set<Event> delayedEvents = Collections.newSetFromMap(new LinkedHashMap<Event, Boolean>() {
        private static final long serialVersionUID = 1L;
        protected boolean removeEldestEntry(java.util.Map.Entry<Event, Boolean> eldest) {
            return size() >= 500;
        };
    });

    private MessageConsole slingConsole;
    private IConsoleListener listener;

    @Override
    public void handleEvent(Event event) {

        synchronized (sync) {

            initSlingConsole();

            if (slingConsole != null) {
                logEvent(event, slingConsole);
                return;
            }

            delayedEvents.add(event);

            final IConsoleManager consoleManager = ConsolePlugin.getDefault().getConsoleManager();

            if (listener == null) {
                listener = new IConsoleListener() {

                    @Override
                    public void consolesRemoved(IConsole[] consoles) {
                        synchronized (sync) {
                            for (IConsole console : consoles) {
                                if (console.equals(slingConsole)) {
                                    slingConsole = null;
                                }
                            }
                        }
                    }

                    @Override
                    public void consolesAdded(IConsole[] consoles) {
                        synchronized (sync) {
                            for (IConsole console : consoles) {
                                if (console.getType().equals(SlingConsoleFactory.CONSOLE_TYPE_SLING)) {
                                    slingConsole = (MessageConsole) console;
                                    synchronized (delayedEvents) {
                                        for (Iterator<Event> it = delayedEvents.iterator(); it.hasNext();) {
                                            logEvent(it.next(), slingConsole);
                                            it.remove();
                                        }
                                    }
                                    consoleManager.removeConsoleListener(listener);
                                    listener = null;
                                    break;
                                }
                            }
                        }
                    }
                };

                consoleManager.addConsoleListener(listener);
            }
        }
    }

    private void initSlingConsole() {
        if (slingConsole == null) {
            final IConsoleManager consoleManager = ConsolePlugin.getDefault().getConsoleManager();
            for (IConsole console : consoleManager.getConsoles()) {
                if (console.getType().equals(SlingConsoleFactory.CONSOLE_TYPE_SLING)) {
                    slingConsole = (MessageConsole) console;
                    break;
                }
            }
        }
    }

    private void logEvent(Event event, MessageConsole console) {

        MessageConsoleStream messageStream = console.newMessageStream();
        try {

            Long start = (Long) event.getProperty(CommandExecutionProperties.TIMESTAMP_START);
            Long end = (Long) event.getProperty(CommandExecutionProperties.TIMESTAMP_END);
            String type = (String) event.getProperty(CommandExecutionProperties.ACTION_TYPE);
            String target = (String) event.getProperty(CommandExecutionProperties.ACTION_TARGET);
            String result = (String) event.getProperty(CommandExecutionProperties.RESULT_TEXT);
            Throwable t = (Throwable) event.getProperty(CommandExecutionProperties.RESULT_THROWABLE);

            StringBuilder message = new StringBuilder();
            DateFormat format = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
            message.append("[").append(format.format(new Date(start))).append("] ").append(type).append(" -> ")
                    .append(target);
            message.append(" : ").append(result).append(" (").append(end - start).append(" ms)").append('\n');

            messageStream.write(message.toString());
            if (t != null) {
                t.printStackTrace(new PrintStream(messageStream));
            }
        } catch (IOException e) {
            Activator.getDefault().getPluginLogger().warn("Failed writing to the console", e);
        } finally {
            IOUtils.closeQuietly(messageStream);
        }
    }

}
TOP

Related Classes of org.apache.sling.ide.eclipse.ui.internal.console.SlingConsoleEventListener

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.