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.text.ParseException;
23 import java.text.SimpleDateFormat;
24 import java.util.ArrayList;
25 import java.util.Calendar;
26 import java.util.Date;
27 import org.apache.maven.archiva.model.ArchivaModelCloner;
28 import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
29 import org.apache.maven.archiva.model.SnapshotVersion;
31 import java.util.Iterator;
32 import java.util.List;
33 import org.apache.commons.lang.StringUtils;
34 import org.apache.commons.lang.time.DateUtils;
35 import org.apache.maven.archiva.model.Plugin;
38 * RepositoryMetadataMerge
40 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
43 public class RepositoryMetadataMerge
45 public static ArchivaRepositoryMetadata merge( final ArchivaRepositoryMetadata mainMetadata,
46 final ArchivaRepositoryMetadata sourceMetadata )
47 throws RepositoryMetadataException
49 if ( mainMetadata == null )
51 throw new RepositoryMetadataException( "Cannot merge a null main project." );
54 if ( sourceMetadata == null )
56 throw new RepositoryMetadataException( "Cannot copy to a null parent project." );
59 ArchivaRepositoryMetadata merged = new ArchivaRepositoryMetadata();
61 merged.setGroupId( merge( mainMetadata.getGroupId(), sourceMetadata.getGroupId() ) );
62 merged.setArtifactId( merge(mainMetadata.getArtifactId(), sourceMetadata.getArtifactId()));
63 merged.setVersion( merge(mainMetadata.getVersion(), sourceMetadata.getVersion()) );
64 merged.setReleasedVersion( merge( mainMetadata.getReleasedVersion(), sourceMetadata.getReleasedVersion() ) );
65 merged.setSnapshotVersion( merge( mainMetadata.getSnapshotVersion(), sourceMetadata.getSnapshotVersion() ) );
66 merged.setAvailableVersions( mergeAvailableVersions( mainMetadata.getAvailableVersions(), sourceMetadata.getAvailableVersions() ) );
67 merged.setPlugins( mergePlugins( mainMetadata.getPlugins(), sourceMetadata.getPlugins() ) );
69 //Don't set if merge was not possible
70 long lastUpdated = mergeTimestamp( mainMetadata.getLastUpdated(), sourceMetadata.getLastUpdated());
73 merged.setLastUpdated( Long.toString(lastUpdated) );
79 private static boolean empty( String val )
86 return ( val.trim().length() <= 0 );
89 private static long mergeTimestamp(String mainTimestamp, String sourceTimestamp)
91 if (sourceTimestamp == null && mainTimestamp != null)
93 return convertTimestampToLong(mainTimestamp);
96 if (mainTimestamp == null && sourceTimestamp != null)
98 return convertTimestampToLong(sourceTimestamp);
101 if (sourceTimestamp == null && mainTimestamp == null)
106 return mergeTimestamp(convertTimestampToLong(mainTimestamp), convertTimestampToLong(sourceTimestamp));
109 private static long mergeTimestamp(long mainTimestamp, long sourceTimestamp)
111 return Math.max( mainTimestamp, sourceTimestamp );
114 private static SnapshotVersion merge( SnapshotVersion mainSnapshotVersion, SnapshotVersion sourceSnapshotVersion )
116 if ( sourceSnapshotVersion == null )
118 return mainSnapshotVersion;
121 if ( mainSnapshotVersion == null )
123 return ArchivaModelCloner.clone( sourceSnapshotVersion );
126 SnapshotVersion merged = new SnapshotVersion();
128 long mainSnapshotLastUpdated = convertTimestampToLong(mainSnapshotVersion.getTimestamp());
129 long sourceSnapshotLastUpdated = convertTimestampToLong(sourceSnapshotVersion.getTimestamp());
131 long lastUpdated = mergeTimestamp(mainSnapshotLastUpdated, sourceSnapshotLastUpdated);
133 if (lastUpdated == mainSnapshotLastUpdated)
135 merged.setTimestamp(mainSnapshotVersion.getTimestamp());
136 merged.setBuildNumber(mainSnapshotVersion.getBuildNumber());
140 merged.setTimestamp(sourceSnapshotVersion.getTimestamp());
141 merged.setBuildNumber(sourceSnapshotVersion.getBuildNumber());
147 private static long convertTimestampToLong(String timestamp)
149 if (timestamp == null)
154 return getLongFromTimestampSafely(StringUtils.replace(timestamp, ".", ""));
157 private static long getLongFromTimestampSafely( String timestampString )
161 return Long.parseLong(timestampString);
163 catch (NumberFormatException e)
169 private static String merge( String main, String source )
171 if ( empty( main ) && !empty( source ) )
179 private static List mergePlugins(List mainPlugins, List sourcePlugins)
181 if ( sourcePlugins == null )
186 if ( mainPlugins == null )
188 return clonePlugins( sourcePlugins );
191 List merged = clonePlugins( mainPlugins );
193 Iterator it = sourcePlugins.iterator();
194 while ( it.hasNext() )
196 Plugin plugin = (Plugin) it.next();
197 if ( !merged.contains( plugin ) )
199 merged.add( plugin );
207 * Clones a list of plugins.
209 * This method exists because ArchivaModelCloner.clonePlugins()
210 * only works with artifact references.
213 * @return list of cloned plugins
215 private static List<Plugin> clonePlugins(List<Plugin> plugins)
222 ArrayList result = new ArrayList();
224 for (Plugin plugin : plugins)
226 Plugin clonedPlugin = new Plugin();
227 clonedPlugin.setArtifactId(plugin.getArtifactId());
228 clonedPlugin.setModelEncoding(plugin.getModelEncoding());
229 clonedPlugin.setName(plugin.getName());
230 clonedPlugin.setPrefix(plugin.getPrefix());
237 private static List mergeAvailableVersions( List mainAvailableVersions, List sourceAvailableVersions )
239 if ( sourceAvailableVersions == null )
241 return mainAvailableVersions;
244 if ( mainAvailableVersions == null )
246 return ArchivaModelCloner.cloneAvailableVersions( sourceAvailableVersions );
249 List merged = ArchivaModelCloner.cloneAvailableVersions( mainAvailableVersions );
251 Iterator it = sourceAvailableVersions.iterator();
252 while ( it.hasNext() )
254 String sourceVersion = (String) it.next();
255 if ( !merged.contains( sourceVersion ) )
257 merged.add( sourceVersion );