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   */
43  public class LocalCheckOutCommand extends AbstractCheckOutCommand implements LocalCommand {
44      /** {@inheritDoc} */
45      protected CheckOutScmResult executeCheckOutCommand(
46              ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version, boolean recursive, boolean shallow)
47              throws ScmException {
48          LocalScmProviderRepository repository = (LocalScmProviderRepository) repo;
49  
50          if (version != null) {
51              throw new ScmException("The local scm doesn't support tags.");
52          }
53  
54          File root = new File(repository.getRoot());
55  
56          String module = repository.getModule();
57  
58          File source = new File(root, module);
59  
60          File baseDestination = fileSet.getBasedir();
61  
62          if (!root.exists()) {
63              throw new ScmException("The base directory doesn't exist (" + root.getAbsolutePath() + ").");
64          }
65  
66          if (!source.exists()) {
67              throw new ScmException("The module directory doesn't exist (" + source.getAbsolutePath() + ").");
68          }
69  
70          List<ScmFile> checkedOutFiles;
71  
72          try {
73              if (baseDestination.exists()) {
74                  FileUtils.deleteDirectory(baseDestination);
75              }
76  
77              if (!baseDestination.mkdirs()) {
78                  throw new ScmException(
79                          "Could not create destination directory '" + baseDestination.getAbsolutePath() + "'.");
80              }
81  
82              if (logger.isInfoEnabled()) {
83                  logger.info("Checking out '" + source.getAbsolutePath() + "' to '" + baseDestination.getAbsolutePath()
84                          + "'.");
85              }
86  
87              List<File> fileList;
88  
89              if (fileSet.getFileList().isEmpty()) {
90                  fileList = FileUtils.getFiles(source.getAbsoluteFile(), "**", null);
91              } else {
92                  fileList = fileSet.getFileList();
93              }
94  
95              checkedOutFiles = checkOut(source, baseDestination, fileList, repository.getModule());
96  
97              // write metadata file
98              LocalScmMetadataUtils metadataUtils = new LocalScmMetadataUtils();
99              metadataUtils.writeMetadata(baseDestination, metadataUtils.buildMetadata(source));
100         } catch (IOException ex) {
101             throw new ScmException("Error while checking out the files.", ex);
102         }
103 
104         return new LocalCheckOutScmResult(null, checkedOutFiles);
105     }
106 
107     private List<ScmFile> checkOut(File source, File baseDestination, List<File> files, String module)
108             throws ScmException, IOException {
109         String sourcePath = source.getAbsolutePath();
110 
111         List<ScmFile> checkedOutFiles = new ArrayList<>();
112 
113         for (File file : files) {
114             String dest = file.getAbsolutePath();
115 
116             dest = dest.substring(sourcePath.length() + 1);
117 
118             File destination = new File(baseDestination, dest);
119 
120             destination = destination.getParentFile();
121 
122             if (!destination.exists() && !destination.mkdirs()) {
123                 throw new ScmException(
124                         "Could not create destination directory '" + destination.getAbsolutePath() + "'.");
125             }
126 
127             FileUtils.copyFileToDirectory(file, destination);
128 
129             File parent = file.getParentFile();
130 
131             String fileName = "/" + module + "/" + dest;
132 
133             checkedOutFiles.add(new ScmFile(fileName, ScmFileStatus.CHECKED_OUT));
134         }
135 
136         return checkedOutFiles;
137     }
138 }