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