comparison src/ch/ethz/ssh2/compression/CompressionFactory.java @ 273:91a31873c42a ganymed

start conversion from trilead to ganymed
author Carl Byington <carl@five-ten-sg.com>
date Fri, 18 Jul 2014 11:21:46 -0700
parents
children 071eccdff8ea
comparison
equal deleted inserted replaced
272:ce2f4e397703 273:91a31873c42a
1 package ch.ethz.ssh2.compression;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 /**
7 * @author Kenny Root
8 * @version $Id: CompressionFactory.java 156 2014-04-29 12:02:35Z dkocher@sudo.ch $
9 */
10 public class CompressionFactory {
11 static class CompressorEntry {
12 String type;
13 String compressorClass;
14
15 public CompressorEntry(String type, String compressorClass) {
16 this.type = type;
17 this.compressorClass = compressorClass;
18 }
19 }
20
21 private static final List<CompressorEntry> compressors
22 = new ArrayList<CompressorEntry>();
23
24 static {
25 // Higher priority first
26 compressors.add(new CompressorEntry("none", null));
27 }
28
29 public static String[] getDefaultCompressorList() {
30 String list[] = new String[compressors.size()];
31 for(int i = 0; i < compressors.size(); i++) {
32 CompressorEntry ce = compressors.get(i);
33 list[i] = ce.type;
34 }
35 return list;
36 }
37
38 public static void checkCompressorList(String[] list) {
39 for(final String candidate : list) {
40 getEntry(candidate);
41 }
42 }
43
44 public static Compressor createCompressor(String type) {
45 try {
46 CompressorEntry ce = getEntry(type);
47 if(null == ce.compressorClass) {
48 return null;
49 }
50 Class<?> cc = Class.forName(ce.compressorClass);
51 return (Compressor) cc.newInstance();
52 }
53 catch(Exception e) {
54 throw new IllegalArgumentException("Cannot instantiate " + type);
55 }
56 }
57
58 private static CompressorEntry getEntry(String type) {
59 for(CompressorEntry ce : compressors) {
60 if(ce.type.equals(type)) {
61 return ce;
62 }
63 }
64 throw new IllegalArgumentException("Unknown algorithm " + type);
65 }
66 }