Coverage Report - org.apache.maven.scm.provider.perforce.command.PerforceWhereCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
PerforceWhereCommand
0 %
0/56
0 %
0/32
9,667
 
 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.log.ScmLogger;
 23  
 import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
 24  
 import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
 25  
 import org.codehaus.plexus.util.IOUtil;
 26  
 import org.codehaus.plexus.util.cli.CommandLineException;
 27  
 import org.codehaus.plexus.util.cli.Commandline;
 28  
 
 29  
 import java.io.BufferedReader;
 30  
 import java.io.File;
 31  
 import java.io.IOException;
 32  
 import java.io.InputStreamReader;
 33  
 
 34  
 /**
 35  
  * Encapsulates the 'p4 where' command which can be very useful in determining
 36  
  * a file's location within the depot.  Use <code>getDepotLocation(String path)</code> to query
 37  
  * the depot location for a particular file.  The data from p4 where looks like this:
 38  
  * <p/>
 39  
  * <pre>
 40  
  * p4 where pom.xml
 41  
  * //depot/modules/fabric/trunk/pom.xml //mikeperham-dt/depot/modules/fabric/trunk/pom.xml
 42  
  * d:\perforce\depot\modules\fabric\trunk\pom.xml
 43  
  * </pre>
 44  
  *
 45  
  * @author mperham
 46  
  * @version $Id: $
 47  
  */
 48  
 public class PerforceWhereCommand
 49  
 {
 50  0
     private ScmLogger logger = null;
 51  
 
 52  0
     private PerforceScmProviderRepository repo = null;
 53  
 
 54  
     public PerforceWhereCommand( ScmLogger log, PerforceScmProviderRepository repos )
 55  0
     {
 56  0
         logger = log;
 57  0
         repo = repos;
 58  0
     }
 59  
 
 60  
     public String getDepotLocation( File file )
 61  
     {
 62  0
         return getDepotLocation( file.getAbsolutePath() );
 63  
     }
 64  
 
 65  
     /**
 66  
      * @param filepath an absolute file path
 67  
      * @return the absolute location of the given file within the Perforce repository or null if the file
 68  
      *         does not exist in a mapping within the current clientspec.
 69  
      */
 70  
     public String getDepotLocation( String filepath )
 71  
     {
 72  0
         if ( !PerforceScmProvider.isLive() )
 73  
         {
 74  0
             return null;
 75  
         }
 76  
 
 77  0
         InputStreamReader isReader = null;
 78  0
         InputStreamReader isReaderErr = null;
 79  
         try
 80  
         {
 81  0
             Commandline command = PerforceScmProvider.createP4Command( repo, null );
 82  0
             command.createArg().setValue( "where" );
 83  0
             command.createArg().setValue( filepath );
 84  0
             if ( logger.isDebugEnabled() )
 85  
             {
 86  0
                 logger.debug( PerforceScmProvider.clean( "Executing: " + command.toString() ) );
 87  
             }
 88  0
             Process proc = command.execute();
 89  0
             isReader = new InputStreamReader( proc.getInputStream() );
 90  0
             isReaderErr = new InputStreamReader( proc.getErrorStream() );
 91  0
             BufferedReader br = new BufferedReader( isReader );
 92  0
             BufferedReader brErr = new BufferedReader( isReaderErr );
 93  
             String line;
 94  0
             String path = null;
 95  0
             while ( ( line = br.readLine() ) != null )
 96  
             {
 97  0
                 if ( line.indexOf( "not in client view" ) != -1 )
 98  
                 {
 99  
                     // uh oh, something bad is happening
 100  0
                     if ( logger.isErrorEnabled() )
 101  
                     {
 102  0
                         logger.error( line );
 103  
                     }
 104  0
                     return null;
 105  
                 }
 106  0
                 if ( line.indexOf( "is not under" ) != -1 )
 107  
                 {
 108  
                     // uh oh, something bad is happening
 109  0
                     if ( logger.isErrorEnabled() )
 110  
                     {
 111  0
                         logger.error( line );
 112  
                     }
 113  0
                     return null;
 114  
                 }
 115  
 
 116  0
                 if ( logger.isDebugEnabled() )
 117  
                 {
 118  0
                     logger.debug( line );
 119  
                 }
 120  
                 // verify that "//" appears twice in the line
 121  0
                 path = line.substring( 0, line.lastIndexOf( "//" ) - 1 );
 122  
             }
 123  
             // Check for errors
 124  0
             while ( ( line = brErr.readLine() ) != null )
 125  
             {
 126  0
                 if ( line.indexOf( "not in client view" ) != -1 )
 127  
                 {
 128  
                     // uh oh, something bad is happening
 129  0
                     if ( logger.isErrorEnabled() )
 130  
                     {
 131  0
                         logger.error( line );
 132  
                     }
 133  0
                     return null;
 134  
                 }
 135  0
                 if ( line.indexOf( "is not under" ) != -1 )
 136  
                 {
 137  
                     // uh oh, something bad is happening
 138  0
                     if ( logger.isErrorEnabled() )
 139  
                     {
 140  0
                         logger.error( line );
 141  
                     }
 142  0
                     return null;
 143  
                 }
 144  
 
 145  0
                 if ( logger.isDebugEnabled() )
 146  
                 {
 147  0
                     logger.debug( line );
 148  
                 }
 149  
             }
 150  
 
 151  0
             return path;
 152  
         }
 153  0
         catch ( CommandLineException e )
 154  
         {
 155  0
             if ( logger.isErrorEnabled() )
 156  
             {
 157  0
                 logger.error( e );
 158  
             }
 159  0
             throw new RuntimeException( e.getLocalizedMessage() );
 160  
         }
 161  0
         catch ( IOException e )
 162  
         {
 163  0
             if ( logger.isErrorEnabled() )
 164  
             {
 165  0
                 logger.error( e );
 166  
             }
 167  0
             throw new RuntimeException( e.getLocalizedMessage() );
 168  
         }
 169  
         finally
 170  
         {
 171  0
             IOUtil.close( isReader );
 172  0
             IOUtil.close( isReaderErr );
 173  
         }
 174  
     }
 175  
 }