This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.metadata.repository.stats.model;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.archiva.metadata.model.ArtifactMetadata;
023import org.apache.archiva.metadata.model.maven2.MavenArtifactFacet;
024import org.apache.archiva.metadata.repository.MetadataRepository;
025import org.apache.archiva.metadata.repository.MetadataRepositoryException;
026import org.apache.archiva.metadata.repository.MetadataResolutionException;
027import org.apache.archiva.metadata.repository.RepositorySession;
028
029import java.util.Collection;
030
031/**
032 *
033 * This is a default implementation of a statistics provider that walks the tree and
034 * counts the artifacts found during the walk.
035 * The implementation is not very fast. If metadata store provider can improve the
036 * process by using store specific techniques (like query language) they should provide
037 * their own implementation.
038 *
039 * @author Martin Stockhammer
040 */
041public class RepositoryWalkingStatisticsProvider implements RepositoryStatisticsProvider
042{
043
044    /**
045     * Walks each namespace of the given repository id and counts the artifacts.
046     *
047     *
048     * @param repositorySession
049     * @param metadataRepository The repository implementation
050     * @param repositoryId The repository Id
051     * @param repositoryStatistics The statistics object that must be populated
052     * @throws MetadataRepositoryException Throws the repository exception, if an error occurs while accessing the repository.
053     */
054    @Override
055    public void populateStatistics( RepositorySession repositorySession, MetadataRepository metadataRepository, String repositoryId,
056                                    RepositoryStatistics repositoryStatistics )
057        throws MetadataRepositoryException
058    {
059        try
060        {
061            for ( String ns : metadataRepository.getRootNamespaces( repositorySession, repositoryId ) )
062            {
063                walkRepository( repositorySession, metadataRepository, repositoryStatistics, repositoryId, ns );
064            }
065        }
066        catch ( MetadataResolutionException e )
067        {
068            throw new MetadataRepositoryException( e.getMessage(), e );
069        }
070    }
071
072    private void walkRepository( RepositorySession repositorySession, MetadataRepository metadataRepository, RepositoryStatistics stats, String repositoryId,
073                                 String ns )
074        throws MetadataResolutionException
075    {
076        for ( String namespace : metadataRepository.getChildNamespaces( repositorySession , repositoryId, ns ) )
077        {
078            walkRepository( repositorySession, metadataRepository, stats, repositoryId, ns + "." + namespace );
079        }
080
081        Collection<String> projects = metadataRepository.getProjects( repositorySession , repositoryId, ns );
082        if ( !projects.isEmpty() )
083        {
084            stats.setTotalGroupCount( stats.getTotalGroupCount() + 1 );
085            stats.setTotalProjectCount( stats.getTotalProjectCount() + projects.size() );
086
087            for ( String project : projects )
088            {
089                for ( String version : metadataRepository.getProjectVersions( repositorySession , repositoryId, ns, project ) )
090                {
091                    for ( ArtifactMetadata artifact : metadataRepository.getArtifacts( repositorySession , repositoryId, ns,
092                        project, version ) )
093                    {
094                        stats.setTotalArtifactCount( stats.getTotalArtifactCount() + 1 );
095                        stats.setTotalArtifactFileSize( stats.getTotalArtifactFileSize() + artifact.getSize() );
096
097                        MavenArtifactFacet facet =
098                            (MavenArtifactFacet) artifact.getFacet( MavenArtifactFacet.FACET_ID );
099                        if ( facet != null )
100                        {
101                            String type = facet.getType();
102                            stats.setTotalCountForType( type, stats.getTotalCountForType( type ) + 1 );
103                        }
104                    }
105                }
106            }
107        }
108    }
109}