1 | |
package org.apache.maven.archiva.repository.metadata; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.util.ArrayList; |
23 | |
import java.util.List; |
24 | |
|
25 | |
import org.apache.commons.lang.StringUtils; |
26 | |
import org.apache.maven.archiva.model.ArchivaModelCloner; |
27 | |
import org.apache.maven.archiva.model.ArchivaRepositoryMetadata; |
28 | |
import org.apache.maven.archiva.model.Plugin; |
29 | |
import org.apache.maven.archiva.model.SnapshotVersion; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | 0 | public class RepositoryMetadataMerge |
37 | |
{ |
38 | |
public static ArchivaRepositoryMetadata merge( final ArchivaRepositoryMetadata mainMetadata, |
39 | |
final ArchivaRepositoryMetadata sourceMetadata ) |
40 | |
throws RepositoryMetadataException |
41 | |
{ |
42 | 0 | if ( mainMetadata == null ) |
43 | |
{ |
44 | 0 | throw new RepositoryMetadataException( "Cannot merge a null main project." ); |
45 | |
} |
46 | |
|
47 | 0 | if ( sourceMetadata == null ) |
48 | |
{ |
49 | 0 | throw new RepositoryMetadataException( "Cannot copy to a null parent project." ); |
50 | |
} |
51 | |
|
52 | 0 | ArchivaRepositoryMetadata merged = new ArchivaRepositoryMetadata(); |
53 | |
|
54 | 0 | merged.setGroupId( merge( mainMetadata.getGroupId(), sourceMetadata.getGroupId() ) ); |
55 | 0 | merged.setArtifactId( merge(mainMetadata.getArtifactId(), sourceMetadata.getArtifactId())); |
56 | 0 | merged.setVersion( merge(mainMetadata.getVersion(), sourceMetadata.getVersion()) ); |
57 | 0 | merged.setReleasedVersion( merge( mainMetadata.getReleasedVersion(), sourceMetadata.getReleasedVersion() ) ); |
58 | 0 | merged.setSnapshotVersion( merge( mainMetadata.getSnapshotVersion(), sourceMetadata.getSnapshotVersion() ) ); |
59 | 0 | merged.setAvailableVersions( mergeAvailableVersions( mainMetadata.getAvailableVersions(), sourceMetadata.getAvailableVersions() ) ); |
60 | 0 | merged.setPlugins( mergePlugins( mainMetadata.getPlugins(), sourceMetadata.getPlugins() ) ); |
61 | |
|
62 | |
|
63 | 0 | long lastUpdated = mergeTimestamp( mainMetadata.getLastUpdated(), sourceMetadata.getLastUpdated()); |
64 | 0 | if (lastUpdated > -1) |
65 | |
{ |
66 | 0 | merged.setLastUpdated( Long.toString(lastUpdated) ); |
67 | |
} |
68 | |
|
69 | 0 | return merged; |
70 | |
} |
71 | |
|
72 | |
private static boolean empty( String val ) |
73 | |
{ |
74 | 0 | if ( val == null ) |
75 | |
{ |
76 | 0 | return true; |
77 | |
} |
78 | |
|
79 | 0 | return ( val.trim().length() <= 0 ); |
80 | |
} |
81 | |
|
82 | |
private static long mergeTimestamp(String mainTimestamp, String sourceTimestamp) |
83 | |
{ |
84 | 0 | if (sourceTimestamp == null && mainTimestamp != null) |
85 | |
{ |
86 | 0 | return convertTimestampToLong(mainTimestamp); |
87 | |
} |
88 | |
|
89 | 0 | if (mainTimestamp == null && sourceTimestamp != null) |
90 | |
{ |
91 | 0 | return convertTimestampToLong(sourceTimestamp); |
92 | |
} |
93 | |
|
94 | 0 | if (sourceTimestamp == null && mainTimestamp == null) |
95 | |
{ |
96 | 0 | return -1; |
97 | |
} |
98 | |
|
99 | 0 | return mergeTimestamp(convertTimestampToLong(mainTimestamp), convertTimestampToLong(sourceTimestamp)); |
100 | |
} |
101 | |
|
102 | |
private static long mergeTimestamp(long mainTimestamp, long sourceTimestamp) |
103 | |
{ |
104 | 0 | return Math.max( mainTimestamp, sourceTimestamp ); |
105 | |
} |
106 | |
|
107 | |
private static SnapshotVersion merge( SnapshotVersion mainSnapshotVersion, SnapshotVersion sourceSnapshotVersion ) |
108 | |
{ |
109 | 0 | if ( sourceSnapshotVersion == null ) |
110 | |
{ |
111 | 0 | return mainSnapshotVersion; |
112 | |
} |
113 | |
|
114 | 0 | if ( mainSnapshotVersion == null ) |
115 | |
{ |
116 | 0 | return ArchivaModelCloner.clone( sourceSnapshotVersion ); |
117 | |
} |
118 | |
|
119 | 0 | SnapshotVersion merged = new SnapshotVersion(); |
120 | |
|
121 | 0 | long mainSnapshotLastUpdated = convertTimestampToLong(mainSnapshotVersion.getTimestamp()); |
122 | 0 | long sourceSnapshotLastUpdated = convertTimestampToLong(sourceSnapshotVersion.getTimestamp()); |
123 | |
|
124 | 0 | long lastUpdated = mergeTimestamp(mainSnapshotLastUpdated, sourceSnapshotLastUpdated); |
125 | |
|
126 | 0 | if (lastUpdated == mainSnapshotLastUpdated) |
127 | |
{ |
128 | 0 | merged.setTimestamp(mainSnapshotVersion.getTimestamp()); |
129 | 0 | merged.setBuildNumber(mainSnapshotVersion.getBuildNumber()); |
130 | |
} |
131 | |
else |
132 | |
{ |
133 | 0 | merged.setTimestamp(sourceSnapshotVersion.getTimestamp()); |
134 | 0 | merged.setBuildNumber(sourceSnapshotVersion.getBuildNumber()); |
135 | |
} |
136 | |
|
137 | 0 | return merged; |
138 | |
} |
139 | |
|
140 | |
private static long convertTimestampToLong(String timestamp) |
141 | |
{ |
142 | 0 | if (timestamp == null) |
143 | |
{ |
144 | 0 | return -1; |
145 | |
} |
146 | |
|
147 | 0 | return getLongFromTimestampSafely(StringUtils.replace(timestamp, ".", "")); |
148 | |
} |
149 | |
|
150 | |
private static long getLongFromTimestampSafely( String timestampString ) |
151 | |
{ |
152 | |
try |
153 | |
{ |
154 | 0 | return Long.parseLong(timestampString); |
155 | |
} |
156 | 0 | catch (NumberFormatException e) |
157 | |
{ |
158 | 0 | return -1; |
159 | |
} |
160 | |
} |
161 | |
|
162 | |
private static String merge( String main, String source ) |
163 | |
{ |
164 | 0 | if ( empty( main ) && !empty( source ) ) |
165 | |
{ |
166 | 0 | return source; |
167 | |
} |
168 | |
|
169 | 0 | return main; |
170 | |
} |
171 | |
|
172 | |
private static List<Plugin> mergePlugins(List<Plugin> mainPlugins, List<Plugin> sourcePlugins) |
173 | |
{ |
174 | 0 | if ( sourcePlugins == null ) |
175 | |
{ |
176 | 0 | return mainPlugins; |
177 | |
} |
178 | |
|
179 | 0 | if ( mainPlugins == null ) |
180 | |
{ |
181 | 0 | return clonePlugins( sourcePlugins ); |
182 | |
} |
183 | |
|
184 | 0 | List<Plugin> merged = clonePlugins( mainPlugins ); |
185 | |
|
186 | 0 | for ( Plugin plugin : sourcePlugins ) |
187 | |
{ |
188 | 0 | if ( !merged.contains( plugin ) ) |
189 | |
{ |
190 | 0 | merged.add( plugin ); |
191 | |
} |
192 | |
} |
193 | |
|
194 | 0 | return merged; |
195 | |
} |
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
private static List<Plugin> clonePlugins(List<Plugin> plugins) |
207 | |
{ |
208 | 0 | if (plugins == null) |
209 | |
{ |
210 | 0 | return null; |
211 | |
} |
212 | |
|
213 | 0 | List<Plugin> result = new ArrayList<Plugin>(); |
214 | |
|
215 | 0 | for (Plugin plugin : plugins) |
216 | |
{ |
217 | 0 | Plugin clonedPlugin = new Plugin(); |
218 | 0 | clonedPlugin.setArtifactId(plugin.getArtifactId()); |
219 | 0 | clonedPlugin.setName(plugin.getName()); |
220 | 0 | clonedPlugin.setPrefix(plugin.getPrefix()); |
221 | 0 | result.add(plugin); |
222 | 0 | } |
223 | |
|
224 | 0 | return result; |
225 | |
} |
226 | |
|
227 | |
private static List<String> mergeAvailableVersions( List<String> mainAvailableVersions, List<String> sourceAvailableVersions ) |
228 | |
{ |
229 | 0 | if ( sourceAvailableVersions == null ) |
230 | |
{ |
231 | 0 | return mainAvailableVersions; |
232 | |
} |
233 | |
|
234 | 0 | if ( mainAvailableVersions == null ) |
235 | |
{ |
236 | 0 | return ArchivaModelCloner.cloneAvailableVersions( sourceAvailableVersions ); |
237 | |
} |
238 | |
|
239 | 0 | List<String> merged = ArchivaModelCloner.cloneAvailableVersions( mainAvailableVersions ); |
240 | |
|
241 | 0 | for ( String sourceVersion : sourceAvailableVersions ) |
242 | |
{ |
243 | 0 | if ( !merged.contains( sourceVersion ) ) |
244 | |
{ |
245 | 0 | merged.add( sourceVersion ); |
246 | |
} |
247 | |
} |
248 | |
|
249 | 0 | return merged; |
250 | |
} |
251 | |
} |