1 package org.apache.maven.wagon.providers.ssh.knownhost;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.ByteArrayInputStream;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.Set;
28
29 import org.codehaus.plexus.util.FileUtils;
30
31
32
33
34
35
36
37
38
39
40
41 public class FileKnownHostsProvider
42 extends StreamKnownHostsProvider
43 {
44 private final File file;
45
46
47
48
49
50
51
52 public FileKnownHostsProvider( File file )
53 throws IOException
54 {
55 super( file.exists() ? (InputStream) new FileInputStream( file ) : new ByteArrayInputStream( "".getBytes() ) );
56 this.file = file;
57 }
58
59
60
61
62
63
64
65 public FileKnownHostsProvider()
66 throws IOException
67 {
68 this( new File( System.getProperty( "user.home" ), ".ssh/known_hosts" ) );
69 }
70
71 public void storeKnownHosts( String contents )
72 throws IOException
73 {
74 Set<KnownHostEntry> hosts = this.loadKnownHosts( contents );
75
76 if ( ! this.knownHosts.equals( hosts ) )
77 {
78 file.getParentFile().mkdirs();
79 FileUtils.fileWrite( file.getAbsolutePath(), contents );
80 this.knownHosts = hosts;
81 }
82 }
83
84 public void addKnownHost( KnownHostEntry knownHostEntry )
85 throws IOException
86 {
87 if ( !this.knownHosts.contains( knownHostEntry ) )
88 {
89 String knownHost = knownHostEntry.getHostName() + " " + knownHostEntry.getKeyType() + " "
90 + knownHostEntry.getKeyValue() + "\n";
91 FileUtils.fileAppend( file.getAbsolutePath(), knownHost );
92 }
93 }
94
95 public File getFile()
96 {
97 return file;
98 }
99 }