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.metadata;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.Reader;
24  import java.io.Writer;
25  import java.util.List;
26  
27  import org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Reader;
28  import org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Writer;
29  import org.codehaus.plexus.util.FileUtils;
30  import org.codehaus.plexus.util.ReaderFactory;
31  import org.codehaus.plexus.util.WriterFactory;
32  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  /**
37   * Utils for dealing with LocalScmMetadata.
38   *
39   * @author <a href="mailto:arne@degenring.de">Arne Degenring</a>
40   */
41  public class LocalScmMetadataUtils {
42      /**
43       * The name of the metadata file.
44       */
45      public static final String FILENAME = ".maven-scm-local";
46  
47      private static final Logger LOGGER = LoggerFactory.getLogger(LocalScmMetadataUtils.class);
48  
49      /**
50       * Builds LocalScmMetadata based on contents of repository.
51       *
52       * @param repository
53       * @return TODO
54       * @throws IOException if any
55       */
56      public LocalScmMetadata buildMetadata(File repository) throws IOException {
57          @SuppressWarnings("unchecked")
58          List<String> repoFilenames = FileUtils.getFileNames(repository.getAbsoluteFile(), "**", null, false);
59          LocalScmMetadata metadata = new LocalScmMetadata();
60          metadata.setRepositoryFileNames(repoFilenames);
61          return metadata;
62      }
63  
64      /**
65       * Writes metadata file.
66       *
67       * @param destinationDir
68       * @param metadata
69       * @throws IOException if any
70       */
71      public void writeMetadata(File destinationDir, LocalScmMetadata metadata) throws IOException {
72          File metadataFile = new File(destinationDir, FILENAME);
73          metadataFile.createNewFile();
74  
75          try (Writer writer = WriterFactory.newXmlWriter(metadataFile)) {
76              new LocalScmMetadataXpp3Writer().write(writer, metadata);
77          }
78      }
79  
80      /**
81       * Reads metadata file from given directory.
82       *
83       * @param dir the directory that should contain the metadata file
84       * @return LocalScmMetadata or <code>null</code> in case of problems
85       */
86      public LocalScmMetadata readMetadata(File dir) {
87          File metadataFile = new File(dir, FILENAME);
88          if (!metadataFile.exists()) {
89              return null;
90          }
91  
92          try (Reader reader = ReaderFactory.newXmlReader(metadataFile)) {
93              LocalScmMetadata result = new LocalScmMetadataXpp3Reader().read(reader);
94              return result;
95          } catch (XmlPullParserException e) {
96              if (LOGGER.isWarnEnabled()) {
97                  LOGGER.warn("Could not interpret .maven-scm-local - ignoring", e);
98              }
99              return null;
100         } catch (IOException e) {
101             if (LOGGER.isWarnEnabled()) {
102                 LOGGER.warn("Could not read .maven-scm-local - ignoring", e);
103             }
104             return null;
105         }
106     }
107 }