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.j2ssh.transport.publickey;
027:
028: import com.sshtools.j2ssh.configuration.ConfigurationException;
029: import com.sshtools.j2ssh.configuration.ConfigurationLoader;
030: import com.sshtools.j2ssh.configuration.SshAPIConfiguration;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034:
035: import java.util.ArrayList;
036: import java.util.HashMap;
037: import java.util.Iterator;
038: import java.util.List;
039: import java.util.Vector;
040:
041: /**
042: *
043: *
044: * @author $author$
045: * @version $Revision: 1.22 $
046: */
047: public class SshPublicKeyFormatFactory {
048: private static String defaultFormat;
049: private static HashMap formatTypes = new HashMap();
050: private static Log log = LogFactory
051: .getLog(SshPublicKeyFormatFactory.class);
052: private static Vector types = new Vector();
053:
054: static {
055: List formats = new ArrayList();
056: formats.add(SECSHPublicKeyFormat.class.getName());
057: formats.add(OpenSSHPublicKeyFormat.class.getName());
058: defaultFormat = "SECSH-PublicKey-Base64Encoded";
059:
060: try {
061: if (ConfigurationLoader
062: .isConfigurationAvailable(SshAPIConfiguration.class)) {
063: SshAPIConfiguration config = (SshAPIConfiguration) ConfigurationLoader
064: .getConfiguration(SshAPIConfiguration.class);
065: defaultFormat = config.getDefaultPublicKeyFormat();
066: formats.addAll(config.getPublicKeyFormats());
067: }
068: } catch (ConfigurationException ex) {
069: }
070:
071: log.debug("Default public key format will be " + defaultFormat);
072:
073: SshPublicKeyFormat f;
074: Iterator it = formats.iterator();
075: String classname;
076:
077: while (it.hasNext()) {
078: classname = (String) it.next();
079:
080: try {
081: Class cls = ConfigurationLoader
082: .getExtensionClass(classname);
083: f = (SshPublicKeyFormat) cls.newInstance();
084: log.debug("Installing " + f.getFormatType()
085: + " public key format");
086: formatTypes.put(f.getFormatType(), cls);
087: types.add(f.getFormatType());
088: } catch (Exception iae) {
089: log.warn("Public key format implemented by "
090: + classname + " will not be available", iae);
091: }
092: }
093: }
094:
095: /**
096: *
097: *
098: * @return
099: */
100: public static List getSupportedFormats() {
101: return types;
102: }
103:
104: /**
105: *
106: *
107: * @param type
108: *
109: * @return
110: *
111: * @throws InvalidSshKeyException
112: */
113: public static SshPublicKeyFormat newInstance(String type)
114: throws InvalidSshKeyException {
115: try {
116: if (formatTypes.containsKey(type)) {
117: return (SshPublicKeyFormat) ((Class) formatTypes
118: .get(type)).newInstance();
119: } else {
120: throw new InvalidSshKeyException("The format type "
121: + type + " is not supported");
122: }
123: } catch (IllegalAccessException iae) {
124: throw new InvalidSshKeyException(
125: "Illegal access to class implementation of " + type);
126: } catch (InstantiationException ie) {
127: throw new InvalidSshKeyException(
128: "Failed to create instance of format type " + type);
129: }
130: }
131:
132: /**
133: *
134: *
135: * @return
136: */
137: public static String getDefaultFormatType() {
138: return defaultFormat;
139: }
140: }
|