1 package org.apache.maven.archiva.repository.metadata;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import java.util.ArrayList;
23 import org.apache.maven.archiva.model.ArchivaModelCloner;
24 import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
25 import org.apache.maven.archiva.model.SnapshotVersion;
27 import java.util.Iterator;
28 import java.util.List;
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.maven.archiva.model.Plugin;
33 * RepositoryMetadataMerge
37 public class RepositoryMetadataMerge
39 public static ArchivaRepositoryMetadata merge( final ArchivaRepositoryMetadata mainMetadata,
40 final ArchivaRepositoryMetadata sourceMetadata )
41 throws RepositoryMetadataException
43 if ( mainMetadata == null )
45 throw new RepositoryMetadataException( "Cannot merge a null main project." );
48 if ( sourceMetadata == null )
50 throw new RepositoryMetadataException( "Cannot copy to a null parent project." );
53 ArchivaRepositoryMetadata merged = new ArchivaRepositoryMetadata();
55 merged.setGroupId( merge( mainMetadata.getGroupId(), sourceMetadata.getGroupId() ) );
56 merged.setArtifactId( merge(mainMetadata.getArtifactId(), sourceMetadata.getArtifactId()));
57 merged.setVersion( merge(mainMetadata.getVersion(), sourceMetadata.getVersion()) );
58 merged.setReleasedVersion( merge( mainMetadata.getReleasedVersion(), sourceMetadata.getReleasedVersion() ) );
59 merged.setSnapshotVersion( merge( mainMetadata.getSnapshotVersion(), sourceMetadata.getSnapshotVersion() ) );
60 merged.setAvailableVersions( mergeAvailableVersions( mainMetadata.getAvailableVersions(), sourceMetadata.getAvailableVersions() ) );
61 merged.setPlugins( mergePlugins( mainMetadata.getPlugins(), sourceMetadata.getPlugins() ) );
63 //Don't set if merge was not possible
64 long lastUpdated = mergeTimestamp( mainMetadata.getLastUpdated(), sourceMetadata.getLastUpdated());
67 merged.setLastUpdated( Long.toString(lastUpdated) );
73 private static boolean empty( String val )
80 return ( val.trim().length() <= 0 );
83 private static long mergeTimestamp(String mainTimestamp, String sourceTimestamp)
85 if (sourceTimestamp == null && mainTimestamp != null)
87 return convertTimestampToLong(mainTimestamp);
90 if (mainTimestamp == null && sourceTimestamp != null)
92 return convertTimestampToLong(sourceTimestamp);
95 if (sourceTimestamp == null && mainTimestamp == null)
100 return mergeTimestamp(convertTimestampToLong(mainTimestamp), convertTimestampToLong(sourceTimestamp));
103 private static long mergeTimestamp(long mainTimestamp, long sourceTimestamp)
105 return Math.max( mainTimestamp, sourceTimestamp );
108 private static SnapshotVersion merge( SnapshotVersion mainSnapshotVersion, SnapshotVersion sourceSnapshotVersion )
110 if ( sourceSnapshotVersion == null )
112 return mainSnapshotVersion;
115 if ( mainSnapshotVersion == null )
117 return ArchivaModelCloner.clone( sourceSnapshotVersion );
120 SnapshotVersion merged = new SnapshotVersion();
122 long mainSnapshotLastUpdated = convertTimestampToLong(mainSnapshotVersion.getTimestamp());
123 long sourceSnapshotLastUpdated = convertTimestampToLong(sourceSnapshotVersion.getTimestamp());
125 long lastUpdated = mergeTimestamp(mainSnapshotLastUpdated, sourceSnapshotLastUpdated);
127 if (lastUpdated == mainSnapshotLastUpdated)
129 merged.setTimestamp(mainSnapshotVersion.getTimestamp());
130 merged.setBuildNumber(mainSnapshotVersion.getBuildNumber());
134 merged.setTimestamp(sourceSnapshotVersion.getTimestamp());
135 merged.setBuildNumber(sourceSnapshotVersion.getBuildNumber());
141 private static long convertTimestampToLong(String timestamp)
143 if (timestamp == null)
148 return getLongFromTimestampSafely(StringUtils.replace(timestamp, ".", ""));
151 private static long getLongFromTimestampSafely( String timestampString )
155 return Long.parseLong(timestampString);
157 catch (NumberFormatException e)
163 private static String merge( String main, String source )
165 if ( empty( main ) && !empty( source ) )
173 private static List mergePlugins(List mainPlugins, List sourcePlugins)
175 if ( sourcePlugins == null )
180 if ( mainPlugins == null )
182 return clonePlugins( sourcePlugins );
185 List merged = clonePlugins( mainPlugins );
187 Iterator it = sourcePlugins.iterator();
188 while ( it.hasNext() )
190 Plugin plugin = (Plugin) it.next();
191 if ( !merged.contains( plugin ) )
193 merged.add( plugin );
201 * Clones a list of plugins.
203 * This method exists because ArchivaModelCloner.clonePlugins()
204 * only works with artifact references.
207 * @return list of cloned plugins
209 private static List<Plugin> clonePlugins(List<Plugin> plugins)
216 ArrayList result = new ArrayList();
218 for (Plugin plugin : plugins)
220 Plugin clonedPlugin = new Plugin();
221 clonedPlugin.setArtifactId(plugin.getArtifactId());
222 clonedPlugin.setModelEncoding(plugin.getModelEncoding());
223 clonedPlugin.setName(plugin.getName());
224 clonedPlugin.setPrefix(plugin.getPrefix());
231 private static List mergeAvailableVersions( List mainAvailableVersions, List sourceAvailableVersions )
233 if ( sourceAvailableVersions == null )
235 return mainAvailableVersions;
238 if ( mainAvailableVersions == null )
240 return ArchivaModelCloner.cloneAvailableVersions( sourceAvailableVersions );
243 List merged = ArchivaModelCloner.cloneAvailableVersions( mainAvailableVersions );
245 Iterator it = sourceAvailableVersions.iterator();
246 while ( it.hasNext() )
248 String sourceVersion = (String) it.next();
249 if ( !merged.contains( sourceVersion ) )
251 merged.add( sourceVersion );