// Path: jetty-console-core/src/main/java/org/simplericity/jettyconsole/io/JTextAreaOutputStream.java
// public class JTextAreaOutputStream extends OutputStream {
// 
//     private JTextArea text;
// 
//     public JTextAreaOutputStream(JTextArea text) {
//         this.text = text;
//     }
// 
//     public void write(int i) throws IOException {
//         write(new byte[] {(byte)i}, 0,1);
//     }
// 
//     public void write(byte[] bytes) throws IOException {
//         write(bytes, 0, bytes.length);
//     }
// 
//     public void write(final byte[] bytes, final int i, final int i1) throws IOException {
// 
//         if(bytes != null && i1 != 0) {
//             final String s = new String(bytes, i, i1);
// 
//             SwingUtilities.invokeLater(new Runnable() {
//                 public void run() {
//                     text.append(s);
//                     text.setCaretPosition(text.getDocument().getLength());
//                     int size = 100000;
//                     int maxOverflow= 500;
//                     int overflow = text.getDocument().getLength() - size;
//                     if (overflow >= maxOverflow) {
//                         text.replaceRange("", 0, overflow);
//                     }
//                 }
//             });
//         }
//     }
// }
// 
// Path: jetty-console-core/src/main/java/org/simplericity/jettyconsole/io/MultiOutputStream.java
// public class MultiOutputStream extends OutputStream {
// 
//     private List outputStreams = new ArrayList();
// 
//     public MultiOutputStream(OutputStream stream) {
//         outputStreams.add(stream);
//     }
// 
//     public void addOutputStream(OutputStream stream) {
//         outputStreams.add(stream);
//     }
// 
//     public void write(int i) throws IOException {
//         for (int j = 0; j < outputStreams.size(); j++) {
//             OutputStream stream = (OutputStream) outputStreams.get(j);
//             stream.write(i);
//         }
//     }
// 
//     public void write(byte[] bytes) throws IOException {
//         for (int i = 0; i < outputStreams.size(); i++) {
//             OutputStream stream = (OutputStream) outputStreams.get(i);
//             stream.write(bytes);
//         }
//     }
// 
//     public void write(byte[] bytes, int i, int i1) throws IOException {
//         for (int j = 0; j < outputStreams.size(); j++) {
//             OutputStream stream = (OutputStream) outputStreams.get(j);
//             stream.write(bytes, i, i1);
//         }
//     }
// 
//     public void flush() throws IOException {
//         for (int i = 0; i < outputStreams.size(); i++) {
//             OutputStream stream = (OutputStream) outputStreams.get(i);
//             stream.flush();
//         }
//     }
// 
//     public void close() throws IOException {
//         for (int i = 0; i < outputStreams.size(); i++) {
//             OutputStream stream = (OutputStream) outputStreams.get(i);
//             stream.close();
//         }
//     }
// }
// 
// Path: jetty-console-api/src/main/java/org/simplericity/jettyconsole/api/StartOption.java
// public interface StartOption {
// 
//     String getName();
// 
//     String validate(String arg);
// 
//     String validate();
// }
// 
// Path: jetty-console-api/src/main/java/org/simplericity/jettyconsole/api/Configuration.java
// public interface Configuration {
// 
//     boolean isHeadless();
// 
//     void setHeadless(boolean headless);
// 
//     String getContextPath();
// 
//     void setContextPath(String contextPath);
// }
// 
// Path: jetty-console-api/src/main/java/org/simplericity/jettyconsole/api/JettyConsolePlugin.java
// public interface JettyConsolePlugin {
//     
//      List<StartOption> getStartOptions();
//      void beforeStart(WebAppContext context);
//      void beforeStop(WebAppContext context);
// 
//      void customizeServer(Server server);
// 
//      void customizeConnector(ServerConnector connector);
// 
//      void bootstrap();
// 
//      void configureConsole(Configuration configuration);
// }


import org.simplericity.jettyconsole.io.JTextAreaOutputStream;
import org.simplericity.jettyconsole.io.MultiOutputStream;
import org.simplericity.jettyconsole.api.StartOption;
import org.simplericity.jettyconsole.api.Configuration;
import org.simplericity.jettyconsole.api.JettyConsolePlugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.kantega.jexmec.PluginManager;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.util.Properties;
/*
 * Copyright 2015 Eirik Bjørsnøs
 *
 * Licensed 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.simplericity.jettyconsole;



/**
 * A graphical console for starting and stopping a webapp in Jetty.
 */
public class JettyConsole {
    private AbstractAction stopAction;
    private AbstractAction startAction;
    private JScrollPane scroll;

    private MultiOutputStream multiOut;
    private MultiOutputStream multiErr;
    private JButton startStop;
    private Logger log = LoggerFactory.getLogger(getClass());
    private JFrame frame;
    private String name;
    private final Properties settings;
    private final JettyManager jettyManager;
    private final PluginManager<JettyConsolePlugin> pluginManager;

    public JettyConsole(Properties settings, JettyManager jettyManager, PluginManager<JettyConsolePlugin> pluginManager) {
        this.settings = settings;
        this.jettyManager = jettyManager;
        this.pluginManager = pluginManager;
    }

// Path: jetty-console-core/src/main/java/org/simplericity/jettyconsole/io/JTextAreaOutputStream.java
// public class JTextAreaOutputStream extends OutputStream {
// 
//     private JTextArea text;
// 
//     public JTextAreaOutputStream(JTextArea text) {
//         this.text = text;
//     }
// 
//     public void write(int i) throws IOException {
//         write(new byte[] {(byte)i}, 0,1);
//     }
// 
//     public void write(byte[] bytes) throws IOException {
//         write(bytes, 0, bytes.length);
//     }
// 
//     public void write(final byte[] bytes, final int i, final int i1) throws IOException {
// 
//         if(bytes != null && i1 != 0) {
//             final String s = new String(bytes, i, i1);
// 
//             SwingUtilities.invokeLater(new Runnable() {
//                 public void run() {
//                     text.append(s);
//                     text.setCaretPosition(text.getDocument().getLength());
//                     int size = 100000;
//                     int maxOverflow= 500;
//                     int overflow = text.getDocument().getLength() - size;
//                     if (overflow >= maxOverflow) {
//                         text.replaceRange("", 0, overflow);
//                     }
//                 }
//             });
//         }
//     }
// }
// 
// Path: jetty-console-core/src/main/java/org/simplericity/jettyconsole/io/MultiOutputStream.java
// public class MultiOutputStream extends OutputStream {
// 
//     private List outputStreams = new ArrayList();
// 
//     public MultiOutputStream(OutputStream stream) {
//         outputStreams.add(stream);
//     }
// 
//     public void addOutputStream(OutputStream stream) {
//         outputStreams.add(stream);
//     }
// 
//     public void write(int i) throws IOException {
//         for (int j = 0; j < outputStreams.size(); j++) {
//             OutputStream stream = (OutputStream) outputStreams.get(j);
//             stream.write(i);
//         }
//     }
// 
//     public void write(byte[] bytes) throws IOException {
//         for (int i = 0; i < outputStreams.size(); i++) {
//             OutputStream stream = (OutputStream) outputStreams.get(i);
//             stream.write(bytes);
//         }
//     }
// 
//     public void write(byte[] bytes, int i, int i1) throws IOException {
//         for (int j = 0; j < outputStreams.size(); j++) {
//             OutputStream stream = (OutputStream) outputStreams.get(j);
//             stream.write(bytes, i, i1);
//         }
//     }
// 
//     public void flush() throws IOException {
//         for (int i = 0; i < outputStreams.size(); i++) {
//             OutputStream stream = (OutputStream) outputStreams.get(i);
//             stream.flush();
//         }
//     }
// 
//     public void close() throws IOException {
//         for (int i = 0; i < outputStreams.size(); i++) {
//             OutputStream stream = (OutputStream) outputStreams.get(i);
//             stream.close();
//         }
//     }
// }
// 
// Path: jetty-console-api/src/main/java/org/simplericity/jettyconsole/api/StartOption.java
// public interface StartOption {
// 
//     String getName();
// 
//     String validate(String arg);
// 
//     String validate();
// }
// 
// Path: jetty-console-api/src/main/java/org/simplericity/jettyconsole/api/Configuration.java
// public interface Configuration {
// 
//     boolean isHeadless();
// 
//     void setHeadless(boolean headless);
// 
//     String getContextPath();
// 
//     void setContextPath(String contextPath);
// }
// 
// Path: jetty-console-api/src/main/java/org/simplericity/jettyconsole/api/JettyConsolePlugin.java
// public interface JettyConsolePlugin {
//     
//      List<StartOption> getStartOptions();
//      void beforeStart(WebAppContext context);
//      void beforeStop(WebAppContext context);
// 
//      void customizeServer(Server server);
// 
//      void customizeConnector(ServerConnector connector);
// 
//      void bootstrap();
// 
//      void configureConsole(Configuration configuration);
// }


// Path: jetty-console-core/src/main/java/org/simplericity/jettyconsole/JettyConsole.java
import org.simplericity.jettyconsole.io.JTextAreaOutputStream;
import org.simplericity.jettyconsole.io.MultiOutputStream;
import org.simplericity.jettyconsole.api.StartOption;
import org.simplericity.jettyconsole.api.Configuration;
import org.simplericity.jettyconsole.api.JettyConsolePlugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.kantega.jexmec.PluginManager;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.util.Properties;

/*
 * Copyright 2015 Eirik Bjørsnøs
 *
 * Licensed 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.simplericity.jettyconsole;



/**
 * A graphical console for starting and stopping a webapp in Jetty.
 */
public class JettyConsole {
    private AbstractAction stopAction;
    private AbstractAction startAction;
    private JScrollPane scroll;

    private MultiOutputStream multiOut;
    private MultiOutputStream multiErr;
    private JButton startStop;
    private Logger log = LoggerFactory.getLogger(getClass());
    private JFrame frame;
    private String name;
    private final Properties settings;
    private final JettyManager jettyManager;
    private final PluginManager<JettyConsolePlugin> pluginManager;

    public JettyConsole(Properties settings, JettyManager jettyManager, PluginManager<JettyConsolePlugin> pluginManager) {
        this.settings = settings;
        this.jettyManager = jettyManager;
        this.pluginManager = pluginManager;
    }

    public void init(final Configuration configuration) throws Exception {