comparison app/src/main/java/com/five_ten_sg/connectbot/transport/TransportFactory.java @ 438:d29cce60f393

migrate from Eclipse to Android Studio
author Carl Byington <carl@five-ten-sg.com>
date Thu, 03 Dec 2015 11:23:55 -0800
parents src/com/five_ten_sg/connectbot/transport/TransportFactory.java@77ac18bc1b2f
children
comparison
equal deleted inserted replaced
437:208b31032318 438:d29cce60f393
1 /*
2 * ConnectBot: simple, powerful, open-source SSH client for Android
3 * Copyright 2007 Kenny Root, Jeffrey Sharkey
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package com.five_ten_sg.connectbot.transport;
19
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import com.five_ten_sg.connectbot.bean.HostBean;
24 import com.five_ten_sg.connectbot.util.HostDatabase;
25 import android.content.Context;
26 import android.net.Uri;
27 import android.util.Log;
28
29
30 /**
31 * @author Kenny Root
32 *
33 */
34 public class TransportFactory {
35 private static final String TAG = "ConnectBot.TransportFactory";
36
37 private static String[] transportNames = {
38 SSH.getProtocolName(),
39 TN5250.getProtocolName(),
40 Telnet.getProtocolName(),
41 Local.getProtocolName(),
42 };
43
44 /**
45 * @param protocol
46 * @return
47 */
48 public static AbsTransport getTransport(String protocol) {
49 if (SSH.getProtocolName().equals(protocol)) {
50 return new SSH();
51 }
52 else if (TN5250.getProtocolName().equals(protocol)) {
53 return new TN5250();
54 }
55 else if (Telnet.getProtocolName().equals(protocol)) {
56 return new Telnet();
57 }
58 else if (Local.getProtocolName().equals(protocol)) {
59 return new Local();
60 }
61 else {
62 return null;
63 }
64 }
65
66 public static Uri getUri(String protocol, String input) {
67 Log.d("TransportFactory", String.format(
68 "Attempting to discover URI for protocol=%s on input=%s",
69 protocol, input));
70 AbsTransport t = getTransport(protocol);
71
72 if (t == null) return null;
73
74 return t.getUri(input);
75 }
76
77 public static String[] getTransportNames() {
78 return transportNames;
79 }
80
81 public static boolean isSameTransportType(AbsTransport a, AbsTransport b) {
82 if (a == null || b == null)
83 return false;
84
85 return a.getClass().equals(b.getClass());
86 }
87
88 public static boolean canForwardPorts(String protocol) {
89 AbsTransport t = getTransport(protocol);
90
91 if (t == null) return false;
92
93 return t.canForwardPorts();
94 }
95
96 /**
97 * @param protocol text name of protocol
98 * @param context
99 * @return expanded format hint
100 */
101 public static String getFormatHint(String protocol, Context context) {
102 AbsTransport t = getTransport(protocol);
103
104 if (t == null) return "???";
105
106 return t.getFormatHint(context);
107 }
108
109 /**
110 * @param hostdb Handle to HostDatabase
111 * @param uri URI to target server
112 * @return HostBean or null
113 */
114 public static HostBean findHost(HostDatabase hostdb, Uri uri) {
115 AbsTransport transport = getTransport(uri.getScheme());
116 Map<String, String> selection = new HashMap<String, String>();
117 transport.getSelectionArgs(uri, selection);
118
119 if (selection.size() == 0) {
120 Log.e(TAG, String.format("Transport %s failed to do something useful with URI=%s",
121 uri.getScheme(), uri.toString()));
122 throw new IllegalStateException("Failed to get needed selection arguments");
123 }
124
125 return hostdb.findHost(selection);
126 }
127 }