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