This project has retired. For details please refer to its Attic page.
RepositoryMetadataWriter xref
View Javadoc
1   package org.apache.archiva.repository.metadata.base;
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.archiva.model.ArchivaRepositoryMetadata;
23  import org.apache.archiva.model.Plugin;
24  import org.apache.archiva.repository.metadata.RepositoryMetadataException;
25  import org.apache.archiva.repository.storage.StorageAsset;
26  import org.apache.archiva.xml.XMLException;
27  import org.apache.archiva.xml.XMLWriter;
28  import org.apache.archiva.xml.XmlUtil;
29  import org.apache.commons.collections4.CollectionUtils;
30  import org.apache.commons.lang3.StringUtils;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  import org.w3c.dom.Document;
34  import org.w3c.dom.Element;
35  
36  import javax.xml.parsers.ParserConfigurationException;
37  import java.io.IOException;
38  import java.io.OutputStreamWriter;
39  import java.io.Writer;
40  import java.util.Collections;
41  import java.util.Comparator;
42  import java.util.Iterator;
43  import java.util.List;
44  
45  /**
46   * RepositoryMetadataWriter
47   */
48  public class RepositoryMetadataWriter
49  {
50      private static final Logger log = LoggerFactory.getLogger(RepositoryMetadataWriter.class);
51  
52      public static void write( ArchivaRepositoryMetadata metadata, StorageAsset outputFile )
53          throws RepositoryMetadataException
54      {
55          boolean thrown = false;
56          try (OutputStreamWriter writer = new OutputStreamWriter( outputFile.getWriteStream(true)))
57          {
58              write( metadata, writer );
59              writer.flush();
60          }
61          catch ( IOException e )
62          {
63              thrown = true;
64              throw new RepositoryMetadataException(
65                  "Unable to write metadata file: " + outputFile.getPath() + " - " + e.getMessage(), e );
66          }
67          finally
68          {
69              if ( thrown )
70              {
71                  try {
72                      outputFile.getStorage().removeAsset(outputFile);
73                  } catch (IOException e) {
74                      log.error("Could not remove asset {}", outputFile);
75                  }
76              }
77          }
78      }
79  
80      public static void write( ArchivaRepositoryMetadata metadata, Writer writer )
81          throws RepositoryMetadataException
82      {
83          Document doc = null;
84          try {
85              doc = XmlUtil.createDocument();
86          } catch (ParserConfigurationException e) {
87              throw new RepositoryMetadataException("Could not create xml doc " + e.getMessage(), e);
88          }
89  
90          Element root = doc.createElement( "metadata" );
91          doc.appendChild(root);
92  
93          addOptionalElementText( root, "groupId", metadata.getGroupId() );
94          addOptionalElementText( root, "artifactId", metadata.getArtifactId() );
95          addOptionalElementText( root, "version", metadata.getVersion() );
96  
97          if ( CollectionUtils.isNotEmpty( metadata.getPlugins() ) )
98          {
99              Element plugins = XmlUtil.addChild(root, "plugins" );
100 
101             List<Plugin> pluginList = metadata.getPlugins();
102             Collections.sort( pluginList, PluginComparator.INSTANCE );
103 
104             for ( Plugin plugin : metadata.getPlugins() )
105             {
106                 Element p = XmlUtil.addChild(plugins, "plugin" );
107                 XmlUtil.addChild(doc, p, "prefix" ).setTextContent( plugin.getPrefix() );
108                 XmlUtil.addChild(doc, p, "artifactId" ).setTextContent( plugin.getArtifactId() );
109                 addOptionalElementText( p, "name", plugin.getName() );
110             }
111         }
112 
113         if ( CollectionUtils.isNotEmpty( metadata.getAvailableVersions() ) //
114             || StringUtils.isNotBlank( metadata.getReleasedVersion() ) //
115             || StringUtils.isNotBlank( metadata.getLatestVersion() ) //
116             || StringUtils.isNotBlank( metadata.getLastUpdated() ) //
117             || ( metadata.getSnapshotVersion() != null ) )
118         {
119             Element versioning = XmlUtil.addChild(root, "versioning" );
120 
121             addOptionalElementText( versioning, "latest", metadata.getLatestVersion() );
122             addOptionalElementText( versioning, "release", metadata.getReleasedVersion() );
123 
124             if ( metadata.getSnapshotVersion() != null )
125             {
126                 Element snapshot = XmlUtil.addChild(versioning, "snapshot" );
127                 String bnum = String.valueOf( metadata.getSnapshotVersion().getBuildNumber() );
128                 addOptionalElementText( snapshot, "buildNumber", bnum );
129                 addOptionalElementText( snapshot, "timestamp", metadata.getSnapshotVersion().getTimestamp() );
130             }
131 
132             if ( CollectionUtils.isNotEmpty( metadata.getAvailableVersions() ) )
133             {
134                 Element versions = XmlUtil.addChild(versioning, "versions" );
135                 Iterator<String> it = metadata.getAvailableVersions().iterator();
136                 while ( it.hasNext() )
137                 {
138                     String version = it.next();
139                     XmlUtil.addChild(versions, "version" ).setTextContent( version );
140                 }
141             }
142 
143             addOptionalElementText( versioning, "lastUpdated", metadata.getLastUpdated() );
144         }
145 
146         try
147         {
148             XMLWriter.write( doc, writer );
149         }
150         catch ( XMLException e )
151         {
152             throw new RepositoryMetadataException( "Unable to write xml contents to writer: " + e.getMessage(), e );
153         }
154     }
155 
156     private static void addOptionalElementText( Element elem, String elemName, String text )
157     {
158         if ( StringUtils.isBlank( text ) )
159         {
160             return;
161         }
162 
163         XmlUtil.addChild(elem, elemName ).setTextContent( text );
164     }
165 
166     private static class PluginComparator
167         implements Comparator<Plugin>
168     {
169         private static final PluginComparator INSTANCE = new PluginComparator();
170 
171         @Override
172         public int compare( Plugin="../../../../../../org/apache/archiva/model/Plugin.html#Plugin">Plugin plugin, Plugin plugin2 )
173         {
174             if ( plugin.getPrefix() != null && plugin2.getPrefix() != null )
175             {
176                 return plugin.getPrefix().compareTo( plugin2.getPrefix() );
177             }
178             if ( plugin.getName() != null && plugin2.getName() != null )
179             {
180                 return plugin.getName().compareTo( plugin2.getName() );
181             }
182             // we assume artifactId is not null which sounds good :-)
183             return plugin.getArtifactId().compareTo( plugin2.getArtifactId() );
184         }
185     }
186 }