This project has retired. For details please refer to its
Attic page.
RepositoryMetadataMerge xref
1 package org.apache.archiva.repository.metadata.base;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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
200
201
202
203
204
205
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 }