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.io.IOUtil;
031: import com.sshtools.j2ssh.openssh.*;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035:
036: import java.io.InputStream;
037:
038: import java.net.URL;
039:
040: import java.util.ArrayList;
041: import java.util.Enumeration;
042: import java.util.HashMap;
043: import java.util.Iterator;
044: import java.util.List;
045: import java.util.Properties;
046: import java.util.Vector;
047:
048: /**
049: *
050: *
051: * @author $author$
052: * @version $Revision: 1.26 $
053: */
054: public class SshPrivateKeyFormatFactory {
055: private static String defaultFormat;
056: private static HashMap formatTypes;
057: private static Log log = LogFactory
058: .getLog(SshPrivateKeyFormatFactory.class);
059: private static Vector types;
060:
061: static {
062: log.info("Loading private key formats");
063:
064: List formats = new ArrayList();
065: types = new Vector();
066: formatTypes = new HashMap();
067: formats.add(SshtoolsPrivateKeyFormat.class.getName());
068: formats.add(OpenSSHPrivateKeyFormat.class.getName());
069:
070: defaultFormat = "SSHTools-PrivateKey-Base64Encoded";
071:
072: try {
073: Enumeration en = ConfigurationLoader
074: .getExtensionClassLoader().getResources(
075: "j2ssh.privatekey");
076: URL url;
077: Properties properties = new Properties();
078: InputStream in;
079:
080: while ((en != null) && en.hasMoreElements()) {
081: url = (URL) en.nextElement();
082: in = url.openStream();
083: properties.load(in);
084: IOUtil.closeStream(in);
085:
086: int num = 1;
087: String name = "";
088: Class cls;
089:
090: while (properties.getProperty("privatekey.name."
091: + String.valueOf(num)) != null) {
092: name = properties.getProperty("privatekey.name."
093: + String.valueOf(num));
094: formats.add(properties
095: .getProperty("privatekey.class."
096: + String.valueOf(num)));
097:
098: num++;
099: }
100: }
101: } catch (Throwable t) {
102: }
103:
104: SshPrivateKeyFormat f;
105:
106: Iterator it = formats.iterator();
107: String classname;
108:
109: while (it.hasNext()) {
110: classname = (String) it.next();
111:
112: try {
113: Class cls = ConfigurationLoader
114: .getExtensionClass(classname);
115: f = (SshPrivateKeyFormat) cls.newInstance();
116: log.debug("Installing " + f.getFormatType()
117: + " private key format");
118: formatTypes.put(f.getFormatType(), cls);
119: types.add(f.getFormatType());
120: } catch (Throwable t) {
121: log.warn("Private key format implemented by "
122: + classname + " will not be available", t);
123: }
124: }
125: }
126:
127: /**
128: *
129: *
130: * @return
131: */
132: public static List getSupportedFormats() {
133: return types;
134: }
135:
136: public static void initialize() {
137: }
138:
139: /**
140: *
141: *
142: * @param type
143: *
144: * @return
145: *
146: * @throws InvalidSshKeyException
147: */
148: public static SshPrivateKeyFormat newInstance(String type)
149: throws InvalidSshKeyException {
150: try {
151: if (formatTypes.containsKey(type)) {
152: return (SshPrivateKeyFormat) ((Class) formatTypes
153: .get(type)).newInstance();
154: } else {
155: throw new InvalidSshKeyException("The format type "
156: + type + " is not supported");
157: }
158: } catch (IllegalAccessException iae) {
159: throw new InvalidSshKeyException(
160: "Illegal access to class implementation of " + type);
161: } catch (InstantiationException ie) {
162: throw new InvalidSshKeyException(
163: "Failed to create instance of format type " + type);
164: }
165: }
166:
167: /**
168: *
169: *
170: * @return
171: */
172: public static String getDefaultFormatType() {
173: return defaultFormat;
174: }
175: }
|