1 package org.apache.maven.archiva.web.action.admin.repositories;
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 com.opensymphony.xwork2.Preparable;
23 import org.apache.archiva.metadata.repository.MetadataRepository;
24 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
25 import org.apache.archiva.metadata.repository.RepositorySession;
26 import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
27 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
28 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
29 import org.apache.maven.archiva.configuration.Configuration;
30 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
32 import org.apache.maven.archiva.configuration.functors.RepositoryConfigurationComparator;
33 import org.apache.archiva.security.ArchivaRoleConstants;
34 import org.apache.maven.archiva.web.action.AbstractActionSupport;
35 import org.apache.archiva.web.util.ContextUtils;
36 import org.apache.struts2.interceptor.ServletRequestAware;
37 import org.codehaus.plexus.redback.rbac.Resource;
38 import org.codehaus.redback.integration.interceptor.SecureAction;
39 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
40 import org.codehaus.redback.integration.interceptor.SecureActionException;
41 import org.springframework.context.annotation.Scope;
42 import org.springframework.stereotype.Controller;
44 import java.util.ArrayList;
45 import java.util.Collections;
46 import java.util.HashMap;
47 import java.util.List;
49 import javax.inject.Inject;
50 import javax.servlet.http.HttpServletRequest;
53 * Shows the Repositories Tab for the administrator.
56 * plexus.component role="com.opensymphony.xwork2.Action" role-hint="repositoriesAction" instantiation-strategy="per-lookup"
58 @Controller( "repositoriesAction" )
60 public class RepositoriesAction
61 extends AbstractActionSupport
62 implements SecureAction, ServletRequestAware, Preparable
68 private ArchivaConfiguration archivaConfiguration;
70 private List<ManagedRepositoryConfiguration> managedRepositories;
72 private List<RemoteRepositoryConfiguration> remoteRepositories;
74 private Map<String, RepositoryStatistics> repositoryStatistics;
76 private Map<String, List<String>> repositoryToGroupMap;
79 * Used to construct the repository WebDAV URL in the repository action.
81 private String baseUrl;
87 private RepositoryStatisticsManager repositoryStatisticsManager;
89 public void setServletRequest( HttpServletRequest request )
91 // TODO: is there a better way to do this?
92 this.baseUrl = ContextUtils.getBaseURL( request, "repository" );
95 public SecureActionBundle getSecureActionBundle()
96 throws SecureActionException
98 SecureActionBundle bundle = new SecureActionBundle();
100 bundle.setRequiresAuthentication( true );
101 bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION, Resource.GLOBAL );
106 @SuppressWarnings( "unchecked" )
107 public void prepare()
109 Configuration config = archivaConfiguration.getConfiguration();
111 remoteRepositories = new ArrayList<RemoteRepositoryConfiguration>( config.getRemoteRepositories() );
112 managedRepositories = new ArrayList<ManagedRepositoryConfiguration>( config.getManagedRepositories() );
113 repositoryToGroupMap = config.getRepositoryToGroupMap();
115 Collections.sort( managedRepositories, new RepositoryConfigurationComparator() );
116 Collections.sort( remoteRepositories, new RepositoryConfigurationComparator() );
118 repositoryStatistics = new HashMap<String, RepositoryStatistics>();
119 RepositorySession repositorySession = repositorySessionFactory.createSession();
122 MetadataRepository metadataRepository = repositorySession.getRepository();
123 for ( ManagedRepositoryConfiguration repo : managedRepositories )
125 RepositoryStatistics stats = null;
128 stats = repositoryStatisticsManager.getLastStatistics( metadataRepository, repo.getId() );
130 catch ( MetadataRepositoryException e )
133 "Error retrieving statistics for repository " + repo.getId() + " - consult application logs" );
134 log.warn( "Error retrieving repository statistics: " + e.getMessage(), e );
138 repositoryStatistics.put( repo.getId(), stats );
144 repositorySession.close();
148 public List<ManagedRepositoryConfiguration> getManagedRepositories()
150 List<ManagedRepositoryConfiguration> managedRepositoriesList = new ArrayList<ManagedRepositoryConfiguration>();
151 for ( ManagedRepositoryConfiguration repoConfig : managedRepositories )
153 if ( !repoConfig.getId().endsWith( "-stage" ) )
155 managedRepositoriesList.add( repoConfig );
158 return managedRepositoriesList;
161 public List<RemoteRepositoryConfiguration> getRemoteRepositories()
163 return remoteRepositories;
166 public Map<String, RepositoryStatistics> getRepositoryStatistics()
168 return repositoryStatistics;
171 public String getBaseUrl()
176 public Map<String, List<String>> getRepositoryToGroupMap()
178 return repositoryToGroupMap;