[logback-dev] svn commit: r1462 - in logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse: reciever views

noreply.seb at qos.ch noreply.seb at qos.ch
Mon Mar 26 21:45:42 CEST 2007


Author: seb
Date: Mon Mar 26 21:45:42 2007
New Revision: 1462

Modified:
   logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/reciever/SimpleSocketServer.java
   logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/views/LogbackView.java

Log:
Displaying message in case of port already in use

Modified: logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/reciever/SimpleSocketServer.java
==============================================================================
--- logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/reciever/SimpleSocketServer.java	(original)
+++ logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/reciever/SimpleSocketServer.java	Mon Mar 26 21:45:42 2007
@@ -3,6 +3,7 @@
 import java.io.IOException;
 import java.net.ServerSocket;
 import java.net.Socket;
+import java.net.UnknownHostException;
 
 import org.eclipse.ui.IMemento;
 
@@ -78,7 +79,15 @@
     if (newPort != 0) {
       this.port = newPort;
     }
-
+  }
+  
+  public boolean isPortFree() {
+    try {
+      Socket s = new Socket("localhost", port);
+      return !s.isConnected();
+    } catch (Exception e) {
+      return true;
+    }
   }
 
 }

Modified: logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/views/LogbackView.java
==============================================================================
--- logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/views/LogbackView.java	(original)
+++ logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/views/LogbackView.java	Mon Mar 26 21:45:42 2007
@@ -8,6 +8,7 @@
 import org.eclipse.jface.action.IToolBarManager;
 import org.eclipse.jface.action.MenuManager;
 import org.eclipse.jface.action.Separator;
+import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.jface.viewers.DoubleClickEvent;
 import org.eclipse.jface.viewers.IDoubleClickListener;
@@ -51,7 +52,7 @@
  */
 
 public class LogbackView extends ViewPart {
-  
+
   private static final String TAG_DISPLAY = "display";
   private static final String TAG_FONTNAME = "fontName";
   private static final String TAG_FONTSIZE = "fontSize";
@@ -185,8 +186,7 @@
     if (port != null) {
       server.setPort(port.intValue());
     }
-    serverThread = new Thread(server);
-    serverThread.start();
+    startServerThread(server);
   }
 
   @Override
@@ -216,7 +216,6 @@
 
   public void updateServerPort(Integer serverPort) {
     launchSocketServer(serverPort);
-    PluginLogger.logInfo("Now listening on port: " + serverPort);
   }
 
   private void initServer() {
@@ -224,8 +223,18 @@
     if (memento != null) {
       server.init(memento);
     }
-    serverThread = new Thread(server);
-    serverThread.start();
+    startServerThread(server);
+  }
+
+  private void startServerThread(SimpleSocketServer server) {
+    if (server.isPortFree()) {
+      serverThread = new Thread(server);
+      serverThread.start();
+      PluginLogger.logInfo("Now listening on port: " + server.getPort());
+    } else {
+      String errorMessage = "Unable to start SocketServer on port: " + server.getPort() + ".";
+      MessageDialog.openInformation(this.getSite().getShell(), "Logback View", errorMessage);
+    }
   }
 
   public void saveState(IMemento memento) {
@@ -245,14 +254,14 @@
     initServer();
     if (memento != null) {
       IMemento mem = memento.getChild(TAG_DISPLAY);
-      if (mem == null)  {
+      if (mem == null) {
         return;
       }
-      
+
       tmpFontSize = mem.getInteger(TAG_FONTSIZE);
       tmpFontName = mem.getString(TAG_FONTNAME);
     }
-    
+
   }
 
   public Viewer getViewer() {
@@ -296,16 +305,16 @@
     }
     return null;
   }
-  
+
   public Font getFont() {
     return viewer.getTable().getFont();
   }
-  
+
   public void updateFont(String fontName, Integer fontSize) {
     if (fontName == null || fontSize == null) {
       return;
     }
-    
+
     Font font = null;
     try {
       font = new Font(Display.getDefault(), fontName, fontSize, SWT.NORMAL);



More information about the logback-dev mailing list