Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
FileKnownHostsProvider |
|
| 1.5;1,5 |
1 | package org.apache.maven.wagon.providers.ssh.knownhost; |
|
2 | ||
3 | import com.jcraft.jsch.HostKey; |
|
4 | import com.jcraft.jsch.HostKeyRepository; |
|
5 | import com.jcraft.jsch.JSch; |
|
6 | ||
7 | import java.io.ByteArrayInputStream; |
|
8 | import java.io.File; |
|
9 | import java.io.FileInputStream; |
|
10 | import java.io.FileWriter; |
|
11 | import java.io.IOException; |
|
12 | import java.io.InputStream; |
|
13 | import java.io.PrintWriter; |
|
14 | ||
15 | import org.codehaus.plexus.util.IOUtil; |
|
16 | ||
17 | /* |
|
18 | * Copyright 2001-2005 The Apache Software Foundation. |
|
19 | * |
|
20 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
21 | * you may not use this file except in compliance with the License. |
|
22 | * You may obtain a copy of the License at |
|
23 | * |
|
24 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
25 | * |
|
26 | * Unless required by applicable law or agreed to in writing, software |
|
27 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
28 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
29 | * See the License for the specific language governing permissions and |
|
30 | * limitations under the License. |
|
31 | */ |
|
32 | ||
33 | /** |
|
34 | * Provides known hosts from a file |
|
35 | * |
|
36 | * @author Juan F. Codagnone |
|
37 | * @since Sep 12, 2005 |
|
38 | */ |
|
39 | public class FileKnownHostsProvider |
|
40 | extends StreamKnownHostsProvider |
|
41 | { |
|
42 | private final File file; |
|
43 | ||
44 | /** |
|
45 | * Creates the FileKnownHostsProvider. |
|
46 | * |
|
47 | * @param file the file that holds the known hosts, in the openssh format |
|
48 | * @throws IOException |
|
49 | */ |
|
50 | public FileKnownHostsProvider( File file ) |
|
51 | throws IOException |
|
52 | { |
|
53 | 0 | super( file.exists() ? (InputStream) new FileInputStream( file ) : new ByteArrayInputStream( "".getBytes() ) ); |
54 | 0 | this.file = file; |
55 | 0 | } |
56 | ||
57 | /** |
|
58 | * Creates a FileKnownHostsProvider using as file openssh knwon_host |
|
59 | * |
|
60 | * @throws IOException |
|
61 | * @see #FileKnownHostsProvider(File) |
|
62 | */ |
|
63 | public FileKnownHostsProvider() |
|
64 | throws IOException |
|
65 | { |
|
66 | 0 | this( new File( System.getProperty( "user.home" ), ".ssh/known_hosts" ) ); |
67 | 0 | } |
68 | ||
69 | public void storeKnownHosts( JSch sch ) |
|
70 | { |
|
71 | 0 | PrintWriter w = null; |
72 | try |
|
73 | { |
|
74 | 0 | w = new PrintWriter( new FileWriter( file ) ); |
75 | ||
76 | 0 | HostKeyRepository hkr = sch.getHostKeyRepository(); |
77 | 0 | HostKey[] keys = hkr.getHostKey(); |
78 | ||
79 | 0 | for ( int i = 0; i < keys.length; i++ ) |
80 | { |
|
81 | 0 | HostKey key = keys[i]; |
82 | 0 | w.println( key.getHost() + " " + key.getType() + " " + key.getKey() ); |
83 | } |
|
84 | } |
|
85 | 0 | catch ( IOException e ) |
86 | { |
|
87 | // TODO: log it |
|
88 | } |
|
89 | finally |
|
90 | { |
|
91 | 0 | IOUtil.close( w ); |
92 | 0 | } |
93 | ||
94 | 0 | super.storeKnownHosts( sch ); |
95 | 0 | } |
96 | ||
97 | public File getFile() |
|
98 | { |
|
99 | 0 | return file; |
100 | } |
|
101 | } |