View Javadoc
1   package org.apache.maven.repository.internal;
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 java.io.File;
23  import java.io.IOException;
24  import java.io.Reader;
25  import java.io.Writer;
26  import java.util.Collections;
27  import java.util.Map;
28  
29  import org.apache.maven.artifact.repository.metadata.Metadata;
30  import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
31  import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
32  import org.codehaus.plexus.util.IOUtil;
33  import org.codehaus.plexus.util.ReaderFactory;
34  import org.codehaus.plexus.util.WriterFactory;
35  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
36  import org.eclipse.aether.RepositoryException;
37  import org.eclipse.aether.metadata.AbstractMetadata;
38  import org.eclipse.aether.metadata.MergeableMetadata;
39  
40  /**
41   * @author Benjamin Bentmann
42   */
43  abstract class MavenMetadata
44      extends AbstractMetadata
45      implements MergeableMetadata
46  {
47  
48      static final String MAVEN_METADATA_XML = "maven-metadata.xml";
49  
50      private final File file;
51  
52      protected Metadata metadata;
53  
54      private boolean merged;
55  
56      protected MavenMetadata( Metadata metadata, File file )
57      {
58          this.metadata = metadata;
59          this.file = file;
60      }
61  
62      public String getType()
63      {
64          return MAVEN_METADATA_XML;
65      }
66  
67      public File getFile()
68      {
69          return file;
70      }
71  
72      public void merge( File existing, File result )
73          throws RepositoryException
74      {
75          Metadata recessive = read( existing );
76  
77          merge( recessive );
78  
79          write( result, metadata );
80  
81          merged = true;
82      }
83  
84      public boolean isMerged()
85      {
86          return merged;
87      }
88  
89      protected abstract void merge( Metadata recessive );
90  
91      static Metadata read( File metadataFile )
92          throws RepositoryException
93      {
94          if ( metadataFile.length() <= 0 )
95          {
96              return new Metadata();
97          }
98  
99          Reader reader = null;
100         try
101         {
102             reader = ReaderFactory.newXmlReader( metadataFile );
103             return new MetadataXpp3Reader().read( reader, false );
104         }
105         catch ( IOException e )
106         {
107             throw new RepositoryException( "Could not read metadata " + metadataFile + ": " + e.getMessage(), e );
108         }
109         catch ( XmlPullParserException e )
110         {
111             throw new RepositoryException( "Could not parse metadata " + metadataFile + ": " + e.getMessage(), e );
112         }
113         finally
114         {
115             IOUtil.close( reader );
116         }
117     }
118 
119     private void write( File metadataFile, Metadata metadata )
120         throws RepositoryException
121     {
122         Writer writer = null;
123         try
124         {
125             metadataFile.getParentFile().mkdirs();
126             writer = WriterFactory.newXmlWriter( metadataFile );
127             new MetadataXpp3Writer().write( writer, metadata );
128         }
129         catch ( IOException e )
130         {
131             throw new RepositoryException( "Could not write metadata " + metadataFile + ": " + e.getMessage(), e );
132         }
133         finally
134         {
135             IOUtil.close( writer );
136         }
137     }
138 
139     public Map<String, String> getProperties()
140     {
141         return Collections.emptyMap();
142     }
143 
144     @Override
145     public org.eclipse.aether.metadata.Metadata setProperties( Map<String, String> properties )
146     {
147         return this;
148     }
149 
150 }