1 package org.apache.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 java.util.List;
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.archiva.model.ArchivaModelCloner;
27 import org.apache.archiva.model.ArchivaRepositoryMetadata;
28 import org.apache.archiva.model.Plugin;
29 import org.apache.archiva.model.SnapshotVersion;
32 * RepositoryMetadataMerge
36 public class RepositoryMetadataMerge
38 public static ArchivaRepositoryMetadata merge( final ArchivaRepositoryMetadata mainMetadata,
39 final ArchivaRepositoryMetadata sourceMetadata )
40 throws RepositoryMetadataException
42 if ( mainMetadata == null )
44 throw new RepositoryMetadataException( "Cannot merge a null main project." );
47 if ( sourceMetadata == null )
49 throw new RepositoryMetadataException( "Cannot copy to a null parent project." );
52 ArchivaRepositoryMetadata merged = new ArchivaRepositoryMetadata();
54 merged.setGroupId( merge( mainMetadata.getGroupId(), sourceMetadata.getGroupId() ) );
55 merged.setArtifactId( merge(mainMetadata.getArtifactId(), sourceMetadata.getArtifactId()));
56 merged.setVersion( merge(mainMetadata.getVersion(), sourceMetadata.getVersion()) );
57 merged.setReleasedVersion( merge( mainMetadata.getReleasedVersion(), sourceMetadata.getReleasedVersion() ) );
58 merged.setSnapshotVersion( merge( mainMetadata.getSnapshotVersion(), sourceMetadata.getSnapshotVersion() ) );
59 merged.setAvailableVersions( mergeAvailableVersions( mainMetadata.getAvailableVersions(), sourceMetadata.getAvailableVersions() ) );
60 merged.setPlugins( mergePlugins( mainMetadata.getPlugins(), sourceMetadata.getPlugins() ) );
62 //Don't set if merge was not possible
63 long lastUpdated = mergeTimestamp( mainMetadata.getLastUpdated(), sourceMetadata.getLastUpdated());
66 merged.setLastUpdated( Long.toString(lastUpdated) );
72 private static boolean empty( String val )
79 return ( val.trim().length() <= 0 );
82 private static long mergeTimestamp(String mainTimestamp, String sourceTimestamp)
84 if (sourceTimestamp == null && mainTimestamp != null)
86 return convertTimestampToLong(mainTimestamp);
89 if (mainTimestamp == null && sourceTimestamp != null)
91 return convertTimestampToLong(sourceTimestamp);
94 if (sourceTimestamp == null && mainTimestamp == null)
99 return mergeTimestamp(convertTimestampToLong(mainTimestamp), convertTimestampToLong(sourceTimestamp));
102 private static long mergeTimestamp(long mainTimestamp, long sourceTimestamp)
104 return Math.max( mainTimestamp, sourceTimestamp );
107 private static SnapshotVersion merge( SnapshotVersion mainSnapshotVersion, SnapshotVersion sourceSnapshotVersion )
109 if ( sourceSnapshotVersion == null )
111 return mainSnapshotVersion;
114 if ( mainSnapshotVersion == null )
116 return ArchivaModelCloner.clone( sourceSnapshotVersion );
119 SnapshotVersion merged = new SnapshotVersion();
121 long mainSnapshotLastUpdated = convertTimestampToLong(mainSnapshotVersion.getTimestamp());
122 long sourceSnapshotLastUpdated = convertTimestampToLong(sourceSnapshotVersion.getTimestamp());
124 long lastUpdated = mergeTimestamp(mainSnapshotLastUpdated, sourceSnapshotLastUpdated);
126 if (lastUpdated == mainSnapshotLastUpdated)
128 merged.setTimestamp(mainSnapshotVersion.getTimestamp());
129 merged.setBuildNumber(mainSnapshotVersion.getBuildNumber());
133 merged.setTimestamp(sourceSnapshotVersion.getTimestamp());
134 merged.setBuildNumber(sourceSnapshotVersion.getBuildNumber());
140 private static long convertTimestampToLong(String timestamp)
142 if (timestamp == null)
147 return getLongFromTimestampSafely(StringUtils.replace(timestamp, ".", ""));
150 private static long getLongFromTimestampSafely( String timestampString )
154 return Long.parseLong(timestampString);
156 catch (NumberFormatException e)
162 private static String merge( String main, String source )
164 if ( empty( main ) && !empty( source ) )
172 private static List<Plugin> mergePlugins(List<Plugin> mainPlugins, List<Plugin> sourcePlugins)
174 if ( sourcePlugins == null )
179 if ( mainPlugins == null )
181 return clonePlugins( sourcePlugins );
184 List<Plugin> merged = clonePlugins( mainPlugins );
186 for ( Plugin plugin : sourcePlugins )
188 if ( !merged.contains( plugin ) )
190 merged.add( plugin );
198 * Clones a list of plugins.
200 * This method exists because ArchivaModelCloner.clonePlugins()
201 * only works with artifact references.
204 * @return list of cloned plugins
206 private static List<Plugin> clonePlugins(List<Plugin> plugins)
213 List<Plugin> result = new ArrayList<Plugin>();
215 for (Plugin plugin : plugins)
217 Plugin clonedPlugin = new Plugin();
218 clonedPlugin.setArtifactId(plugin.getArtifactId());
219 clonedPlugin.setName(plugin.getName());
220 clonedPlugin.setPrefix(plugin.getPrefix());
227 private static List<String> mergeAvailableVersions( List<String> mainAvailableVersions, List<String> sourceAvailableVersions )
229 if ( sourceAvailableVersions == null )
231 return mainAvailableVersions;
234 if ( mainAvailableVersions == null )
236 return ArchivaModelCloner.cloneAvailableVersions( sourceAvailableVersions );
239 List<String> merged = ArchivaModelCloner.cloneAvailableVersions( mainAvailableVersions );
241 for ( String sourceVersion : sourceAvailableVersions )
243 if ( !merged.contains( sourceVersion ) )
245 merged.add( sourceVersion );