view src/com/five_ten_sg/connectbot/transport/TransportFactory.java @ 204:aab87298244f

Added tag stable-1.8.2 for changeset 88c4168cc272
author Carl Byington <carl@five-ten-sg.com>
date Wed, 02 Jul 2014 22:20:20 -0700
parents 77ac18bc1b2f
children
line wrap: on
line source

/*
 * ConnectBot: simple, powerful, open-source SSH client for Android
 * Copyright 2007 Kenny Root, Jeffrey Sharkey
 *
 * 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 com.five_ten_sg.connectbot.transport;

import java.util.HashMap;
import java.util.Map;

import com.five_ten_sg.connectbot.bean.HostBean;
import com.five_ten_sg.connectbot.util.HostDatabase;
import android.content.Context;
import android.net.Uri;
import android.util.Log;


/**
 * @author Kenny Root
 *
 */
public class TransportFactory {
    private static final String TAG = "ConnectBot.TransportFactory";

    private static String[] transportNames = {
        SSH.getProtocolName(),
        TN5250.getProtocolName(),
        Telnet.getProtocolName(),
        Local.getProtocolName(),
    };

    /**
     * @param protocol
     * @return
     */
    public static AbsTransport getTransport(String protocol) {
        if (SSH.getProtocolName().equals(protocol)) {
            return new SSH();
        }
        else if (TN5250.getProtocolName().equals(protocol)) {
            return new TN5250();
        }
        else if (Telnet.getProtocolName().equals(protocol)) {
            return new Telnet();
        }
        else if (Local.getProtocolName().equals(protocol)) {
            return new Local();
        }
        else {
            return null;
        }
    }

    public static Uri getUri(String protocol, String input) {
        Log.d("TransportFactory", String.format(
                  "Attempting to discover URI for protocol=%s on input=%s",
                  protocol, input));
        AbsTransport t = getTransport(protocol);

        if (t == null) return null;

        return t.getUri(input);
    }

    public static String[] getTransportNames() {
        return transportNames;
    }

    public static boolean isSameTransportType(AbsTransport a, AbsTransport b) {
        if (a == null || b == null)
            return false;

        return a.getClass().equals(b.getClass());
    }

    public static boolean canForwardPorts(String protocol) {
        AbsTransport t = getTransport(protocol);

        if (t == null) return false;

        return t.canForwardPorts();
    }

    /**
     * @param protocol text name of protocol
     * @param context
     * @return expanded format hint
     */
    public static String getFormatHint(String protocol, Context context) {
        AbsTransport t = getTransport(protocol);

        if (t == null) return "???";

        return t.getFormatHint(context);
    }

    /**
     * @param hostdb Handle to HostDatabase
     * @param uri URI to target server
     * @return HostBean or null
     */
    public static HostBean findHost(HostDatabase hostdb, Uri uri) {
        AbsTransport transport = getTransport(uri.getScheme());
        Map<String, String> selection = new HashMap<String, String>();
        transport.getSelectionArgs(uri, selection);

        if (selection.size() == 0) {
            Log.e(TAG, String.format("Transport %s failed to do something useful with URI=%s",
                                     uri.getScheme(), uri.toString()));
            throw new IllegalStateException("Failed to get needed selection arguments");
        }

        return hostdb.findHost(selection);
    }
}