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