View Javadoc

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