Coverage Report - org.apache.maven.wagon.providers.ssh.knownhost.StreamKnownHostsProvider
 
Classes in this File Line Coverage Branch Coverage Complexity
StreamKnownHostsProvider
100 %
18/18
83 %
5/6
2,5
 
 1  
 package org.apache.maven.wagon.providers.ssh.knownhost;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.BufferedReader;
 23  
 import java.io.IOException;
 24  
 import java.io.InputStream;
 25  
 import java.io.StringReader;
 26  
 import java.util.HashSet;
 27  
 import java.util.Set;
 28  
 
 29  
 import org.codehaus.plexus.util.IOUtil;
 30  
 import org.codehaus.plexus.util.StringOutputStream;
 31  
 import org.codehaus.plexus.util.StringUtils;
 32  
 
 33  
 /**
 34  
  * Provides known hosts from a file
 35  
  *
 36  
  * @author Juan F. Codagnone
 37  
  * @since Sep 12, 2005
 38  
  */
 39  
 public class StreamKnownHostsProvider
 40  
     extends AbstractKnownHostsProvider
 41  
 {
 42  
 
 43  
     public StreamKnownHostsProvider( InputStream stream )
 44  
         throws IOException
 45  2
     {
 46  
         try
 47  
         {
 48  2
             StringOutputStream stringOutputStream = new StringOutputStream();
 49  2
             IOUtil.copy( stream, stringOutputStream );
 50  2
             this.contents = stringOutputStream.toString();
 51  
             
 52  2
             this.knownHosts = this.loadKnownHosts( this.contents );
 53  
         }
 54  
         finally
 55  
         {
 56  2
             IOUtil.close( stream );
 57  2
         }
 58  2
     }
 59  
     
 60  
     protected Set<KnownHostEntry> loadKnownHosts( String contents )
 61  
         throws IOException
 62  
     {
 63  4
         Set<KnownHostEntry> hosts = new HashSet<KnownHostEntry>();
 64  
         
 65  4
         BufferedReader br = new BufferedReader( new StringReader( contents ) );
 66  
         
 67  4
         String line = null;
 68  
         
 69  
         do 
 70  
         {
 71  13
             line = br.readLine();
 72  13
             if ( line != null )
 73  
             {
 74  9
                 String tokens[] = StringUtils.split( line );
 75  9
                 if ( tokens.length == 3 )
 76  
                 {
 77  9
                     hosts.add( new KnownHostEntry( tokens[0], tokens[1], tokens[2] ) );
 78  
                 }
 79  
             }
 80  
             
 81  
         }
 82  13
         while ( line != null );
 83  
         
 84  4
         return hosts;
 85  
     }
 86  
 }