View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.local.command.checkout;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.maven.scm.ScmException;
27  import org.apache.maven.scm.ScmFile;
28  import org.apache.maven.scm.ScmFileSet;
29  import org.apache.maven.scm.ScmFileStatus;
30  import org.apache.maven.scm.ScmVersion;
31  import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
32  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
33  import org.apache.maven.scm.provider.ScmProviderRepository;
34  import org.apache.maven.scm.provider.local.command.LocalCommand;
35  import org.apache.maven.scm.provider.local.metadata.LocalScmMetadataUtils;
36  import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
37  import org.codehaus.plexus.util.FileUtils;
38  
39  /**
40   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
41   */
42  public class LocalCheckOutCommand extends AbstractCheckOutCommand implements LocalCommand {
43      /**
44       * {@inheritDoc}
45       */
46      protected CheckOutScmResult executeCheckOutCommand(
47              ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version, boolean recursive, boolean shallow)
48              throws ScmException {
49          LocalScmProviderRepository repository = (LocalScmProviderRepository) repo;
50  
51          if (version != null) {
52              throw new ScmException("The local scm doesn't support tags.");
53          }
54  
55          File root = new File(repository.getRoot());
56  
57          String module = repository.getModule();
58  
59          File source = new File(root, module);
60  
61          File baseDestination = fileSet.getBasedir();
62  
63          if (!root.exists()) {
64              throw new ScmException("The base directory doesn't exist (" + root.getAbsolutePath() + ").");
65          }
66  
67          if (!source.exists()) {
68              throw new ScmException("The module directory doesn't exist (" + source.getAbsolutePath() + ").");
69          }
70  
71          List<ScmFile> checkedOutFiles;
72  
73          try {
74              if (baseDestination.exists()) {
75                  FileUtils.deleteDirectory(baseDestination);
76              }
77  
78              if (!baseDestination.mkdirs()) {
79                  throw new ScmException(
80                          "Could not create destination directory '" + baseDestination.getAbsolutePath() + "'.");
81              }
82  
83              if (logger.isInfoEnabled()) {
84                  logger.info("Checking out '" + source.getAbsolutePath() + "' to '" + baseDestination.getAbsolutePath()
85                          + "'.");
86              }
87  
88              List<File> fileList;
89  
90              if (fileSet.getFileList().isEmpty()) {
91                  fileList = FileUtils.getFiles(source.getAbsoluteFile(), "**", null);
92              } else {
93                  fileList = fileSet.getFileList();
94              }
95  
96              checkedOutFiles = checkOut(source, baseDestination, fileList, repository.getModule());
97  
98              // write metadata file
99              LocalScmMetadataUtils metadataUtils = new LocalScmMetadataUtils();
100             metadataUtils.writeMetadata(baseDestination, metadataUtils.buildMetadata(source));
101         } catch (IOException ex) {
102             throw new ScmException("Error while checking out the files.", ex);
103         }
104 
105         return new LocalCheckOutScmResult(null, checkedOutFiles);
106     }
107 
108     private List<ScmFile> checkOut(File source, File baseDestination, List<File> files, String module)
109             throws ScmException, IOException {
110         String sourcePath = source.getAbsolutePath();
111 
112         List<ScmFile> checkedOutFiles = new ArrayList<>();
113 
114         for (File file : files) {
115             String dest = file.getAbsolutePath();
116 
117             dest = dest.substring(sourcePath.length() + 1);
118 
119             File destination = new File(baseDestination, dest);
120 
121             destination = destination.getParentFile();
122 
123             if (!destination.exists() && !destination.mkdirs()) {
124                 throw new ScmException(
125                         "Could not create destination directory '" + destination.getAbsolutePath() + "'.");
126             }
127 
128             FileUtils.copyFileToDirectory(file, destination);
129 
130             String fileName = "/" + module + "/" + dest;
131 
132             checkedOutFiles.add(new ScmFile(fileName, ScmFileStatus.CHECKED_OUT));
133         }
134 
135         return checkedOutFiles;
136     }
137 }