This project has retired. For details please refer to its Attic page.
RepositoryMetadataMerge 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.ArchivaModelCloner;
23  import org.apache.archiva.model.ArchivaRepositoryMetadata;
24  import org.apache.archiva.model.Plugin;
25  import org.apache.archiva.model.SnapshotVersion;
26  import org.apache.archiva.repository.metadata.RepositoryMetadataException;
27  import org.apache.commons.lang3.StringUtils;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  /**
33   * RepositoryMetadataMerge 
34   *
35   *
36   */
37  public class RepositoryMetadataMerge
38  {
39      public static ArchivaRepositoryMetadatayMetadata.html#ArchivaRepositoryMetadata">ArchivaRepositoryMetadata merge( final ArchivaRepositoryMetadata mainMetadata,
40                                                     final ArchivaRepositoryMetadata sourceMetadata )
41          throws RepositoryMetadataException
42      {
43          if ( mainMetadata == null )
44          {
45              throw new RepositoryMetadataException( "Cannot merge a null main project." );
46          }
47  
48          if ( sourceMetadata == null )
49          {
50              throw new RepositoryMetadataException( "Cannot copy to a null parent project." );
51          }
52  
53          ArchivaRepositoryMetadatayMetadata.html#ArchivaRepositoryMetadata">ArchivaRepositoryMetadata merged = new ArchivaRepositoryMetadata();
54  
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() ) );
62          
63          //Don't set if merge was not possible
64          long lastUpdated = mergeTimestamp( mainMetadata.getLastUpdated(), sourceMetadata.getLastUpdated());
65          if (lastUpdated > -1)
66          {
67              merged.setLastUpdated(  Long.toString(lastUpdated) );
68          }
69          
70          return merged;
71      }
72  
73      private static boolean empty( String val )
74      {
75          if ( val == null )
76          {
77              return true;
78          }
79  
80          return ( val.trim().length() <= 0 );
81      }
82      
83      private static long mergeTimestamp(String mainTimestamp, String sourceTimestamp)
84      {
85          if (sourceTimestamp == null && mainTimestamp != null)
86          {
87              return convertTimestampToLong(mainTimestamp);
88          }
89          
90          if (mainTimestamp == null && sourceTimestamp != null)
91          {
92              return convertTimestampToLong(sourceTimestamp);
93          }
94          
95          if (sourceTimestamp == null && mainTimestamp == null)
96          {
97              return -1;
98          }
99          
100         return mergeTimestamp(convertTimestampToLong(mainTimestamp), convertTimestampToLong(sourceTimestamp));
101     }
102     
103     private static long mergeTimestamp(long mainTimestamp, long sourceTimestamp)
104     { 
105         return Math.max( mainTimestamp, sourceTimestamp );
106     }
107 
108     private static SnapshotVersiong/apache/archiva/model/SnapshotVersion.html#SnapshotVersion">SnapshotVersion../../../../org/apache/archiva/model/SnapshotVersion.html#SnapshotVersion">SnapshotVersion merge( SnapshotVersiong/apache/archiva/model/SnapshotVersion.html#SnapshotVersion">SnapshotVersion mainSnapshotVersion, SnapshotVersion sourceSnapshotVersion )
109     {
110         if ( sourceSnapshotVersion == null )
111         {
112             return mainSnapshotVersion;
113         }
114 
115         if ( mainSnapshotVersion == null )
116         {
117             return ArchivaModelCloner.clone( sourceSnapshotVersion );
118         }
119 
120         SnapshotVersionotVersion.html#SnapshotVersion">SnapshotVersion merged = new SnapshotVersion();
121        
122         long mainSnapshotLastUpdated = convertTimestampToLong(mainSnapshotVersion.getTimestamp());
123         long sourceSnapshotLastUpdated = convertTimestampToLong(sourceSnapshotVersion.getTimestamp());
124                         
125         long lastUpdated = mergeTimestamp(mainSnapshotLastUpdated, sourceSnapshotLastUpdated);
126         
127         if (lastUpdated == mainSnapshotLastUpdated)
128         {
129             merged.setTimestamp(mainSnapshotVersion.getTimestamp());
130             merged.setBuildNumber(mainSnapshotVersion.getBuildNumber());
131         }
132         else
133         {
134             merged.setTimestamp(sourceSnapshotVersion.getTimestamp());
135             merged.setBuildNumber(sourceSnapshotVersion.getBuildNumber());
136         }
137 
138         return merged;
139     }
140     
141     private static long convertTimestampToLong(String timestamp)
142     {
143         if (timestamp == null)
144         {
145             return -1;
146         }
147         
148         return getLongFromTimestampSafely(StringUtils.replace(timestamp, ".", ""));
149     }
150     
151     private static long getLongFromTimestampSafely( String timestampString )
152     {
153         try
154         {
155             return Long.parseLong(timestampString);
156         }
157         catch (NumberFormatException e)
158         {
159             return -1;
160         }
161     }
162 
163     private static String merge( String main, String source )
164     {
165         if ( empty( main ) && !empty( source ) )
166         {
167             return source;
168         }
169 
170         return main;
171     }
172     
173     private static List<Plugin> mergePlugins(List<Plugin> mainPlugins, List<Plugin> sourcePlugins)
174     {
175         if ( sourcePlugins == null )
176         {
177             return mainPlugins;
178         }
179         
180         if ( mainPlugins == null )
181         {
182             return clonePlugins( sourcePlugins );
183         }
184         
185         List<Plugin> merged = clonePlugins( mainPlugins );
186         
187         for ( Plugin plugin : sourcePlugins )
188         {
189             if ( !merged.contains( plugin ) )
190             {
191                 merged.add( plugin );
192             }
193         }
194 
195         return merged;
196     }
197     
198     /**
199      * Clones a list of plugins.
200      * 
201      * This method exists because ArchivaModelCloner.clonePlugins() 
202      * only works with artifact references.
203      * 
204      * @param plugins
205      * @return list of cloned plugins
206      */
207     private static List<Plugin> clonePlugins(List<Plugin> plugins)
208     {
209         if (plugins == null)
210         {
211             return null;
212         }
213         
214         List<Plugin> result = new ArrayList<>();
215         
216         for (Plugin plugin : plugins)
217         {
218             Plugingin.html#Plugin">Plugin clonedPlugin = new Plugin();
219             clonedPlugin.setArtifactId(plugin.getArtifactId());
220             clonedPlugin.setName(plugin.getName());
221             clonedPlugin.setPrefix(plugin.getPrefix());
222             result.add(plugin);
223         }
224         
225         return result;
226     }
227 
228     private static List<String> mergeAvailableVersions( List<String> mainAvailableVersions, List<String> sourceAvailableVersions )
229     {
230         if ( sourceAvailableVersions == null )
231         {
232             return mainAvailableVersions;
233         }
234 
235         if ( mainAvailableVersions == null )
236         {
237             return ArchivaModelCloner.cloneAvailableVersions( sourceAvailableVersions );
238         }
239 
240         List<String> merged = ArchivaModelCloner.cloneAvailableVersions( mainAvailableVersions );
241 
242         for ( String sourceVersion : sourceAvailableVersions )
243         {
244             if ( !merged.contains( sourceVersion ) )
245             {
246                 merged.add( sourceVersion );
247             }
248         }
249 
250         return merged;
251     }
252 }