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.compression;
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.transport.AlgorithmNotSupportedException;
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.Map;
046: import java.util.Properties;
047: import java.util.Vector;
048:
049: /**
050: *
051: *
052: * @author $author$
053: * @version $Revision: 1.30 $
054: */
055: public class SshCompressionFactory {
056: /** */
057: public final static String COMP_NONE = "none";
058: private static String defaultAlgorithm;
059: private static Map comps;
060: private static Log log = LogFactory
061: .getLog(SshCompressionFactory.class);
062:
063: static {
064: comps = new HashMap();
065:
066: log.info("Loading compression methods");
067:
068: comps.put(COMP_NONE, "");
069:
070: defaultAlgorithm = COMP_NONE;
071:
072: try {
073: Enumeration en = ConfigurationLoader
074: .getExtensionClassLoader().getResources(
075: "j2ssh.compression");
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("compression.name."
091: + String.valueOf(num)) != null) {
092: try {
093: name = properties
094: .getProperty("compression.name."
095: + String.valueOf(num));
096: cls = ConfigurationLoader
097: .getExtensionClassLoader()
098: .loadClass(
099: properties
100: .getProperty("compression.class."
101: + String
102: .valueOf(num)));
103: cls.newInstance();
104: comps.put(name, cls);
105: log.info("Installed " + name + " compression");
106: } catch (Throwable ex) {
107: log.info("Could not install cipher class for "
108: + name, ex);
109: }
110:
111: num++;
112: }
113: }
114: } catch (Throwable t) {
115: }
116: }
117:
118: /**
119: * Creates a new SshCompressionFactory object.
120: */
121: protected SshCompressionFactory() {
122: }
123:
124: /**
125: *
126: */
127: public static void initialize() {
128: }
129:
130: /**
131: *
132: *
133: * @return
134: */
135: public static String getDefaultCompression() {
136: return defaultAlgorithm;
137: }
138:
139: /**
140: *
141: *
142: * @return
143: */
144: public static List getSupportedCompression() {
145: return new ArrayList(comps.keySet());
146: }
147:
148: /**
149: *
150: *
151: * @param algorithmName
152: *
153: * @return
154: *
155: * @throws AlgorithmNotSupportedException
156: */
157: public static SshCompression newInstance(String algorithmName)
158: throws AlgorithmNotSupportedException {
159: try {
160: if (algorithmName.equals(COMP_NONE)) {
161: return null;
162: } else {
163: return (SshCompression) ((Class) comps
164: .get(algorithmName)).newInstance();
165: }
166: } catch (Exception e) {
167: throw new AlgorithmNotSupportedException(algorithmName
168: + " is not supported!");
169: }
170: }
171: }
|