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 org.apache.commons.io.FileUtils;
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.maven.archiva.configuration.Configuration;
25 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.maven.archiva.security.ArchivaRoleConstants;
27 import org.codehaus.plexus.redback.role.RoleManager;
28 import org.codehaus.plexus.redback.role.RoleManagerException;
29 import org.codehaus.plexus.registry.Registry;
32 import java.io.IOException;
35 * Abstract ManagedRepositories Action.
37 * Place for all generic methods used in Managed Repository Administration.
41 public abstract class AbstractManagedRepositoriesAction
42 extends AbstractRepositoriesAdminAction
45 * @plexus.requirement role-hint="default"
47 protected RoleManager roleManager;
50 * Plexus registry to read the configuration from.
52 * @plexus.requirement role-hint="commons-configuration"
54 private Registry registry;
56 public static final String CONFIRM = "confirm";
58 public RoleManager getRoleManager()
63 public void setRoleManager( RoleManager roleManager )
65 this.roleManager = roleManager;
68 public void setRegistry( Registry registry )
70 this.registry = registry;
73 protected void addRepository( ManagedRepositoryConfiguration repository, Configuration configuration )
77 File file = new File( repository.getLocation() );
78 repository.setLocation( file.getCanonicalPath() );
83 if ( !file.exists() || !file.isDirectory() )
85 throw new IOException( "Unable to add repository - no write access, can not create the root directory: " + file );
88 configuration.addManagedRepository( repository );
92 protected void addRepositoryRoles( ManagedRepositoryConfiguration newRepository ) throws RoleManagerException
94 String repoId = newRepository.getId();
96 // TODO: double check these are configured on start up
97 // TODO: belongs in the business logic
99 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
101 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
104 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
106 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
110 protected void removeContents( ManagedRepositoryConfiguration existingRepository )
113 File dir = new File( existingRepository.getLocation() );
114 if ( dir.exists() && !FileUtils.deleteQuietly( dir ) )
116 throw new IOException( "Cannot delete repository " + dir );
120 protected void removeRepository( String repoId, Configuration configuration )
122 ManagedRepositoryConfiguration toremove = configuration.findManagedRepositoryById( repoId );
123 if ( toremove != null )
125 configuration.removeManagedRepository( toremove );
129 protected void removeRepositoryRoles( ManagedRepositoryConfiguration existingRepository )
130 throws RoleManagerException
132 String repoId = existingRepository.getId();
134 if ( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
136 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
139 if ( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
141 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
144 log.debug( "removed user roles associated with repository " + repoId );
147 protected String removeExpressions( String directory )
149 String value = StringUtils.replace( directory, "${appserver.base}", registry.getString( "appserver.base",
150 "${appserver.base}" ) );
151 value = StringUtils.replace( value, "${appserver.home}", registry.getString( "appserver.home",
152 "${appserver.home}" ) );