Package com.onpositive.gae.tools.core

Source Code of com.onpositive.gae.tools.core.DebugServerLaunchManager

package com.onpositive.gae.tools.core;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationListener;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.ILaunchesListener2;
import org.eclipse.debug.core.model.IDebugTarget;
import org.eclipse.debug.core.model.IProcess;
import org.eclipse.debug.core.model.IThread;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.ui.IStartup;
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.IOConsole;

import com.onpositive.gae.tools.Activator;

public class DebugServerLaunchManager implements IStartup {

  private static final String ORG_ECLIPSE_JDT_LAUNCHING_PROJECT_ATTR = "org.eclipse.jdt.launching.PROJECT_ATTR";

  private final class AA implements ILaunchesListener2 {

    public void launchesTerminated(ILaunch[] launches) {
      for (ILaunch l : launches) {
        try {
          if (l.isTerminated()) {
            ILaunchConfiguration launchConfiguration = l
                .getLaunchConfiguration();
            Map<?, ?> attributes = launchConfiguration
                .getAttributes();
            Object object = attributes
                .get(ORG_ECLIPSE_JDT_LAUNCHING_PROJECT_ATTR);
            if (object instanceof String) {
              knownLaucnhes.remove(object);
            }
          }
        } catch (CoreException e) {
          Activator.log(e);
        }
      }
    }

    public void launchesAdded(ILaunch[] launches) {
      for (ILaunch l : launches) {
        try {
          ILaunchConfiguration ilc = l.getLaunchConfiguration();
          Map attr = ilc.getAttributes();
          return;
        } catch (CoreException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }

      }
    }

    public void launchesChanged(ILaunch[] launches) {
    }

    public void launchesRemoved(ILaunch[] launches) {

    }
  }

  private static final String COM_GOOGLE_GDT_ECLIPSE_SUITE_WEBAPP = "com.google.gdt.eclipse.suite.webapp";

  private static HashMap<String, Integer> knownLaucnhes = new HashMap<String, Integer>();

  public static int getPort(IJavaProject prj) {
    Integer integer = knownLaucnhes.get(prj.getElementName());
    if (integer != null) {
      return integer;
    }
    try {
      ILaunchConfiguration[] launchConfigurations = DebugPlugin
          .getDefault().getLaunchManager().getLaunchConfigurations();
      for (ILaunchConfiguration l : launchConfigurations) {
        if (l.getType().getIdentifier()
            .equals(COM_GOOGLE_GDT_ECLIPSE_SUITE_WEBAPP)) {
          Map<?, ?> attributes = l.getAttributes();
          String name = (String) attributes
              .get(ORG_ECLIPSE_JDT_LAUNCHING_PROJECT_ATTR);
          if (name != null) {
            if (name.equals(prj.getElementName())) {
              Object x = attributes
                  .get("com.google.gdt.eclipse.core.SERVER_PORT");

              if (x == null) {

                ILaunch[] p = DebugPlugin.getDefault()
                    .getLaunchManager().getLaunches();
                for (ILaunch s : p) {
                  String name3 = (String) s
                      .getLaunchConfiguration()
                      .getAttributes()
                      .get("org.eclipse.jdt.launching.PROJECT_ATTR");
                  if(name3 == null || prj == null){
                    continue;
                  }
                  if (name3.equals(prj.getElementName())) {
                    IDebugTarget[] debugTargets = s
                        .getDebugTargets();
                    for (IDebugTarget m : debugTargets) {
                      IThread[] threads= m.getThreads();
                      for(IThread t : threads){
                        String name2 = t.getName();
                        int lastIndexOf = name2.lastIndexOf(":");
                        if(lastIndexOf != -1){
                          x = name2.substring(lastIndexOf +1);
                        }
                      }
                    }
                  }
                }
                if (x == null) {
                  continue;
                }
              }
              int parseInt = Integer.parseInt(x.toString());
              URLConnection c;
              try {
                URL url = new URL("http://localhost:"
                    + parseInt + "/");
                URLConnection openConnection = url
                    .openConnection();
                openConnection.setConnectTimeout(100);
                c = openConnection;
                InputStream inputStream = c.getInputStream();
                inputStream.close();
                return parseInt;

              } catch (MalformedURLException e) {
              } catch (IOException e) {
              }

            }
          }
        }
      }
    } catch (CoreException e) {
      Activator.log(e);
    }
    return -1;
  }

  public static ILaunchConfiguration getLaunchConfiguration(IJavaProject pro)
      throws CoreException {
    ILaunchConfiguration[] launchConfigurations = DebugPlugin.getDefault()
        .getLaunchManager().getLaunchConfigurations();
    for (ILaunchConfiguration l : launchConfigurations) {
      String identifier = l.getType().getIdentifier();
      if (identifier.equals(COM_GOOGLE_GDT_ECLIPSE_SUITE_WEBAPP)) {
        Map<?, ?> attributes = l.getAttributes();
        String name = (String) attributes
            .get(ORG_ECLIPSE_JDT_LAUNCHING_PROJECT_ATTR);
        if (name != null) {
          if (name.equals(pro.getElementName())) {
            return l;
          }
        }
      }
    }
    return null;
  }

  public void earlyStartup() {
    VersionChecker.checkVersion();
    IConsoleManager consoleManager = ConsolePlugin.getDefault()
        .getConsoleManager();
    ILaunchManager launchManager = DebugPlugin.getDefault()
        .getLaunchManager();
    launchManager.addLaunchListener(new AA());

    consoleManager.addConsoleListener(new IConsoleListener() {

      public void consolesRemoved(IConsole[] consoles) {

      }

      public void consolesAdded(IConsole[] consoles) {
        for (IConsole c : consoles) {
          if (c instanceof IOConsole) {
            final IOConsole p = (IOConsole) c;
            Object attribute = p
                .getAttribute("org.eclipse.debug.ui.ATTR_CONSOLE_PROCESS");
            if (attribute != null) {
              IProcess pr = (IProcess) attribute;
              ILaunch launch = pr.getLaunch();
              ILaunchConfiguration launchConfiguration = launch
                  .getLaunchConfiguration();
              Map<?, ?> attributes = null;
              try {
                attributes = launchConfiguration
                    .getAttributes();
              } catch (CoreException e1) {
                Activator.log(e1);
                return;
              }
              final String project = (String) attributes
                  .get(ORG_ECLIPSE_JDT_LAUNCHING_PROJECT_ATTR);

              try {
                ILaunchConfigurationType type = launchConfiguration
                    .getType();
                String identifier = type.getIdentifier();
                if (identifier
                    .equals(COM_GOOGLE_GDT_ECLIPSE_SUITE_WEBAPP)) {
                  p.getDocument().addDocumentListener(
                      new IDocumentListener() {

                        public void documentChanged(
                            DocumentEvent event) {
                          String mark = "The server is running at ";
                          int indexOf = event
                              .getText().indexOf(
                                  mark);
                          if (indexOf != -1) {
                            String substring = event
                                .getText()
                                .substring(
                                    indexOf
                                        + mark.length());
                            int lastIndexOf = substring
                                .lastIndexOf(':');
                            if (lastIndexOf != -1) {
                              String substring2 = substring
                                  .substring(lastIndexOf + 1);
                              int lastIndexOf2 = substring2
                                  .indexOf('/');
                              if (lastIndexOf2 != -1) {
                                substring2 = substring2
                                    .substring(
                                        0,
                                        lastIndexOf2);
                              }
                             
                              int port = Integer
                                  .parseInt(substring2);
                              knownLaucnhes.put(
                                  project,
                                  port);
                              p.getDocument()
                                  .removeDocumentListener(
                                      this);
                            }
                          }

                        }

                        public void documentAboutToBeChanged(
                            DocumentEvent event) {

                        }
                      });

                }
              } catch (CoreException e) {
                Activator.log(e);
              }
            }
          }
        }
      }
    });

  }

}
TOP

Related Classes of com.onpositive.gae.tools.core.DebugServerLaunchManager

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.