geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From xuhaih...@apache.org
Subject svn commit: r1029580 - in /geronimo/server/trunk/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell: BaseCommandSupport.java deploy/ConnectCommand.java
Date Mon, 01 Nov 2010 09:15:11 GMT
Author: xuhaihong
Date: Mon Nov  1 09:15:11 2010
New Revision: 1029580

URL: http://svn.apache.org/viewvc?rev=1029580&view=rev
Log:
GERONIMO-5579 Check hostname to determine whether the expected server is the embedded one
Just the first step changes, some naming updates are required

Modified:
    geronimo/server/trunk/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/BaseCommandSupport.java
    geronimo/server/trunk/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/deploy/ConnectCommand.java

Modified: geronimo/server/trunk/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/BaseCommandSupport.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/BaseCommandSupport.java?rev=1029580&r1=1029579&r2=1029580&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/BaseCommandSupport.java
(original)
+++ geronimo/server/trunk/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/BaseCommandSupport.java
Mon Nov  1 09:15:11 2010
@@ -20,10 +20,16 @@
 package org.apache.geronimo.shell;
 
 import java.io.IOException;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
+import java.net.UnknownHostException;
+import java.util.Enumeration;
 import java.util.Set;
 
 import org.apache.felix.karaf.shell.console.OsgiCommandSupport;
 import org.apache.geronimo.deployment.cli.ConsoleReader;
+import org.apache.geronimo.gbean.AbstractName;
 import org.apache.geronimo.gbean.AbstractNameQuery;
 import org.apache.geronimo.kernel.Kernel;
 import org.osgi.framework.ServiceReference;
@@ -82,7 +88,7 @@ public abstract class BaseCommandSupport
     public String readLine(String msg) throws IOException {
         return readLine(msg, '\0');
     }
-    
+
     private String readLine(String msg, char mask) throws IOException {
         StringBuffer sb = new StringBuffer();
         if (msg != null) {
@@ -100,9 +106,9 @@ public abstract class BaseCommandSupport
                     continue;
                 }
                 session.getConsole().print("\b \b");
-                session.getConsole().flush();               
+                session.getConsole().flush();
                 sb.delete(size - 1, size);
-            } else if (c == '\r' || c == '\n') {  
+            } else if (c == '\r' || c == '\n') {
                 session.getConsole().println();
                 session.getConsole().flush();
                 break;
@@ -133,7 +139,7 @@ public abstract class BaseCommandSupport
     public String readPassword(String prompt) throws IOException {
         return readLine(prompt, '*');
     }
-    
+
     public Kernel getKernel() {
         ServiceReference reference = bundleContext.getServiceReference(Kernel.class.getName());
         Kernel kernel = null;
@@ -142,17 +148,77 @@ public abstract class BaseCommandSupport
         }
         return kernel;
     }
-    
+
     public boolean isEmbedded() {
         return isEmbedded(getKernel());
     }
-    
+
     public boolean isEmbedded(Kernel kernel) {
         if (kernel != null) {
-            Set deamon = kernel.listGBeans(new AbstractNameQuery("org.apache.geronimo.system.main.EmbeddedDaemon"));
+            Set<AbstractName> deamon = kernel.listGBeans(new AbstractNameQuery("org.apache.geronimo.system.main.EmbeddedDaemon"));
             return !deamon.isEmpty();
         } else {
             return false;
-        }  
+        }
+    }
+
+    public boolean isEmbeddedServer(String hostname, int port) {
+        if (!isEmbedded()) {
+            return false;
+        }
+        Kernel kernel = getKernel();
+        try {
+            int embeddedPort = (Integer) kernel.getAttribute("RMIRegistry", "port");
+            if (port != embeddedPort) {
+                return false;
+            }
+            // If it runs here, the user should have access to the local shell.
+            // we should only identify whether the host name is any interface of the local
machine, and ignore whether the server is really listening on this interface.
+            return isLocalMachine(hostname);
+        } catch (Exception e) {
+            return false;
+        }
+    }
+
+    private boolean isLocalMachine(String hostName) {
+        try {
+            InetAddress hostAddress = InetAddress.getByName(hostName);
+            if (hostAddress.isAnyLocalAddress() || hostAddress.isLoopbackAddress() || hostName.equals(getLocalHostName())
|| hostName.equals(getFullLocalHostName())) {
+                return true;
+            } else {
+                Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
+                while (en.hasMoreElements()) {
+                    NetworkInterface iface = en.nextElement();
+                    Enumeration<InetAddress> ien = iface.getInetAddresses();
+                    while (ien.hasMoreElements()) {
+                        InetAddress address = ien.nextElement();
+                        if (address.equals(hostAddress)) {
+                            return true;
+                        }
+                    }
+                }
+                return false;
+            }
+        } catch (SocketException e) {
+            return false;
+        } catch (UnknownHostException e) {
+            return false;
+        }
+    }
+
+    private String getLocalHostName() {
+        try {
+            return InetAddress.getLocalHost().getHostName();
+        } catch (UnknownHostException e) {
+            return null;
+        }
+    }
+
+    private String getFullLocalHostName() {
+        try {
+            return InetAddress.getLocalHost().getCanonicalHostName();
+        } catch (UnknownHostException e) {
+            return null;
+        }
     }
 }

Modified: geronimo/server/trunk/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/deploy/ConnectCommand.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/deploy/ConnectCommand.java?rev=1029580&r1=1029579&r2=1029580&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/deploy/ConnectCommand.java
(original)
+++ geronimo/server/trunk/framework/modules/geronimo-shell-base/src/main/java/org/apache/geronimo/shell/deploy/ConnectCommand.java
Mon Nov  1 09:15:11 2010
@@ -20,21 +20,17 @@
 package org.apache.geronimo.shell.deploy;
 
 import java.io.IOException;
-import java.util.Set;
 
 import org.apache.felix.gogo.commands.Command;
 import org.apache.felix.gogo.commands.Option;
+import org.apache.geronimo.cli.deployer.ConnectionParamsImpl;
 import org.apache.geronimo.deployment.cli.OfflineServerConnection;
 import org.apache.geronimo.deployment.cli.OnlineServerConnection;
 import org.apache.geronimo.deployment.cli.ServerConnection;
 import org.apache.geronimo.deployment.cli.ServerConnection.UsernamePasswordHandler;
 import org.apache.geronimo.deployment.plugin.factories.BaseDeploymentFactory;
-import org.apache.geronimo.gbean.AbstractNameQuery;
-import org.apache.geronimo.cli.deployer.ConnectionParamsImpl;
 import org.apache.geronimo.kernel.Kernel;
 import org.apache.geronimo.shell.BaseCommandSupport;
-
-import org.osgi.framework.ServiceReference;
 import org.osgi.service.command.CommandSession;
 
 /**
@@ -138,16 +134,16 @@ public class ConnectCommand extends Base
      */
     protected ServerConnection openConnection(boolean quiet) throws Exception {
         Kernel kernel = getKernel();
-        
+
         ServerConnection connection;
-        
-        if (isEmbedded(kernel)) {
+
+        if (isEmbeddedServer(hostname, port)) {
             connection = new OfflineServerConnection(kernel, false);
         } else {
             if (!quiet) {
                 println("Connecting to Geronimo server: " + hostname + ":" + port);
             }
-        
+
             BaseDeploymentFactory deploymentFactory = new BaseDeploymentFactory();
             ConnectionParamsImpl connectionParams = new ConnectionParamsImpl(null, hostname,
port, null, username, password, false, false, false, secure);
             connection = new OnlineServerConnection(connectionParams, new ShellUserPasswordHandler(session),
deploymentFactory);



Mime
View raw message