Author: evenisse
Date: Thu Mar 15 03:22:19 2007
New Revision: 518563

URL: http://svn.apache.org/viewvc?view=rev&rev=518563
Log:
[SCM-284] Add support for ext protocol

Added:
    
maven/sandbox/trunk/scm/maven-scm-provider-cvsjava/src/main/java/org/apache/maven/scm/provider/cvslib/cvsjava/util/ExtConnection.java
   (with props)
Modified:
    maven/sandbox/trunk/scm/maven-scm-provider-cvsjava/pom.xml
    
maven/sandbox/trunk/scm/maven-scm-provider-cvsjava/src/main/java/org/apache/maven/scm/provider/cvslib/cvsjava/util/CvsConnection.java

Modified: maven/sandbox/trunk/scm/maven-scm-provider-cvsjava/pom.xml
URL: 
http://svn.apache.org/viewvc/maven/sandbox/trunk/scm/maven-scm-provider-cvsjava/pom.xml?view=diff&rev=518563&r1=518562&r2=518563
==============================================================================
--- maven/sandbox/trunk/scm/maven-scm-provider-cvsjava/pom.xml (original)
+++ maven/sandbox/trunk/scm/maven-scm-provider-cvsjava/pom.xml Thu Mar 15 
03:22:19 2007
@@ -39,6 +39,11 @@
       <version>20060125</version>
     </dependency>
     <dependency>
+      <groupId>ch.ethz.ganymed</groupId>
+      <artifactId>ganymed-ssh2</artifactId>
+      <version>build210</version>
+    </dependency>
+    <dependency>
       <groupId>org.apache.maven.scm</groupId>
       <artifactId>maven-scm-provider-cvstest</artifactId>
       <version>1.0-SNAPSHOT</version>

Modified: 
maven/sandbox/trunk/scm/maven-scm-provider-cvsjava/src/main/java/org/apache/maven/scm/provider/cvslib/cvsjava/util/CvsConnection.java
URL: 
http://svn.apache.org/viewvc/maven/sandbox/trunk/scm/maven-scm-provider-cvsjava/src/main/java/org/apache/maven/scm/provider/cvslib/cvsjava/util/CvsConnection.java?view=diff&rev=518563&r1=518562&r2=518563
==============================================================================
--- 
maven/sandbox/trunk/scm/maven-scm-provider-cvsjava/src/main/java/org/apache/maven/scm/provider/cvslib/cvsjava/util/CvsConnection.java
 (original)
+++ 
maven/sandbox/trunk/scm/maven-scm-provider-cvsjava/src/main/java/org/apache/maven/scm/provider/cvslib/cvsjava/util/CvsConnection.java
 Thu Mar 15 03:22:19 2007
@@ -21,6 +21,7 @@
 
 import org.apache.maven.scm.log.ScmLogger;
 import org.codehaus.plexus.util.StringUtils;
+import org.codehaus.plexus.util.cli.CommandLineUtils;
 import org.netbeans.lib.cvsclient.CVSRoot;
 import org.netbeans.lib.cvsclient.Client;
 import org.netbeans.lib.cvsclient.admin.StandardAdminHandler;
@@ -30,6 +31,7 @@
 import org.netbeans.lib.cvsclient.command.GlobalOptions;
 import org.netbeans.lib.cvsclient.commandLine.CommandFactory;
 import org.netbeans.lib.cvsclient.commandLine.GetOpt;
+import org.netbeans.lib.cvsclient.connection.AbstractConnection;
 import org.netbeans.lib.cvsclient.connection.AuthenticationException;
 import org.netbeans.lib.cvsclient.connection.Connection;
 import org.netbeans.lib.cvsclient.connection.ConnectionFactory;
@@ -113,10 +115,51 @@
     private void connect( CVSRoot root, String password )
         throws IllegalArgumentException, AuthenticationException, 
CommandAbortedException
     {
-        connection = ConnectionFactory.getConnection( root );
-        if ( CVSRoot.METHOD_PSERVER.equals( root.getMethod() ) )
+        if ( CVSRoot.METHOD_EXT.equals( root.getMethod() ) )
         {
-            ( (PServerConnection) connection ).setEncodedPassword( password );
+            String cvs_rsh = System.getProperty( "maven.scm.cvs.java.cvs_rsh" 
);
+            if ( cvs_rsh == null )
+            {
+                try
+                {
+                    cvs_rsh = CommandLineUtils.getSystemEnvVars().getProperty( 
"CVS_RSH" );
+                }
+                catch ( IOException e )
+                {
+                }
+            }
+
+            if ( cvs_rsh != null )
+            {
+                if ( cvs_rsh.indexOf( ' ' ) < 0 )
+                {
+                    //cvs_rsh should be 'rsh' or 'ssh'
+                    //Complete the command to use
+                    String username = root.getUserName();
+                    if ( username == null )
+                    {
+                        username = System.getProperty( "user.name" );
+                    }
+
+                    cvs_rsh += " " + username + "@" + root.getHostName() + " 
cvs server";
+                }
+
+                AbstractConnection conn = new 
org.netbeans.lib.cvsclient.connection.ExtConnection( cvs_rsh );
+                conn.setRepository( root.getRepository() );
+                connection = conn;
+            }
+            else
+            {
+                connection = new ExtConnection( root );
+            }
+        }
+        else
+        {
+            connection = ConnectionFactory.getConnection( root );
+            if ( CVSRoot.METHOD_PSERVER.equals( root.getMethod() ) )
+            {
+                ( (PServerConnection) connection ).setEncodedPassword( 
password );
+            }
         }
         connection.open();
 

Added: 
maven/sandbox/trunk/scm/maven-scm-provider-cvsjava/src/main/java/org/apache/maven/scm/provider/cvslib/cvsjava/util/ExtConnection.java
URL: 
http://svn.apache.org/viewvc/maven/sandbox/trunk/scm/maven-scm-provider-cvsjava/src/main/java/org/apache/maven/scm/provider/cvslib/cvsjava/util/ExtConnection.java?view=auto&rev=518563
==============================================================================
--- 
maven/sandbox/trunk/scm/maven-scm-provider-cvsjava/src/main/java/org/apache/maven/scm/provider/cvslib/cvsjava/util/ExtConnection.java
 (added)
+++ 
maven/sandbox/trunk/scm/maven-scm-provider-cvsjava/src/main/java/org/apache/maven/scm/provider/cvslib/cvsjava/util/ExtConnection.java
 Thu Mar 15 03:22:19 2007
@@ -0,0 +1,305 @@
+package org.apache.maven.scm.provider.cvslib.cvsjava.util;
+
+/*
+ * 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.
+ */
+
+import ch.ethz.ssh2.Connection;
+import ch.ethz.ssh2.Session;
+import ch.ethz.ssh2.StreamGobbler;
+import org.netbeans.lib.cvsclient.CVSRoot;
+import org.netbeans.lib.cvsclient.command.CommandAbortedException;
+import org.netbeans.lib.cvsclient.connection.AbstractConnection;
+import org.netbeans.lib.cvsclient.connection.AuthenticationException;
+import org.netbeans.lib.cvsclient.connection.ConnectionModifier;
+import org.netbeans.lib.cvsclient.util.LoggedDataInputStream;
+import org.netbeans.lib.cvsclient.util.LoggedDataOutputStream;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+/**
+ * @author <a href="mailto:[EMAIL PROTECTED]">Emmanuel Venisse</a>
+ * @version $Id$
+ */
+public class ExtConnection
+    extends AbstractConnection
+{
+    private String host;
+
+    private int port;
+
+    private String userName;
+
+    private String password;
+
+    private Connection connection;
+
+    private Session session;
+
+    private BufferedReader stderrReader;
+
+    public ExtConnection( CVSRoot cvsRoot )
+    {
+        this( cvsRoot.getHostName(), cvsRoot.getPort(), cvsRoot.getUserName(), 
cvsRoot.getPassword(),
+              cvsRoot.getRepository() );
+    }
+
+    public ExtConnection( String host, int port, String username, String 
password, String repository )
+    {
+        this.userName = username;
+
+        if ( this.userName == null )
+        {
+            this.userName = System.getProperty( "user.name" );
+        }
+
+        this.password = password;
+
+        this.host = host;
+
+        setRepository( repository );
+
+        this.port = port;
+
+        if ( this.port == 0 )
+        {
+            this.port = 22;
+        }
+    }
+
+    public void open()
+        throws AuthenticationException, CommandAbortedException
+    {
+        connection = new Connection( host, port );
+
+        /* TODO: add proxy support
+        ProxyData proxy = new HTTPProxyData( proxyHost, proxyPort, 
proxyUserName, proxyPassword );
+
+        connection.setProxyData( proxy );
+        */
+
+        try
+        {
+            // TODO: connection timeout?
+            connection.connect();
+        }
+        catch ( IOException e )
+        {
+            String message = "Cannot connect. Reason: " + e.getMessage();
+            throw new AuthenticationException( message, e, message );
+        }
+
+        File privateKey = getPrivateKey();
+
+        try
+        {
+            boolean authenticated;
+            if ( privateKey != null && privateKey.exists() )
+            {
+                authenticated = connection.authenticateWithPublicKey( 
userName, privateKey, getPassphrase() );
+            }
+            else
+            {
+                authenticated = connection.authenticateWithPassword( userName, 
password );
+            }
+
+            if ( !authenticated )
+            {
+                String message = "Authentication failed.";
+                throw new AuthenticationException( message, message );
+            }
+        }
+        catch ( IOException e )
+        {
+            closeConnection();
+            String message = "Cannot authenticate. Reason: " + e.getMessage();
+            throw new AuthenticationException( message, e, message );
+        }
+
+        try
+        {
+            session = connection.openSession();
+        }
+        catch ( IOException e )
+        {
+            String message = "Cannot open session. Reason: " + e.getMessage();
+            throw new CommandAbortedException( message, message );
+        }
+
+        String command = "cvs server";
+        try
+        {
+            session.execCommand( command );
+        }
+        catch ( IOException e )
+        {
+            String message = "Cannot execute remote command: " + command;
+            throw new CommandAbortedException( message, message );
+        }
+
+        InputStream stdout = new StreamGobbler( session.getStdout() );
+        InputStream stderr = new StreamGobbler( session.getStderr() );
+        stderrReader = new BufferedReader( new InputStreamReader( stderr ) );
+        setInputStream( new LoggedDataInputStream( stdout ) );
+        setOutputStream( new LoggedDataOutputStream( session.getStdin() ) );
+    }
+
+    public void verify()
+        throws AuthenticationException
+    {
+        try
+        {
+            open();
+            verifyProtocol();
+            close();
+        }
+        catch ( Exception e )
+        {
+            String message = "Failed to verify the connection: " + 
e.getMessage();
+            throw new AuthenticationException( message, e, message );
+        }
+    }
+
+    private void closeConnection()
+    {
+        try
+        {
+            if ( stderrReader != null )
+            {
+                while ( true )
+                {
+                    String line = stderrReader.readLine();
+                    if ( line == null )
+                    {
+                        break;
+                    }
+
+                    System.err.println( line );
+                }
+            }
+        }
+        catch ( IOException e )
+        {
+            //nothing to do
+        }
+
+        if ( session != null )
+        {
+            System.out.println( "Exit code:" + 
session.getExitStatus().intValue() );
+            session.close();
+        }
+
+        if ( connection != null )
+        {
+            connection.close();
+        }
+
+        reset();
+    }
+
+    private void reset()
+    {
+        connection = null;
+        session = null;
+        stderrReader = null;
+        setInputStream( null );
+        setOutputStream( null );
+    }
+
+    public void close()
+        throws IOException
+    {
+        closeConnection();
+    }
+
+    public boolean isOpen()
+    {
+        return connection != null;
+    }
+
+    public int getPort()
+    {
+        return port;
+    }
+
+    public void modifyInputStream( ConnectionModifier modifier )
+        throws IOException
+    {
+        modifier.modifyInputStream( getInputStream() );
+    }
+
+    public void modifyOutputStream( ConnectionModifier modifier )
+        throws IOException
+    {
+        modifier.modifyOutputStream( getOutputStream() );
+    }
+
+    private File getPrivateKey()
+    {
+        // If user don't define a password, he want to use a private key
+        File privateKey = null;
+        if ( password == null )
+        {
+            String pk = System.getProperty( 
"maven.scm.cvs.java.ssh.privateKey" );
+            if ( pk != null )
+            {
+                privateKey = new File( pk );
+            }
+            else
+            {
+                privateKey = findPrivateKey();
+            }
+        }
+        return privateKey;
+    }
+
+    private String getPassphrase()
+    {
+        String passphrase = System.getProperty( 
"maven.scm.cvs.java.ssh.passphrase" );
+
+        if ( passphrase == null )
+        {
+            passphrase = "";
+        }
+
+        return passphrase;
+    }
+
+    private File findPrivateKey()
+    {
+        String privateKeyDirectory = System.getProperty( 
"maven.scm.ssh.privateKeyDirectory" );
+
+        if ( privateKeyDirectory == null )
+        {
+            privateKeyDirectory = System.getProperty( "user.home" );
+        }
+
+        File privateKey = new File( privateKeyDirectory, ".ssh/id_dsa" );
+
+        if ( !privateKey.exists() )
+        {
+            privateKey = new File( privateKeyDirectory, ".ssh/id_rsa" );
+        }
+
+        return privateKey;
+    }
+}

Propchange: 
maven/sandbox/trunk/scm/maven-scm-provider-cvsjava/src/main/java/org/apache/maven/scm/provider/cvslib/cvsjava/util/ExtConnection.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/sandbox/trunk/scm/maven-scm-provider-cvsjava/src/main/java/org/apache/maven/scm/provider/cvslib/cvsjava/util/ExtConnection.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"


Reply via email to