View Javadoc

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      private ScmLogger logger = null;
51  
52      private PerforceScmProviderRepository repo = null;
53  
54      public PerforceWhereCommand( ScmLogger log, PerforceScmProviderRepository repos )
55      {
56          logger = log;
57          repo = repos;
58      }
59  
60      public String getDepotLocation( File file )
61      {
62          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          if ( !PerforceScmProvider.isLive() )
73          {
74              return null;
75          }
76  
77          InputStreamReader isReader = null;
78          InputStreamReader isReaderErr = null;
79          try
80          {
81              Commandline command = PerforceScmProvider.createP4Command( repo, null );
82              command.createArg().setValue( "where" );
83              command.createArg().setValue( filepath );
84              if ( logger.isDebugEnabled() )
85              {
86                  logger.debug( PerforceScmProvider.clean( "Executing: " + command.toString() ) );
87              }
88              Process proc = command.execute();
89              isReader = new InputStreamReader( proc.getInputStream() );
90              isReaderErr = new InputStreamReader( proc.getErrorStream() );
91              BufferedReader br = new BufferedReader( isReader );
92              BufferedReader brErr = new BufferedReader( isReaderErr );
93              String line;
94              String path = null;
95              while ( ( line = br.readLine() ) != null )
96              {
97                  if ( line.indexOf( "not in client view" ) != -1 )
98                  {
99                      // uh oh, something bad is happening
100                     if ( logger.isErrorEnabled() )
101                     {
102                         logger.error( line );
103                     }
104                     return null;
105                 }
106                 if ( line.indexOf( "is not under" ) != -1 )
107                 {
108                     // uh oh, something bad is happening
109                     if ( logger.isErrorEnabled() )
110                     {
111                         logger.error( line );
112                     }
113                     return null;
114                 }
115 
116                 if ( logger.isDebugEnabled() )
117                 {
118                     logger.debug( line );
119                 }
120                 // verify that "//" appears twice in the line
121                 path = line.substring( 0, line.lastIndexOf( "//" ) - 1 );
122             }
123             // Check for errors
124             while ( ( line = brErr.readLine() ) != null )
125             {
126                 if ( line.indexOf( "not in client view" ) != -1 )
127                 {
128                     // uh oh, something bad is happening
129                     if ( logger.isErrorEnabled() )
130                     {
131                         logger.error( line );
132                     }
133                     return null;
134                 }
135                 if ( line.indexOf( "is not under" ) != -1 )
136                 {
137                     // uh oh, something bad is happening
138                     if ( logger.isErrorEnabled() )
139                     {
140                         logger.error( line );
141                     }
142                     return null;
143                 }
144 
145                 if ( logger.isDebugEnabled() )
146                 {
147                     logger.debug( line );
148                 }
149             }
150 
151             return path;
152         }
153         catch ( CommandLineException e )
154         {
155             if ( logger.isErrorEnabled() )
156             {
157                 logger.error( e );
158             }
159             throw new RuntimeException( e.getLocalizedMessage() );
160         }
161         catch ( IOException e )
162         {
163             if ( logger.isErrorEnabled() )
164             {
165                 logger.error( e );
166             }
167             throw new RuntimeException( e.getLocalizedMessage() );
168         }
169         finally
170         {
171             IOUtil.close( isReader );
172             IOUtil.close( isReaderErr );
173         }
174     }
175 }