DefaultUpdateCheckManager implements UpdateCheckManager, Service { public void checkMetadata( RepositorySystemSession session, UpdateCheck<Metadata, MetadataTransferException> check ) { if ( check.getLocalLastUpdated() != 0 && !isUpdatedRequired( session, check.getLocalLastUpdated(), check.getPolicy() ) ) { if ( logger.isDebugEnabled() ) { logger.debug( "Skipped remote update check for " + check.getItem() + ", locally installed metadata up-to-date." ); } check.setRequired( false ); return; } Metadata metadata = check.getItem(); RemoteRepository repository = check.getRepository(); File metadataFile = check.getFile(); if ( metadataFile == null ) { throw new IllegalArgumentException( String.format( "The metadata '%s' has no file attached", metadata ) ); } boolean fileExists = check.isFileValid() && metadataFile.exists(); File touchFile = getTouchFile( metadata, metadataFile ); Properties props = read( touchFile ); String updateKey = getUpdateKey( metadataFile, repository ); String dataKey = getDataKey( metadata, metadataFile, check.getAuthoritativeRepository() ); String error = getError( props, dataKey ); long lastUpdated; if ( error == null ) { if ( fileExists ) { lastUpdated = getLastUpdated( props, dataKey ); } else { lastUpdated = 0; } } else if ( error.length() <= 0 ) { lastUpdated = getLastUpdated( props, dataKey ); } else { String transferKey = getTransferKey( metadata, metadataFile, repository ); lastUpdated = getLastUpdated( props, transferKey ); } if ( isAlreadyUpdated( session.getData(), updateKey ) ) { if ( logger.isDebugEnabled() ) { logger.debug( "Skipped remote update check for " + check.getItem() + ", already updated during this session." ); } check.setRequired( false ); if ( error != null ) { check.setException( newException( error, metadata, repository ) ); } } else if ( lastUpdated == 0 ) { check.setRequired( true ); } else if ( isUpdatedRequired( session, lastUpdated, check.getPolicy() ) ) { check.setRequired( true ); } else if ( fileExists ) { if ( logger.isDebugEnabled() ) { logger.debug( "Skipped remote update check for " + check.getItem() + ", locally cached metadata up-to-date." ); } check.setRequired( false ); } else { if ( error == null || error.length() <= 0 ) { check.setRequired( false ); check.setException( newException( error, metadata, repository ) ); } else { if ( session.isTransferErrorCachingEnabled() ) { check.setRequired( false ); check.setException( newException( error, metadata, repository ) ); } else { check.setRequired( true ); } } } } DefaultUpdateCheckManager();  DefaultUpdateCheckManager( Logger logger ); void initService( ServiceLocator locator ); DefaultUpdateCheckManager setLogger( Logger logger ); String getEffectiveUpdatePolicy( RepositorySystemSession session, String policy1, String policy2 ); void checkArtifact( RepositorySystemSession session, UpdateCheck<Artifact, ArtifactTransferException> check ); void checkMetadata( RepositorySystemSession session, UpdateCheck<Metadata, MetadataTransferException> check ); boolean isUpdatedRequired( RepositorySystemSession session, long lastModified, String policy ); void touchArtifact( RepositorySystemSession session, UpdateCheck<Artifact, ArtifactTransferException> check ); void touchMetadata( RepositorySystemSession session, UpdateCheck<Metadata, MetadataTransferException> check );  }
@Test( expected = Exception.class ) public void testCheckMetadataFailOnNoFile() throws Exception { UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck(); check.setItem( metadata.setFile( null ) ); check.setFile( null ); manager.checkMetadata( session, check ); }
@Test public void testCheckMetadataUpdatePolicyRequired() throws Exception { UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck(); Calendar cal = Calendar.getInstance(); cal.add( Calendar.DATE, -1 ); check.setLocalLastUpdated( cal.getTimeInMillis() ); check.setPolicy( RepositoryPolicy.UPDATE_POLICY_ALWAYS ); manager.checkMetadata( session, check ); assertNull( check.getException() ); assertTrue( check.isRequired() ); check.setPolicy( RepositoryPolicy.UPDATE_POLICY_DAILY ); manager.checkMetadata( session, check ); assertNull( check.getException() ); assertTrue( check.isRequired() ); check.setPolicy( RepositoryPolicy.UPDATE_POLICY_INTERVAL + ":60" ); manager.checkMetadata( session, check ); assertNull( check.getException() ); assertTrue( check.isRequired() ); }
@Test public void testCheckMetadataUpdatePolicyNotRequired() throws Exception { UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck(); check.setLocalLastUpdated( System.currentTimeMillis() ); check.setPolicy( RepositoryPolicy.UPDATE_POLICY_NEVER ); manager.checkMetadata( session, check ); assertFalse( check.isRequired() ); check.setPolicy( RepositoryPolicy.UPDATE_POLICY_DAILY ); manager.checkMetadata( session, check ); assertFalse( check.isRequired() ); check.setPolicy( RepositoryPolicy.UPDATE_POLICY_INTERVAL + ":61" ); manager.checkMetadata( session, check ); assertFalse( check.isRequired() ); check.setPolicy( "no particular policy" ); manager.checkMetadata( session, check ); assertFalse( check.isRequired() ); }
@Test public void testCheckMetadata() throws Exception { UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck(); check.setPolicy( RepositoryPolicy.UPDATE_POLICY_DAILY ); manager.checkMetadata( session, check ); assertEquals( true, check.isRequired() ); manager.touchMetadata( session, check ); resetSessionData( session ); check = newMetadataCheck(); check.setPolicy( RepositoryPolicy.UPDATE_POLICY_INTERVAL + ":60" ); manager.checkMetadata( session, check ); assertEquals( false, check.isRequired() ); check.getFile().delete(); manager.checkMetadata( session, check ); assertEquals( true, check.isRequired() ); }
@Test public void testCheckMetadataNoLocalFile() throws Exception { metadata.getFile().delete(); UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck(); long lastUpdate = new Date().getTime() - HOUR; check.setLocalLastUpdated( lastUpdate ); check.setLocalLastUpdated( lastUpdate ); manager.checkMetadata( session, check ); assertEquals( true, check.isRequired() ); }
@Test public void testCheckMetadataWhenLocallyMissingEvenIfUpdatePolicyIsNever() throws Exception { UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck(); check.setPolicy( RepositoryPolicy.UPDATE_POLICY_NEVER ); session.setNotFoundCachingEnabled( true ); check.getFile().delete(); assertEquals( check.getFile().getAbsolutePath(), false, check.getFile().exists() ); manager.checkMetadata( session, check ); assertEquals( true, check.isRequired() ); }
@Test public void testCheckMetadataNotWhenUpdatePolicyIsNeverAndTimestampIsUnavailable() throws Exception { UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck(); check.setPolicy( RepositoryPolicy.UPDATE_POLICY_NEVER ); session.setNotFoundCachingEnabled( true ); manager.checkMetadata( session, check ); assertEquals( false, check.isRequired() ); }