This project has retired. For details please refer to its Attic page.
RepositoryWalkingStatisticsProvider xref
View Javadoc
1   package org.apache.archiva.metadata.repository.stats.model;
2   
3   /*
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
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import org.apache.archiva.metadata.model.ArtifactMetadata;
23  import org.apache.archiva.metadata.model.maven2.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;
28  
29  import java.util.Collection;
30  
31  /**
32   *
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.
38   *
39   * @author Martin Stockhammer
40   */
41  public class RepositoryWalkingStatisticsProvider implements RepositoryStatisticsProvider
42  {
43  
44      /**
45       * Walks each namespace of the given repository id and counts the artifacts.
46       *
47       *
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.
53       */
54      @Override
55      public void populateStatistics( RepositorySession repositorySession, MetadataRepository metadataRepository, String repositoryId,
56                                      RepositoryStatistics repositoryStatistics )
57          throws MetadataRepositoryException
58      {
59          try
60          {
61              for ( String ns : metadataRepository.getRootNamespaces( repositorySession, repositoryId ) )
62              {
63                  walkRepository( repositorySession, metadataRepository, repositoryStatistics, repositoryId, ns );
64              }
65          }
66          catch ( MetadataResolutionException e )
67          {
68              throw new MetadataRepositoryException( e.getMessage(), e );
69          }
70      }
71  
72      private void walkRepository( RepositorySession repositorySession, MetadataRepository metadataRepository, RepositoryStatistics stats, String repositoryId,
73                                   String ns )
74          throws MetadataResolutionException
75      {
76          for ( String namespace : metadataRepository.getChildNamespaces( repositorySession , repositoryId, ns ) )
77          {
78              walkRepository( repositorySession, metadataRepository, stats, repositoryId, ns + "." + namespace );
79          }
80  
81          Collection<String> projects = metadataRepository.getProjects( repositorySession , repositoryId, ns );
82          if ( !projects.isEmpty() )
83          {
84              stats.setTotalGroupCount( stats.getTotalGroupCount() + 1 );
85              stats.setTotalProjectCount( stats.getTotalProjectCount() + projects.size() );
86  
87              for ( String project : projects )
88              {
89                  for ( String version : metadataRepository.getProjectVersions( repositorySession , repositoryId, ns, project ) )
90                  {
91                      for ( ArtifactMetadata artifact : metadataRepository.getArtifacts( repositorySession , repositoryId, ns,
92                          project, version ) )
93                      {
94                          stats.setTotalArtifactCount( stats.getTotalArtifactCount() + 1 );
95                          stats.setTotalArtifactFileSize( stats.getTotalArtifactFileSize() + artifact.getSize() );
96  
97                          MavenArtifactFacet facet =
98                              (MavenArtifactFacet) artifact.getFacet( MavenArtifactFacet.FACET_ID );
99                          if ( facet != null )
100                         {
101                             String type = facet.getType();
102                             stats.setTotalCountForType( type, stats.getTotalCountForType( type ) + 1 );
103                         }
104                     }
105                 }
106             }
107         }
108     }
109 }