View Javadoc
1   package org.apache.maven.scm.provider.starteam.command.checkout;
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.ScmFile;
23  import org.apache.maven.scm.ScmFileStatus;
24  import org.apache.maven.scm.log.ScmLogger;
25  import org.apache.maven.scm.provider.starteam.command.StarteamCommandLineUtils;
26  import org.codehaus.plexus.util.cli.StreamConsumer;
27  
28  import java.io.File;
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  /**
33   * @author <a href="mailto:dantran@gmail.com">Dan T. Tran</a>
34   * @author Olivier Lamy
35   *
36   */
37  public class StarteamCheckOutConsumer
38      implements StreamConsumer
39  {
40      private ScmLogger logger;
41  
42      private String workingDirectory;
43  
44      private String currentDir = "";
45  
46      private List<ScmFile> files = new ArrayList<ScmFile>();
47  
48      /**
49       * Marks current directory data
50       */
51      private static final String DIR_MARKER = "(working dir: ";
52  
53      /**
54       * Marks current file data
55       */
56      private static final String CHECKOUT_MARKER = ": checked out";
57  
58      /**
59       * Marks skipped file during update
60       */
61      private static final String SKIPPED_MARKER = ": skipped";
62  
63      public StarteamCheckOutConsumer( ScmLogger logger, File workingDirectory )
64      {
65          this.logger = logger;
66  
67          this.workingDirectory = workingDirectory.getPath().replace( '\\', '/' );
68      }
69  
70      /** {@inheritDoc} */
71      public void consumeLine( String line )
72      {
73          if ( logger.isDebugEnabled() )
74          {
75              logger.debug( line );
76          }
77  
78          int pos = 0;
79  
80          if ( ( pos = line.indexOf( CHECKOUT_MARKER ) ) != -1 )
81          {
82              processCheckedOutFile( line, pos );
83          }
84          else if ( ( pos = line.indexOf( DIR_MARKER ) ) != -1 )
85          {
86              processDirectory( line, pos );
87          }
88          else if ( ( pos = line.indexOf( CHECKOUT_MARKER ) ) != -1 )
89          {
90              processCheckedOutFile( line, pos );
91          }
92          else if ( ( pos = line.indexOf( SKIPPED_MARKER ) ) != -1 )
93          {
94              processSkippedFile( line, pos );
95          }
96          else
97          {
98              if ( logger.isWarnEnabled() )
99              {
100                 logger.warn( "Unknown checkout ouput: " + line );
101             }
102         }
103     }
104 
105     public List<ScmFile> getCheckedOutFiles()
106     {
107         return files;
108     }
109 
110     private void processDirectory( String line, int pos )
111     {
112         String dirPath = line.substring( pos + DIR_MARKER.length(), line.length() - 1 ).replace( '\\', '/' );
113 
114         try
115         {
116             this.currentDir = StarteamCommandLineUtils.getRelativeChildDirectory( this.workingDirectory, dirPath );
117         }
118         catch ( IllegalStateException e )
119         {
120             String error = "Working and checkout directories are not on the same tree";
121 
122             if ( logger.isErrorEnabled() )
123             {
124                 logger.error( error );
125                 logger.error( "Working directory: " + workingDirectory );
126                 logger.error( "Checked out directory: " + dirPath );
127             }
128 
129             throw new IllegalStateException( error );
130         }
131 
132     }
133 
134     private void processCheckedOutFile( String line, int pos )
135     {
136         String checkedOutFilePath = this.currentDir + "/" + line.substring( 0, pos );
137 
138         this.files.add( new ScmFile( checkedOutFilePath, ScmFileStatus.CHECKED_OUT ) );
139 
140         if ( logger.isInfoEnabled() )
141         {
142             logger.info( "Checked out: " + checkedOutFilePath );
143         }
144     }
145 
146     private void processSkippedFile( String line, int pos )
147     {
148         String skippedFilePath = this.currentDir + "/" + line.substring( 0, pos );
149 
150         if ( logger.isDebugEnabled() )
151         {
152             logger.debug( "Skipped: " + skippedFilePath );
153         }
154     }
155 
156 }