Coverage Report - org.apache.maven.scm.provider.perforce.command.PerforceInfoCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
PerforceInfoCommand
0 %
0/47
0 %
0/18
5
 
 1  
 package org.apache.maven.scm.provider.perforce.command;
 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 org.apache.maven.scm.CommandParameters;
 23  
 import org.apache.maven.scm.ScmException;
 24  
 import org.apache.maven.scm.ScmFileSet;
 25  
 import org.apache.maven.scm.ScmResult;
 26  
 import org.apache.maven.scm.command.AbstractCommand;
 27  
 import org.apache.maven.scm.log.ScmLogger;
 28  
 import org.apache.maven.scm.provider.ScmProviderRepository;
 29  
 import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
 30  
 import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
 31  
 import org.codehaus.plexus.util.IOUtil;
 32  
 import org.codehaus.plexus.util.cli.CommandLineException;
 33  
 import org.codehaus.plexus.util.cli.Commandline;
 34  
 
 35  
 import java.io.BufferedReader;
 36  
 import java.io.IOException;
 37  
 import java.io.InputStreamReader;
 38  
 import java.util.HashMap;
 39  
 import java.util.Map;
 40  
 
 41  
 /**
 42  
  * Encapsulates the 'p4 info' command which can be very useful in determining
 43  
  * the runtime environment.  Use <code>getEntry(String key)</code> to query
 44  
  * the info set for a particular property.  The data from p4 info looks like this:
 45  
  * <p/>
 46  
  * <pre>
 47  
  * User name: mperham
 48  
  * Client name: mikeperham-dt
 49  
  * Client host: mikeperham-dt
 50  
  * Client root: d:\perforce
 51  
  * </pre>
 52  
  * <p/>
 53  
  * where the key is the content before the first colon and the value is the data after
 54  
  * the first colon, trimmed.  For example:
 55  
  * <code>PerforceInfoCommand.getInfo( this, repo ).getEntry( "User name" )</code>
 56  
  * <p/>
 57  
  * Note that this is not a traditional SCM command.  This uses the Command class
 58  
  * simply because it needs a logger for error handling and the current repository data for
 59  
  * command line creation.
 60  
  *
 61  
  * @author mperham
 62  
  * @version $Id: $
 63  
  */
 64  0
 public class PerforceInfoCommand
 65  
     extends AbstractCommand
 66  
     implements PerforceCommand
 67  
 {
 68  0
     private static PerforceInfoCommand singleton = null;
 69  
 
 70  0
     private Map<String, String> entries = null;
 71  
 
 72  
     public static PerforceInfoCommand getInfo( ScmLogger logger, PerforceScmProviderRepository repo )
 73  
     {
 74  0
         return getSingleton( logger, repo );
 75  
     }
 76  
 
 77  
     public String getEntry( String key )
 78  
     {
 79  0
         return (String) entries.get( key );
 80  
     }
 81  
 
 82  
     private static synchronized PerforceInfoCommand getSingleton( ScmLogger logger, PerforceScmProviderRepository repo )
 83  
     {
 84  0
         if ( singleton == null )
 85  
         {
 86  0
             PerforceInfoCommand pic = new PerforceInfoCommand();
 87  0
             if ( logger != null )
 88  
             {
 89  0
                 pic.setLogger( logger );
 90  
             }
 91  
             try
 92  
             {
 93  0
                 pic.executeCommand( repo, null, null );
 94  0
                 singleton = pic;
 95  
             }
 96  0
             catch ( ScmException e )
 97  
             {
 98  0
                 if ( pic.getLogger().isErrorEnabled() )
 99  
                 {
 100  0
                     pic.getLogger().error( "ScmException " + e.getMessage(), e );
 101  
                 }
 102  0
             }
 103  
         }
 104  
 
 105  0
         return singleton;
 106  
     }
 107  
 
 108  
     /**
 109  
      * {@inheritDoc}
 110  
      */
 111  
     protected ScmResult executeCommand( ScmProviderRepository repo, ScmFileSet scmFileSet,
 112  
                                         CommandParameters commandParameters )
 113  
         throws ScmException
 114  
     {
 115  0
         if ( !PerforceScmProvider.isLive() )
 116  
         {
 117  0
             return null;
 118  
         }
 119  0
         InputStreamReader isReader = null;
 120  
         try
 121  
         {
 122  0
             Commandline command = PerforceScmProvider.createP4Command( (PerforceScmProviderRepository) repo, null );
 123  0
             command.createArg().setValue( "info" );
 124  0
             if ( getLogger().isDebugEnabled() )
 125  
             {
 126  0
                 getLogger().debug( PerforceScmProvider.clean( "Executing: " + command.toString() ) );
 127  
             }
 128  0
             Process proc = command.execute();
 129  0
             isReader = new InputStreamReader( proc.getInputStream() );
 130  0
             BufferedReader br = new BufferedReader( isReader );
 131  
             String line;
 132  0
             entries = new HashMap<String, String>();
 133  0
             while ( ( line = br.readLine() ) != null )
 134  
             {
 135  0
                 int idx = line.indexOf( ':' );
 136  0
                 if ( idx == -1 )
 137  
                 {
 138  0
                     if ( line.indexOf( "Client unknown." ) == -1 )
 139  
                     {
 140  0
                         throw new IllegalStateException( "Unexpected results from 'p4 info' command: " + line );
 141  
                     }
 142  
 
 143  0
                     if ( getLogger().isDebugEnabled() )
 144  
                     {
 145  0
                         getLogger().debug( "Cannot find client." );
 146  
                     }
 147  0
                     entries.put( "Client root", "" );
 148  
                 }
 149  
                 else
 150  
                 {
 151  0
                     String key = line.substring( 0, idx );
 152  0
                     String value = line.substring( idx + 1 ).trim();
 153  0
                     entries.put( key, value );
 154  
                 }
 155  0
             }
 156  0
         }
 157  0
         catch ( CommandLineException e )
 158  
         {
 159  0
             throw new ScmException( e.getLocalizedMessage() );
 160  
         }
 161  0
         catch ( IOException e )
 162  
         {
 163  0
             throw new ScmException( e.getLocalizedMessage() );
 164  
         }
 165  
         finally
 166  
         {
 167  0
             IOUtil.close( isReader );
 168  0
         }
 169  0
         return null;
 170  
     }
 171  
 }