001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.common.automate;
027:
028: import com.sshtools.j2ssh.configuration.ConfigurationException;
029: import com.sshtools.j2ssh.configuration.ConfigurationLoader;
030:
031: import java.util.Iterator;
032: import java.util.Map;
033:
034: /**
035: *
036: *
037: * @author $author$
038: * @version $Revision: 1.14 $
039: */
040: public class RemoteIdentificationFactory {
041: private static Map remoteIdentifications = null;
042:
043: static {
044: try {
045: if (ConfigurationLoader
046: .isConfigurationAvailable(AutomationConfiguration.class)) {
047: remoteIdentifications = ((AutomationConfiguration) ConfigurationLoader
048: .getConfiguration(AutomationConfiguration.class))
049: .getRemoteIdentifications();
050: }
051: } catch (ConfigurationException ex) {
052: }
053: }
054:
055: /**
056: *
057: *
058: * @param serverId
059: *
060: * @return
061: *
062: * @throws RemoteIdentificationException
063: */
064: public static synchronized RemoteIdentification getInstance(
065: String serverId, String hostname)
066: throws RemoteIdentificationException {
067: if (remoteIdentifications == null) {
068: throw new RemoteIdentificationException(
069: "There are no remote identification rules!");
070: }
071:
072: Iterator it = remoteIdentifications.entrySet().iterator();
073: Map.Entry entry;
074: RemoteIdentification rid;
075:
076: // Check the hostname first
077: while (it.hasNext()) {
078: entry = (Map.Entry) it.next();
079: rid = (RemoteIdentification) entry.getValue();
080:
081: if (hostname != null) {
082: if (rid.getDefaultName().equals(hostname)) {
083: return rid;
084: }
085: }
086: }
087:
088: it = remoteIdentifications.entrySet().iterator();
089:
090: // Now check against the rules
091: while (it.hasNext()) {
092: entry = (Map.Entry) it.next();
093: rid = (RemoteIdentification) entry.getValue();
094:
095: if (rid.testRules(serverId)) {
096: return rid;
097: }
098: }
099:
100: throw new RemoteIdentificationException(
101: "Failed to find a remote identification rule");
102: }
103:
104: /**
105: *
106: *
107: * @param args
108: */
109: public static void main(String[] args) {
110: try {
111: RemoteIdentification rid;
112: String serverId = "http://www.sshtools.com J2SSH 0.1.1 beta [CLIENT]";
113: rid = getInstance(serverId, null);
114: System.out.println("Remote Identification: "
115: + rid.getName(serverId));
116: serverId = "OpenSSH_3.4p1";
117: rid = getInstance(serverId, null);
118: System.out.println("Remote Identification: "
119: + rid.getName(serverId));
120: } catch (Exception e) {
121: e.printStackTrace();
122: }
123: }
124: }
|