You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MetadataTools.java 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. package org.apache.archiva.repository.metadata;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.checksum.ChecksumAlgorithm;
  21. import org.apache.archiva.checksum.ChecksummedFile;
  22. import org.apache.archiva.common.utils.PathUtil;
  23. import org.apache.archiva.common.utils.VersionComparator;
  24. import org.apache.archiva.common.utils.VersionUtil;
  25. import org.apache.archiva.configuration.ArchivaConfiguration;
  26. import org.apache.archiva.configuration.ConfigurationNames;
  27. import org.apache.archiva.configuration.FileTypes;
  28. import org.apache.archiva.configuration.ProxyConnectorConfiguration;
  29. import org.apache.archiva.maven2.metadata.MavenMetadataReader;
  30. import org.apache.archiva.model.ArchivaRepositoryMetadata;
  31. import org.apache.archiva.model.ArtifactReference;
  32. import org.apache.archiva.model.Plugin;
  33. import org.apache.archiva.model.ProjectReference;
  34. import org.apache.archiva.model.SnapshotVersion;
  35. import org.apache.archiva.model.VersionedReference;
  36. import org.apache.archiva.redback.components.registry.Registry;
  37. import org.apache.archiva.redback.components.registry.RegistryListener;
  38. import org.apache.archiva.repository.ContentNotFoundException;
  39. import org.apache.archiva.repository.ManagedRepositoryContent;
  40. import org.apache.archiva.repository.RemoteRepositoryContent;
  41. import org.apache.archiva.repository.layout.LayoutException;
  42. import org.apache.archiva.xml.XMLException;
  43. import org.apache.commons.collections.CollectionUtils;
  44. import org.apache.commons.io.FileUtils;
  45. import org.apache.commons.lang.StringUtils;
  46. import org.apache.commons.lang.math.NumberUtils;
  47. import org.apache.commons.lang.time.DateUtils;
  48. import org.slf4j.Logger;
  49. import org.slf4j.LoggerFactory;
  50. import org.springframework.stereotype.Service;
  51. import javax.annotation.PostConstruct;
  52. import javax.inject.Inject;
  53. import javax.inject.Named;
  54. import java.io.File;
  55. import java.io.IOException;
  56. import java.text.ParseException;
  57. import java.text.SimpleDateFormat;
  58. import java.util.ArrayList;
  59. import java.util.Calendar;
  60. import java.util.Collection;
  61. import java.util.Collections;
  62. import java.util.Date;
  63. import java.util.HashMap;
  64. import java.util.HashSet;
  65. import java.util.Iterator;
  66. import java.util.LinkedHashSet;
  67. import java.util.List;
  68. import java.util.Map;
  69. import java.util.Set;
  70. import java.util.regex.Matcher;
  71. /**
  72. * MetadataTools
  73. *
  74. *
  75. */
  76. @Service( "metadataTools#default" )
  77. public class MetadataTools
  78. implements RegistryListener
  79. {
  80. private Logger log = LoggerFactory.getLogger( getClass() );
  81. public static final String MAVEN_METADATA = "maven-metadata.xml";
  82. public static final String MAVEN_ARCHETYPE_CATALOG ="archetype-catalog.xml";
  83. private static final char PATH_SEPARATOR = '/';
  84. private static final char GROUP_SEPARATOR = '.';
  85. /**
  86. *
  87. */
  88. @Inject
  89. @Named( value = "archivaConfiguration#default" )
  90. private ArchivaConfiguration configuration;
  91. /**
  92. *
  93. */
  94. @Inject
  95. @Named( value = "fileTypes" )
  96. private FileTypes filetypes;
  97. private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
  98. private List<String> artifactPatterns;
  99. private Map<String, Set<String>> proxies;
  100. private static final char NUMS[] = new char[]{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  101. private SimpleDateFormat lastUpdatedFormat;
  102. public MetadataTools()
  103. {
  104. lastUpdatedFormat = new SimpleDateFormat( "yyyyMMddHHmmss" );
  105. lastUpdatedFormat.setTimeZone( DateUtils.UTC_TIME_ZONE );
  106. }
  107. public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  108. {
  109. if ( ConfigurationNames.isProxyConnector( propertyName ) )
  110. {
  111. initConfigVariables();
  112. }
  113. }
  114. public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  115. {
  116. /* nothing to do */
  117. }
  118. /**
  119. * Gather the set of snapshot versions found in a particular versioned reference.
  120. *
  121. * @return the Set of snapshot artifact versions found.
  122. * @throws LayoutException
  123. * @throws ContentNotFoundException
  124. */
  125. public Set<String> gatherSnapshotVersions( ManagedRepositoryContent managedRepository,
  126. VersionedReference reference )
  127. throws LayoutException, IOException, ContentNotFoundException
  128. {
  129. Set<String> foundVersions = managedRepository.getVersions( reference );
  130. // Next gather up the referenced 'latest' versions found in any proxied repositories
  131. // maven-metadata-${proxyId}.xml files that may be present.
  132. // Does this repository have a set of remote proxied repositories?
  133. Set<String> proxiedRepoIds = this.proxies.get( managedRepository.getId() );
  134. if ( CollectionUtils.isNotEmpty( proxiedRepoIds ) )
  135. {
  136. String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
  137. baseVersion = baseVersion.substring( 0, baseVersion.indexOf( VersionUtil.SNAPSHOT ) - 1 );
  138. // Add in the proxied repo version ids too.
  139. Iterator<String> it = proxiedRepoIds.iterator();
  140. while ( it.hasNext() )
  141. {
  142. String proxyId = it.next();
  143. ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata( managedRepository, reference, proxyId );
  144. if ( proxyMetadata == null )
  145. {
  146. // There is no proxy metadata, skip it.
  147. continue;
  148. }
  149. // Is there some snapshot info?
  150. SnapshotVersion snapshot = proxyMetadata.getSnapshotVersion();
  151. if ( snapshot != null )
  152. {
  153. String timestamp = snapshot.getTimestamp();
  154. int buildNumber = snapshot.getBuildNumber();
  155. // Only interested in the timestamp + buildnumber.
  156. if ( StringUtils.isNotBlank( timestamp ) && ( buildNumber > 0 ) )
  157. {
  158. foundVersions.add( baseVersion + "-" + timestamp + "-" + buildNumber );
  159. }
  160. }
  161. }
  162. }
  163. return foundVersions;
  164. }
  165. /**
  166. * Take a path to a maven-metadata.xml, and attempt to translate it to a VersionedReference.
  167. *
  168. * @param path
  169. * @return
  170. */
  171. public VersionedReference toVersionedReference( String path )
  172. throws RepositoryMetadataException
  173. {
  174. if ( !path.endsWith( "/" + MAVEN_METADATA ) )
  175. {
  176. throw new RepositoryMetadataException( "Cannot convert to versioned reference, not a metadata file. " );
  177. }
  178. VersionedReference reference = new VersionedReference();
  179. String normalizedPath = StringUtils.replace( path, "\\", "/" );
  180. String pathParts[] = StringUtils.split( normalizedPath, '/' );
  181. int versionOffset = pathParts.length - 2;
  182. int artifactIdOffset = versionOffset - 1;
  183. int groupIdEnd = artifactIdOffset - 1;
  184. reference.setVersion( pathParts[versionOffset] );
  185. if ( !hasNumberAnywhere( reference.getVersion() ) )
  186. {
  187. // Scary check, but without it, all paths are version references;
  188. throw new RepositoryMetadataException(
  189. "Not a versioned reference, as version id on path has no number in it." );
  190. }
  191. reference.setArtifactId( pathParts[artifactIdOffset] );
  192. StringBuilder gid = new StringBuilder();
  193. for ( int i = 0; i <= groupIdEnd; i++ )
  194. {
  195. if ( i > 0 )
  196. {
  197. gid.append( "." );
  198. }
  199. gid.append( pathParts[i] );
  200. }
  201. reference.setGroupId( gid.toString() );
  202. return reference;
  203. }
  204. private boolean hasNumberAnywhere( String version )
  205. {
  206. return StringUtils.indexOfAny( version, NUMS ) != ( -1 );
  207. }
  208. public ProjectReference toProjectReference( String path )
  209. throws RepositoryMetadataException
  210. {
  211. if ( !path.endsWith( "/" + MAVEN_METADATA ) )
  212. {
  213. throw new RepositoryMetadataException( "Cannot convert to versioned reference, not a metadata file. " );
  214. }
  215. ProjectReference reference = new ProjectReference();
  216. String normalizedPath = StringUtils.replace( path, "\\", "/" );
  217. String pathParts[] = StringUtils.split( normalizedPath, '/' );
  218. // Assume last part of the path is the version.
  219. int artifactIdOffset = pathParts.length - 2;
  220. int groupIdEnd = artifactIdOffset - 1;
  221. reference.setArtifactId( pathParts[artifactIdOffset] );
  222. StringBuilder gid = new StringBuilder();
  223. for ( int i = 0; i <= groupIdEnd; i++ )
  224. {
  225. if ( i > 0 )
  226. {
  227. gid.append( "." );
  228. }
  229. gid.append( pathParts[i] );
  230. }
  231. reference.setGroupId( gid.toString() );
  232. return reference;
  233. }
  234. public String toPath( ProjectReference reference )
  235. {
  236. StringBuilder path = new StringBuilder();
  237. path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
  238. path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
  239. path.append( MAVEN_METADATA );
  240. return path.toString();
  241. }
  242. public String toPath( VersionedReference reference )
  243. {
  244. StringBuilder path = new StringBuilder();
  245. path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
  246. path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
  247. if ( reference.getVersion() != null )
  248. {
  249. // add the version only if it is present
  250. path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
  251. }
  252. path.append( MAVEN_METADATA );
  253. return path.toString();
  254. }
  255. private String formatAsDirectory( String directory )
  256. {
  257. return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
  258. }
  259. /**
  260. * Adjusts a path for a metadata.xml file to its repository specific path.
  261. *
  262. * @param repository the repository to base new path off of.
  263. * @param path the path to the metadata.xml file to adjust the name of.
  264. * @return the newly adjusted path reference to the repository specific metadata path.
  265. */
  266. public String getRepositorySpecificName( RemoteRepositoryContent repository, String path )
  267. {
  268. return getRepositorySpecificName( repository.getId(), path );
  269. }
  270. /**
  271. * Adjusts a path for a metadata.xml file to its repository specific path.
  272. *
  273. * @param proxyId the repository id to base new path off of.
  274. * @param path the path to the metadata.xml file to adjust the name of.
  275. * @return the newly adjusted path reference to the repository specific metadata path.
  276. */
  277. public String getRepositorySpecificName( String proxyId, String path )
  278. {
  279. StringBuilder ret = new StringBuilder();
  280. int idx = path.lastIndexOf( '/' );
  281. if ( idx > 0 )
  282. {
  283. ret.append( path.substring( 0, idx + 1 ) );
  284. }
  285. // TODO: need to filter out 'bad' characters from the proxy id.
  286. ret.append( "maven-metadata-" ).append( proxyId ).append( ".xml" );
  287. return ret.toString();
  288. }
  289. @PostConstruct
  290. public void initialize()
  291. {
  292. this.artifactPatterns = new ArrayList<>();
  293. this.proxies = new HashMap<>();
  294. initConfigVariables();
  295. configuration.addChangeListener( this );
  296. }
  297. public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository,
  298. ProjectReference reference, String proxyId )
  299. {
  300. String metadataPath = getRepositorySpecificName( proxyId, toPath( reference ) );
  301. File metadataFile = new File( managedRepository.getRepoRoot(), metadataPath );
  302. if ( !metadataFile.exists() || !metadataFile.isFile() )
  303. {
  304. // Nothing to do. return null.
  305. return null;
  306. }
  307. try
  308. {
  309. return MavenMetadataReader.read( metadataFile );
  310. }
  311. catch ( XMLException e )
  312. {
  313. // TODO: [monitor] consider a monitor for this event.
  314. // TODO: consider a read-redo on monitor return code?
  315. log.warn( "Unable to read metadata: {}", metadataFile.getAbsolutePath(), e );
  316. return null;
  317. }
  318. }
  319. public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository,
  320. String logicalResource, String proxyId )
  321. {
  322. String metadataPath = getRepositorySpecificName( proxyId, logicalResource );
  323. File metadataFile = new File( managedRepository.getRepoRoot(), metadataPath );
  324. if ( !metadataFile.exists() || !metadataFile.isFile() )
  325. {
  326. // Nothing to do. return null.
  327. return null;
  328. }
  329. try
  330. {
  331. return MavenMetadataReader.read( metadataFile );
  332. }
  333. catch ( XMLException e )
  334. {
  335. // TODO: [monitor] consider a monitor for this event.
  336. // TODO: consider a read-redo on monitor return code?
  337. log.warn( "Unable to read metadata: {}", metadataFile.getAbsolutePath(), e );
  338. return null;
  339. }
  340. }
  341. public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository,
  342. VersionedReference reference, String proxyId )
  343. {
  344. String metadataPath = getRepositorySpecificName( proxyId, toPath( reference ) );
  345. File metadataFile = new File( managedRepository.getRepoRoot(), metadataPath );
  346. if ( !metadataFile.exists() || !metadataFile.isFile() )
  347. {
  348. // Nothing to do. return null.
  349. return null;
  350. }
  351. try
  352. {
  353. return MavenMetadataReader.read( metadataFile );
  354. }
  355. catch ( XMLException e )
  356. {
  357. // TODO: [monitor] consider a monitor for this event.
  358. // TODO: consider a read-redo on monitor return code?
  359. log.warn( "Unable to read metadata: {}", metadataFile.getAbsolutePath(), e );
  360. return null;
  361. }
  362. }
  363. public void updateMetadata( ManagedRepositoryContent managedRepository, String logicalResource )
  364. throws RepositoryMetadataException
  365. {
  366. final File metadataFile = new File( managedRepository.getRepoRoot(), logicalResource );
  367. ArchivaRepositoryMetadata metadata = null;
  368. //Gather and merge all metadata available
  369. List<ArchivaRepositoryMetadata> metadatas =
  370. getMetadatasForManagedRepository( managedRepository, logicalResource );
  371. for ( ArchivaRepositoryMetadata proxiedMetadata : metadatas )
  372. {
  373. if ( metadata == null )
  374. {
  375. metadata = proxiedMetadata;
  376. continue;
  377. }
  378. metadata = RepositoryMetadataMerge.merge( metadata, proxiedMetadata );
  379. }
  380. if ( metadata == null )
  381. {
  382. log.debug( "No metadata to update for {}", logicalResource );
  383. return;
  384. }
  385. Set<String> availableVersions = new HashSet<String>();
  386. List<String> metadataAvailableVersions = metadata.getAvailableVersions();
  387. if ( metadataAvailableVersions != null )
  388. {
  389. availableVersions.addAll( metadataAvailableVersions );
  390. }
  391. availableVersions = findPossibleVersions( availableVersions, metadataFile.getParentFile() );
  392. if ( availableVersions.size() > 0 )
  393. {
  394. updateMetadataVersions( availableVersions, metadata );
  395. }
  396. RepositoryMetadataWriter.write( metadata, metadataFile );
  397. ChecksummedFile checksum = new ChecksummedFile( metadataFile );
  398. checksum.fixChecksums( algorithms );
  399. }
  400. /**
  401. * Skims the parent directory of a metadata in vain hope of finding
  402. * subdirectories that contain poms.
  403. *
  404. * @param metadataParentDirectory
  405. * @return origional set plus newley found versions
  406. */
  407. private Set<String> findPossibleVersions( Set<String> versions, File metadataParentDirectory )
  408. {
  409. Set<String> result = new HashSet<String>( versions );
  410. for ( File directory : metadataParentDirectory.listFiles() )
  411. {
  412. if ( directory.isDirectory() )
  413. {
  414. for ( File possiblePom : directory.listFiles() )
  415. {
  416. if ( possiblePom.getName().endsWith( ".pom" ) )
  417. {
  418. result.add( directory.getName() );
  419. }
  420. }
  421. }
  422. }
  423. return result;
  424. }
  425. private List<ArchivaRepositoryMetadata> getMetadatasForManagedRepository(
  426. ManagedRepositoryContent managedRepository, String logicalResource )
  427. {
  428. List<ArchivaRepositoryMetadata> metadatas = new ArrayList<>();
  429. File file = new File( managedRepository.getRepoRoot(), logicalResource );
  430. if ( file.exists() )
  431. {
  432. try
  433. {
  434. ArchivaRepositoryMetadata existingMetadata = MavenMetadataReader.read( file );
  435. if ( existingMetadata != null )
  436. {
  437. metadatas.add( existingMetadata );
  438. }
  439. }
  440. catch ( XMLException e )
  441. {
  442. log.debug( "Could not read metadata at {}. Metadata will be removed.", file.getAbsolutePath() );
  443. FileUtils.deleteQuietly( file );
  444. }
  445. }
  446. Set<String> proxyIds = proxies.get( managedRepository.getId() );
  447. if ( proxyIds != null )
  448. {
  449. for ( String proxyId : proxyIds )
  450. {
  451. ArchivaRepositoryMetadata proxyMetadata =
  452. readProxyMetadata( managedRepository, logicalResource, proxyId );
  453. if ( proxyMetadata != null )
  454. {
  455. metadatas.add( proxyMetadata );
  456. }
  457. }
  458. }
  459. return metadatas;
  460. }
  461. /**
  462. * Update the metadata to represent the all versions/plugins of
  463. * the provided groupId:artifactId project or group reference,
  464. * based off of information present in the repository,
  465. * the maven-metadata.xml files, and the proxy/repository specific
  466. * metadata file contents.
  467. * <p>
  468. * We must treat this as a group or a project metadata file as there is no way to know in advance
  469. *
  470. * @param managedRepository the managed repository where the metadata is kept.
  471. * @param reference the reference to update.
  472. * @throws LayoutException
  473. * @throws RepositoryMetadataException
  474. * @throws IOException
  475. * @throws ContentNotFoundException
  476. * @deprecated
  477. */
  478. public void updateMetadata( ManagedRepositoryContent managedRepository, ProjectReference reference )
  479. throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException
  480. {
  481. File metadataFile = new File( managedRepository.getRepoRoot(), toPath( reference ) );
  482. long lastUpdated = getExistingLastUpdated( metadataFile );
  483. ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
  484. metadata.setGroupId( reference.getGroupId() );
  485. metadata.setArtifactId( reference.getArtifactId() );
  486. // Gather up all versions found in the managed repository.
  487. Set<String> allVersions = managedRepository.getVersions( reference );
  488. // Gather up all plugins found in the managed repository.
  489. // TODO: do we know this information instead?
  490. // Set<Plugin> allPlugins = managedRepository.getPlugins( reference );
  491. Set<Plugin> allPlugins;
  492. if ( metadataFile.exists() )
  493. {
  494. try
  495. {
  496. allPlugins = new LinkedHashSet<Plugin>( MavenMetadataReader.read( metadataFile ).getPlugins() );
  497. }
  498. catch ( XMLException e )
  499. {
  500. throw new RepositoryMetadataException( e.getMessage(), e );
  501. }
  502. }
  503. else
  504. {
  505. allPlugins = new LinkedHashSet<Plugin>();
  506. }
  507. // Does this repository have a set of remote proxied repositories?
  508. Set<String> proxiedRepoIds = this.proxies.get( managedRepository.getId() );
  509. if ( CollectionUtils.isNotEmpty( proxiedRepoIds ) )
  510. {
  511. // Add in the proxied repo version ids too.
  512. Iterator<String> it = proxiedRepoIds.iterator();
  513. while ( it.hasNext() )
  514. {
  515. String proxyId = it.next();
  516. ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata( managedRepository, reference, proxyId );
  517. if ( proxyMetadata != null )
  518. {
  519. allVersions.addAll( proxyMetadata.getAvailableVersions() );
  520. allPlugins.addAll( proxyMetadata.getPlugins() );
  521. long proxyLastUpdated = getLastUpdated( proxyMetadata );
  522. lastUpdated = Math.max( lastUpdated, proxyLastUpdated );
  523. }
  524. }
  525. }
  526. if ( !allVersions.isEmpty() )
  527. {
  528. updateMetadataVersions( allVersions, metadata );
  529. }
  530. else
  531. {
  532. // Add the plugins to the metadata model.
  533. metadata.setPlugins( new ArrayList<>( allPlugins ) );
  534. // artifact ID was actually the last part of the group
  535. metadata.setGroupId( metadata.getGroupId() + "." + metadata.getArtifactId() );
  536. metadata.setArtifactId( null );
  537. }
  538. if ( lastUpdated > 0 )
  539. {
  540. metadata.setLastUpdatedTimestamp( toLastUpdatedDate( lastUpdated ) );
  541. }
  542. // Save the metadata model to disk.
  543. RepositoryMetadataWriter.write( metadata, metadataFile );
  544. ChecksummedFile checksum = new ChecksummedFile( metadataFile );
  545. checksum.fixChecksums( algorithms );
  546. }
  547. private void updateMetadataVersions( Collection<String> allVersions, ArchivaRepositoryMetadata metadata )
  548. {
  549. // Sort the versions
  550. List<String> sortedVersions = new ArrayList<>( allVersions );
  551. Collections.sort( sortedVersions, VersionComparator.getInstance() );
  552. // Split the versions into released and snapshots.
  553. List<String> releasedVersions = new ArrayList<>();
  554. List<String> snapshotVersions = new ArrayList<>();
  555. for ( String version : sortedVersions )
  556. {
  557. if ( VersionUtil.isSnapshot( version ) )
  558. {
  559. snapshotVersions.add( version );
  560. }
  561. else
  562. {
  563. releasedVersions.add( version );
  564. }
  565. }
  566. Collections.sort( releasedVersions, VersionComparator.getInstance() );
  567. Collections.sort( snapshotVersions, VersionComparator.getInstance() );
  568. String latestVersion = sortedVersions.get( sortedVersions.size() - 1 );
  569. String releaseVersion = null;
  570. if ( CollectionUtils.isNotEmpty( releasedVersions ) )
  571. {
  572. releaseVersion = releasedVersions.get( releasedVersions.size() - 1 );
  573. }
  574. // Add the versions to the metadata model.
  575. metadata.setAvailableVersions( sortedVersions );
  576. metadata.setLatestVersion( latestVersion );
  577. metadata.setReleasedVersion( releaseVersion );
  578. }
  579. private Date toLastUpdatedDate( long lastUpdated )
  580. {
  581. Calendar cal = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
  582. cal.setTimeInMillis( lastUpdated );
  583. return cal.getTime();
  584. }
  585. private long toLastUpdatedLong( String timestampString )
  586. {
  587. try
  588. {
  589. Date date = lastUpdatedFormat.parse( timestampString );
  590. Calendar cal = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
  591. cal.setTime( date );
  592. return cal.getTimeInMillis();
  593. }
  594. catch ( ParseException e )
  595. {
  596. return 0;
  597. }
  598. }
  599. private long getLastUpdated( ArchivaRepositoryMetadata metadata )
  600. {
  601. if ( metadata == null )
  602. {
  603. // Doesn't exist.
  604. return 0;
  605. }
  606. try
  607. {
  608. String lastUpdated = metadata.getLastUpdated();
  609. if ( StringUtils.isBlank( lastUpdated ) )
  610. {
  611. // Not set.
  612. return 0;
  613. }
  614. Date lastUpdatedDate = lastUpdatedFormat.parse( lastUpdated );
  615. return lastUpdatedDate.getTime();
  616. }
  617. catch ( ParseException e )
  618. {
  619. // Bad format on the last updated string.
  620. return 0;
  621. }
  622. }
  623. private long getExistingLastUpdated( File metadataFile )
  624. {
  625. if ( !metadataFile.exists() )
  626. {
  627. // Doesn't exist.
  628. return 0;
  629. }
  630. try
  631. {
  632. ArchivaRepositoryMetadata metadata = MavenMetadataReader.read( metadataFile );
  633. return getLastUpdated( metadata );
  634. }
  635. catch ( XMLException e )
  636. {
  637. // Error.
  638. return 0;
  639. }
  640. }
  641. /**
  642. * Update the metadata based on the following rules.
  643. * <p>
  644. * 1) If this is a SNAPSHOT reference, then utilize the proxy/repository specific
  645. * metadata files to represent the current / latest SNAPSHOT available.
  646. * 2) If this is a RELEASE reference, and the metadata file does not exist, then
  647. * create the metadata file with contents required of the VersionedReference
  648. *
  649. * @param managedRepository the managed repository where the metadata is kept.
  650. * @param reference the versioned reference to update
  651. * @throws LayoutException
  652. * @throws RepositoryMetadataException
  653. * @throws IOException
  654. * @throws ContentNotFoundException
  655. * @deprecated
  656. */
  657. public void updateMetadata( ManagedRepositoryContent managedRepository, VersionedReference reference )
  658. throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException
  659. {
  660. File metadataFile = new File( managedRepository.getRepoRoot(), toPath( reference ) );
  661. long lastUpdated = getExistingLastUpdated( metadataFile );
  662. ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
  663. metadata.setGroupId( reference.getGroupId() );
  664. metadata.setArtifactId( reference.getArtifactId() );
  665. if ( VersionUtil.isSnapshot( reference.getVersion() ) )
  666. {
  667. // Do SNAPSHOT handling.
  668. metadata.setVersion( VersionUtil.getBaseVersion( reference.getVersion() ) );
  669. // Gather up all of the versions found in the reference dir, and any
  670. // proxied maven-metadata.xml files.
  671. Set<String> snapshotVersions = gatherSnapshotVersions( managedRepository, reference );
  672. if ( snapshotVersions.isEmpty() )
  673. {
  674. throw new ContentNotFoundException(
  675. "No snapshot versions found on reference [" + VersionedReference.toKey( reference ) + "]." );
  676. }
  677. // sort the list to determine to aide in determining the Latest version.
  678. List<String> sortedVersions = new ArrayList<>();
  679. sortedVersions.addAll( snapshotVersions );
  680. Collections.sort( sortedVersions, new VersionComparator() );
  681. String latestVersion = sortedVersions.get( sortedVersions.size() - 1 );
  682. if ( VersionUtil.isUniqueSnapshot( latestVersion ) )
  683. {
  684. // The latestVersion will contain the full version string "1.0-alpha-5-20070821.213044-8"
  685. // This needs to be broken down into ${base}-${timestamp}-${build_number}
  686. Matcher m = VersionUtil.UNIQUE_SNAPSHOT_PATTERN.matcher( latestVersion );
  687. if ( m.matches() )
  688. {
  689. metadata.setSnapshotVersion( new SnapshotVersion() );
  690. int buildNumber = NumberUtils.toInt( m.group( 3 ), -1 );
  691. metadata.getSnapshotVersion().setBuildNumber( buildNumber );
  692. Matcher mtimestamp = VersionUtil.TIMESTAMP_PATTERN.matcher( m.group( 2 ) );
  693. if ( mtimestamp.matches() )
  694. {
  695. String tsDate = mtimestamp.group( 1 );
  696. String tsTime = mtimestamp.group( 2 );
  697. long snapshotLastUpdated = toLastUpdatedLong( tsDate + tsTime );
  698. lastUpdated = Math.max( lastUpdated, snapshotLastUpdated );
  699. metadata.getSnapshotVersion().setTimestamp( m.group( 2 ) );
  700. }
  701. }
  702. }
  703. else if ( VersionUtil.isGenericSnapshot( latestVersion ) )
  704. {
  705. // The latestVersion ends with the generic version string.
  706. // Example: 1.0-alpha-5-SNAPSHOT
  707. metadata.setSnapshotVersion( new SnapshotVersion() );
  708. /* Disabled due to decision in [MRM-535].
  709. * Do not set metadata.lastUpdated to file.lastModified.
  710. *
  711. * Should this be the last updated timestamp of the file, or in the case of an
  712. * archive, the most recent timestamp in the archive?
  713. *
  714. ArtifactReference artifact = getFirstArtifact( managedRepository, reference );
  715. if ( artifact == null )
  716. {
  717. throw new IOException( "Not snapshot artifact found to reference in " + reference );
  718. }
  719. File artifactFile = managedRepository.toFile( artifact );
  720. if ( artifactFile.exists() )
  721. {
  722. Date lastModified = new Date( artifactFile.lastModified() );
  723. metadata.setLastUpdatedTimestamp( lastModified );
  724. }
  725. */
  726. }
  727. else
  728. {
  729. throw new RepositoryMetadataException(
  730. "Unable to process snapshot version <" + latestVersion + "> reference <" + reference + ">" );
  731. }
  732. }
  733. else
  734. {
  735. // Do RELEASE handling.
  736. metadata.setVersion( reference.getVersion() );
  737. }
  738. // Set last updated
  739. if ( lastUpdated > 0 )
  740. {
  741. metadata.setLastUpdatedTimestamp( toLastUpdatedDate( lastUpdated ) );
  742. }
  743. // Save the metadata model to disk.
  744. RepositoryMetadataWriter.write( metadata, metadataFile );
  745. ChecksummedFile checksum = new ChecksummedFile( metadataFile );
  746. checksum.fixChecksums( algorithms );
  747. }
  748. private void initConfigVariables()
  749. {
  750. synchronized ( this.artifactPatterns )
  751. {
  752. this.artifactPatterns.clear();
  753. this.artifactPatterns.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
  754. }
  755. synchronized ( proxies )
  756. {
  757. this.proxies.clear();
  758. List<ProxyConnectorConfiguration> proxyConfigs = configuration.getConfiguration().getProxyConnectors();
  759. for ( ProxyConnectorConfiguration proxyConfig : proxyConfigs )
  760. {
  761. String key = proxyConfig.getSourceRepoId();
  762. Set<String> remoteRepoIds = this.proxies.get( key );
  763. if ( remoteRepoIds == null )
  764. {
  765. remoteRepoIds = new HashSet<String>();
  766. }
  767. remoteRepoIds.add( proxyConfig.getTargetRepoId() );
  768. this.proxies.put( key, remoteRepoIds );
  769. }
  770. }
  771. }
  772. /**
  773. * Get the first Artifact found in the provided VersionedReference location.
  774. *
  775. * @param managedRepository the repository to search within.
  776. * @param reference the reference to the versioned reference to search within
  777. * @return the ArtifactReference to the first artifact located within the versioned reference. or null if
  778. * no artifact was found within the versioned reference.
  779. * @throws IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
  780. * @throws LayoutException
  781. */
  782. public ArtifactReference getFirstArtifact( ManagedRepositoryContent managedRepository,
  783. VersionedReference reference )
  784. throws LayoutException, IOException
  785. {
  786. String path = toPath( reference );
  787. int idx = path.lastIndexOf( '/' );
  788. if ( idx > 0 )
  789. {
  790. path = path.substring( 0, idx );
  791. }
  792. File repoDir = new File( managedRepository.getRepoRoot(), path );
  793. if ( !repoDir.exists() )
  794. {
  795. throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
  796. + repoDir.getAbsolutePath() );
  797. }
  798. if ( !repoDir.isDirectory() )
  799. {
  800. throw new IOException(
  801. "Unable to gather the list of snapshot versions on a non-directory: " + repoDir.getAbsolutePath() );
  802. }
  803. File repoFiles[] = repoDir.listFiles();
  804. for ( int i = 0; i < repoFiles.length; i++ )
  805. {
  806. if ( repoFiles[i].isDirectory() )
  807. {
  808. // Skip it. it's a directory.
  809. continue;
  810. }
  811. String relativePath = PathUtil.getRelative( managedRepository.getRepoRoot(), repoFiles[i] );
  812. if ( filetypes.matchesArtifactPattern( relativePath ) )
  813. {
  814. ArtifactReference artifact = managedRepository.toArtifactReference( relativePath );
  815. return artifact;
  816. }
  817. }
  818. // No artifact was found.
  819. return null;
  820. }
  821. public ArchivaConfiguration getConfiguration()
  822. {
  823. return configuration;
  824. }
  825. public void setConfiguration( ArchivaConfiguration configuration )
  826. {
  827. this.configuration = configuration;
  828. }
  829. public FileTypes getFiletypes()
  830. {
  831. return filetypes;
  832. }
  833. public void setFiletypes( FileTypes filetypes )
  834. {
  835. this.filetypes = filetypes;
  836. }
  837. }