/*
 * The Gemma project
 * 
 * Copyright (c) 2005 Columbia University
 * 
 * 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 edu.columbia.gemma.loader.expression.smd.util;

import java.io.IOException;
import java.net.SocketException;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

/**
 * <hr>
 * <p>
 * Copyright (c) 2004 Columbia University
 * 
 * @author pavlidis
 * @version $Id$
 */
public class SmdUtil {

    protected static final Log log = LogFactory.getLog( SmdUtil.class );
    private static String host;
    private static String login;
    private static String password;
    public static final String SMD_DELIM = "\n";

    static {
        Configuration config = null;
        try {
            config = new PropertiesConfiguration( "Gemma.properties" );
        } catch ( ConfigurationException e ) {
            log.error( e );
        }
        host = ( String ) config.getProperty( "smd.host" );
        login = ( String ) config.getProperty( "smd.login" );
        password = ( String ) config.getProperty( "smd.password" );
    }

    /**
     * Split a SMD-formatted key-value string. These are preceded by 0 or more white-space, a "!", and then a
     * "="-delimited key-value pair.
     * 
     * @param k
     * @return String array containing the key and value, or null if the input was not a valid SMD-formatted key-value.
     */
    public static String[] smdSplit( String k ) {
        String f = k.trim();

        if ( !f.startsWith( "!" ) ) return null;
        f = f.replaceFirst( "^!", "" );
        String[] vals = f.split( "=" ); // could be nothing after the equals.
        if ( vals.length < 1 ) throw new IllegalStateException( "Could not parse " + k );
        return vals;
    }

    /**
     * Convenient method to get a FTP connection.
     * 
     * @param host
     * @param login
     * @param password
     * @param mode
     * @return
     * @throws SocketException
     * @throws IOException
     */
    public static FTPClient connect( int mode ) throws SocketException, IOException {
        FTPClient f = new FTPClient();

        boolean success = false;
        f.connect( host );
        int reply = f.getReplyCode();
        if ( FTPReply.isPositiveCompletion( reply ) ) success = f.login( login, password );
        if ( !success ) {
            f.disconnect();
            throw new IOException( "Couldn't connect to " + host );
        }
        f.setFileType( mode );
        log.info( "Connected to " + host );
        return f;
    }

    /**
     * @return
     */
    public static String getHostName() {
        return host;
    }

}
@@ -1067,50 +1067,38 @@
 nt;%0A
+%0A
 import 
-org.apache.commons.net.ftp.FTPReply
+baseCode.util.NetUtils
 ;%0A%0A/
@@ -2866,453 +2866,62 @@
     
-FTPClient f = new FTPClient();%0A%0A        boolean success = false;%0A        f.connect( host );%0A        int reply = f.getReplyCode();%0A        if ( FTPReply.isPositiveCompletion( reply ) ) success = f.login( login, password );%0A        if ( !success ) %7B%0A            f.disconnect();%0A            throw new IOException( %22Couldn't connect to %22 + host );%0A        %7D%0A        f.setFileType( mode );%0A        log.info( %22Connected to %22 + host );%0A        return f
+return NetUtils.connect( mode, host, login, password )
 ;%0A  

change to use NetUtils