1 package org.apache.archiva.metadata.repository.stats.model;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.metadata.model.ArtifactMetadata;
23 import org.apache.archiva.metadata.maven.model.MavenArtifactFacet;
24 import org.apache.archiva.metadata.repository.MetadataRepository;
25 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
26 import org.apache.archiva.metadata.repository.MetadataResolutionException;
27 import org.apache.archiva.metadata.repository.RepositorySession;
29 import java.util.Collection;
33 * This is a default implementation of a statistics provider that walks the tree and
34 * counts the artifacts found during the walk.
35 * The implementation is not very fast. If metadata store provider can improve the
36 * process by using store specific techniques (like query language) they should provide
37 * their own implementation.
39 * @author Martin Stockhammer
41 public class RepositoryWalkingStatisticsProvider implements RepositoryStatisticsProvider
45 * Walks each namespace of the given repository id and counts the artifacts.
48 * @param repositorySession
49 * @param metadataRepository The repository implementation
50 * @param repositoryId The repository Id
51 * @param repositoryStatistics The statistics object that must be populated
52 * @throws MetadataRepositoryException Throws the repository exception, if an error occurs while accessing the repository.
55 public void populateStatistics( RepositorySession repositorySession, MetadataRepository metadataRepository, String repositoryId,
56 RepositoryStatistics repositoryStatistics )
57 throws MetadataRepositoryException
61 for ( String ns : metadataRepository.getRootNamespaces( repositorySession, repositoryId ) )
63 walkRepository( repositorySession, metadataRepository, repositoryStatistics, repositoryId, ns );
66 catch ( MetadataResolutionException e )
68 throw new MetadataRepositoryException( e.getMessage(), e );
72 private void walkRepository( RepositorySession repositorySession, MetadataRepository metadataRepository, RepositoryStatistics stats, String repositoryId,
74 throws MetadataResolutionException
76 for ( String namespace : metadataRepository.getChildNamespaces( repositorySession , repositoryId, ns ) )
78 walkRepository( repositorySession, metadataRepository, stats, repositoryId, ns + "." + namespace );
81 Collection<String> projects = metadataRepository.getProjects( repositorySession , repositoryId, ns );
82 if ( !projects.isEmpty() )
84 stats.setTotalGroupCount( stats.getTotalGroupCount() + 1 );
85 stats.setTotalProjectCount( stats.getTotalProjectCount() + projects.size() );
87 for ( String project : projects )
89 for ( String version : metadataRepository.getProjectVersions( repositorySession , repositoryId, ns, project ) )
91 for ( ArtifactMetadata artifact : metadataRepository.getArtifacts( repositorySession , repositoryId, ns,
94 stats.setTotalArtifactCount( stats.getTotalArtifactCount() + 1 );
95 stats.setTotalArtifactFileSize( stats.getTotalArtifactFileSize() + artifact.getSize() );
97 MavenArtifactFacet facet =
98 (MavenArtifactFacet) artifact.getFacet( MavenArtifactFacet.FACET_ID );
101 String type = facet.getType();
102 stats.setTotalCountForType( type, stats.getTotalCountForType( type ) + 1 );