View Javadoc
1   package org.apache.maven.scm.provider.local.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 java.io.File;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.maven.scm.ScmException;
28  import org.apache.maven.scm.ScmFile;
29  import org.apache.maven.scm.ScmFileSet;
30  import org.apache.maven.scm.ScmFileStatus;
31  import org.apache.maven.scm.ScmVersion;
32  import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
33  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
34  import org.apache.maven.scm.provider.ScmProviderRepository;
35  import org.apache.maven.scm.provider.local.command.LocalCommand;
36  import org.apache.maven.scm.provider.local.metadata.LocalScmMetadataUtils;
37  import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
38  import org.codehaus.plexus.util.FileUtils;
39  
40  /**
41   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
42   *
43   */
44  public class LocalCheckOutCommand
45      extends AbstractCheckOutCommand
46      implements LocalCommand
47  {
48      /** {@inheritDoc} */
49      protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repo, ScmFileSet fileSet,
50                                                          ScmVersion version, boolean recursive )
51          throws ScmException
52      {
53          LocalScmProviderRepository repository = (LocalScmProviderRepository) repo;
54  
55          if ( version != null )
56          {
57              throw new ScmException( "The local scm doesn't support tags." );
58          }
59  
60          File root = new File( repository.getRoot() );
61  
62          String module = repository.getModule();
63  
64          File source = new File( root, module );
65  
66          File baseDestination = fileSet.getBasedir();
67  
68          if ( !root.exists() )
69          {
70              throw new ScmException( "The base directory doesn't exist (" + root.getAbsolutePath() + ")." );
71          }
72  
73          if ( !source.exists() )
74          {
75              throw new ScmException( "The module directory doesn't exist (" + source.getAbsolutePath() + ")." );
76          }
77  
78          List<ScmFile> checkedOutFiles;
79  
80          try
81          {
82              if ( baseDestination.exists() )
83              {
84                  FileUtils.deleteDirectory( baseDestination );
85              }
86  
87              if ( !baseDestination.mkdirs() )
88              {
89                  throw new ScmException(
90                      "Could not create destination directory '" + baseDestination.getAbsolutePath() + "'." );
91              }
92  
93              if ( getLogger().isInfoEnabled() )
94              {
95                  getLogger().info(
96                                    "Checking out '" + source.getAbsolutePath() + "' to '"
97                                        + baseDestination.getAbsolutePath() + "'." );
98              }
99  
100             List<File> fileList;
101 
102             if ( fileSet.getFileList().isEmpty() )
103             {
104                 @SuppressWarnings( "unchecked" )
105                 List<File> files = FileUtils.getFiles( source.getAbsoluteFile(), "**", null ); 
106                 fileList = files;
107             }
108             else
109             {
110                 fileList = fileSet.getFileList();
111             }
112 
113             checkedOutFiles = checkOut( source, baseDestination, fileList, repository.getModule() );
114 
115             // write metadata file
116             LocalScmMetadataUtils metadataUtils = new LocalScmMetadataUtils( getLogger() );
117             metadataUtils.writeMetadata( baseDestination, metadataUtils.buildMetadata( source ) );
118         }
119         catch ( IOException ex )
120         {
121             throw new ScmException( "Error while checking out the files.", ex );
122         }
123 
124         return new LocalCheckOutScmResult( null, checkedOutFiles );
125     }
126 
127     private List<ScmFile> checkOut( File source, File baseDestination, List<File> files, String module )
128         throws ScmException, IOException
129     {
130         String sourcePath = source.getAbsolutePath();
131 
132         List<ScmFile> checkedOutFiles = new ArrayList<ScmFile>();
133 
134         for ( File file : files )
135         {
136             String dest = file.getAbsolutePath();
137 
138             dest = dest.substring( sourcePath.length() + 1 );
139 
140             File destination = new File( baseDestination, dest );
141 
142             destination = destination.getParentFile();
143 
144             if ( !destination.exists() && !destination.mkdirs() )
145             {
146                 throw new ScmException(
147                     "Could not create destination directory '" + destination.getAbsolutePath() + "'." );
148             }
149 
150             FileUtils.copyFileToDirectory( file, destination );
151 
152             File parent = file.getParentFile();
153 
154             // TODO: Add more excludes here
155             if ( parent != null && parent.getName().equals( "CVS" ) )
156             {
157                 continue;
158             }
159 
160             String fileName = "/" + module + "/" + dest;
161 
162             checkedOutFiles.add( new ScmFile( fileName, ScmFileStatus.CHECKED_OUT ) );
163         }
164 
165         return checkedOutFiles;
166     }
167 
168 
169 }