1package org.apache.archiva.metadata.repository.stats.model;
23/*4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122import org.apache.archiva.metadata.model.ArtifactMetadata;
23import org.apache.archiva.metadata.model.maven2.MavenArtifactFacet;
24import org.apache.archiva.metadata.repository.MetadataRepository;
25import org.apache.archiva.metadata.repository.MetadataRepositoryException;
26import org.apache.archiva.metadata.repository.MetadataResolutionException;
27import org.apache.archiva.metadata.repository.RepositorySession;
2829import java.util.Collection;
3031/**32 *33 * This is a default implementation of a statistics provider that walks the tree and34 * counts the artifacts found during the walk.35 * The implementation is not very fast. If metadata store provider can improve the36 * process by using store specific techniques (like query language) they should provide37 * their own implementation.38 *39 * @author Martin Stockhammer40 */41publicclassRepositoryWalkingStatisticsProviderimplementsRepositoryStatisticsProvider42 {
4344/**45 * Walks each namespace of the given repository id and counts the artifacts.46 *47 *48 * @param repositorySession49 * @param metadataRepository The repository implementation50 * @param repositoryId The repository Id51 * @param repositoryStatistics The statistics object that must be populated52 * @throws MetadataRepositoryException Throws the repository exception, if an error occurs while accessing the repository.53 */54 @Override
55publicvoid populateStatistics( RepositorySession repositorySession, MetadataRepository metadataRepository, String repositoryId,
56RepositoryStatistics repositoryStatistics )
57throwsMetadataRepositoryException58 {
59try60 {
61for ( String ns : metadataRepository.getRootNamespaces( repositorySession, repositoryId ) )
62 {
63 walkRepository( repositorySession, metadataRepository, repositoryStatistics, repositoryId, ns );
64 }
65 }
66catch ( MetadataResolutionException e )
67 {
68thrownewMetadataRepositoryException( e.getMessage(), e );
69 }
70 }
7172privatevoid walkRepository( RepositorySession repositorySession, MetadataRepository metadataRepository, RepositoryStatistics stats, String repositoryId,
73 String ns )
74throwsMetadataResolutionException75 {
76for ( String namespace : metadataRepository.getChildNamespaces( repositorySession , repositoryId, ns ) )
77 {
78 walkRepository( repositorySession, metadataRepository, stats, repositoryId, ns + "." + namespace );
79 }
8081 Collection<String> projects = metadataRepository.getProjects( repositorySession , repositoryId, ns );
82if ( !projects.isEmpty() )
83 {
84 stats.setTotalGroupCount( stats.getTotalGroupCount() + 1 );
85 stats.setTotalProjectCount( stats.getTotalProjectCount() + projects.size() );
8687for ( String project : projects )
88 {
89for ( String version : metadataRepository.getProjectVersions( repositorySession , repositoryId, ns, project ) )
90 {
91for ( ArtifactMetadata artifact : metadataRepository.getArtifacts( repositorySession , repositoryId, ns,
92 project, version ) )
93 {
94 stats.setTotalArtifactCount( stats.getTotalArtifactCount() + 1 );
95 stats.setTotalArtifactFileSize( stats.getTotalArtifactFileSize() + artifact.getSize() );
9697MavenArtifactFacet facet =
98 (MavenArtifactFacet) artifact.getFacet( MavenArtifactFacet.FACET_ID );
99if ( facet != null )
100 {
101 String type = facet.getType();
102 stats.setTotalCountForType( type, stats.getTotalCountForType( type ) + 1 );
103 }
104 }
105 }
106 }
107 }
108 }
109 }