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.xwork.Preparable;
23 import org.apache.commons.io.FileUtils;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.maven.archiva.configuration.Configuration;
26 import org.apache.maven.archiva.configuration.InvalidConfigurationException;
27 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.codehaus.plexus.redback.role.RoleManager;
29 import org.codehaus.plexus.redback.role.RoleManagerException;
30 import org.codehaus.plexus.registry.RegistryException;
31 import org.codehaus.plexus.scheduler.CronExpressionValidator;
34 import java.io.IOException;
37 * Configures the application repositories.
39 * @plexus.component role="com.opensymphony.xwork.Action" role-hint="configureRepositoryAction"
41 public class ConfigureRepositoryAction
42 extends AbstractConfigureRepositoryAction
46 * The model for this action.
48 private AdminRepositoryConfiguration repository;
51 * @plexus.requirement role-hint="default"
53 protected RoleManager roleManager;
59 this.repository.setReleases( true );
60 this.repository.setIndexed( true );
65 public String delete()
67 String result = SUCCESS;
68 if ( StringUtils.equals( mode, "delete-entry" ) || StringUtils.equals( mode, "delete-contents" ) )
70 AdminRepositoryConfiguration existingRepository = repository;
71 if ( existingRepository == null )
73 addActionError( "A repository with that id does not exist" );
77 // TODO: remove from index too!
81 Configuration configuration = archivaConfiguration.getConfiguration();
82 removeRepository( repoid, configuration );
83 result = saveConfiguration( configuration );
85 if ( result.equals( SUCCESS ) )
87 removeRepositoryRoles( existingRepository );
88 if ( StringUtils.equals( mode, "delete-contents" ) )
90 removeContents( existingRepository );
94 catch ( IOException e )
96 addActionError( "Unable to delete repository: " + e.getMessage() );
99 catch ( RoleManagerException e )
101 addActionError( "Unable to delete repository: " + e.getMessage() );
104 catch ( InvalidConfigurationException e )
106 addActionError( "Unable to delete repository: " + e.getMessage() );
109 catch ( RegistryException e )
111 addActionError( "Unable to delete repository: " + e.getMessage() );
119 public AdminRepositoryConfiguration getRepository()
124 public void prepare()
129 this.repository = new AdminRepositoryConfiguration();
130 this.repository.setReleases( false );
131 this.repository.setIndexed( false );
134 ManagedRepositoryConfiguration repoconfig =
135 archivaConfiguration.getConfiguration().findManagedRepositoryById( id );
136 if ( repoconfig != null )
138 this.repository = new AdminRepositoryConfiguration( repoconfig );
144 String repoId = repository.getId();
146 Configuration configuration = archivaConfiguration.getConfiguration();
147 boolean containsError = validateFields( configuration );
149 if ( containsError && StringUtils.equalsIgnoreCase( "add", mode ) )
153 else if ( containsError && StringUtils.equalsIgnoreCase( "edit", this.mode ) )
158 if ( StringUtils.equalsIgnoreCase( "edit", this.mode ) )
160 removeRepository( repoId, configuration );
166 addRepository( repository, configuration );
167 result = saveConfiguration( configuration );
169 catch ( IOException e )
171 addActionError( "I/O Exception: " + e.getMessage() );
174 catch ( RoleManagerException e )
176 addActionError( "Role Manager Exception: " + e.getMessage() );
179 catch ( InvalidConfigurationException e )
181 addActionError( "Invalid Configuration Exception: " + e.getMessage() );
184 catch ( RegistryException e )
186 addActionError( "Configuration Registry Exception: " + e.getMessage() );
193 private boolean validateFields( Configuration config )
195 boolean containsError = false;
196 CronExpressionValidator validator = new CronExpressionValidator();
197 String repoId = repository.getId();
199 if ( StringUtils.isBlank( repoId ) )
201 addFieldError( "repository.id", "You must enter a repository identifier." );
202 containsError = true;
204 //if edit mode, do not validate existence of repoId
205 else if ( ( config.getManagedRepositoriesAsMap().containsKey( repoId ) ||
206 config.getRemoteRepositoriesAsMap().containsKey( repoId ) ) &&
207 !StringUtils.equalsIgnoreCase( mode, "edit" ) )
209 addFieldError( "repository.id",
210 "Unable to add new repository with id [" + repoId + "], that id already exists." );
211 containsError = true;
214 if ( StringUtils.isBlank( repository.getLocation() ) )
216 addFieldError( "repository.location", "You must enter a directory." );
217 containsError = true;
219 if ( StringUtils.isBlank( repository.getName() ) )
221 addFieldError( "repository.name", "You must enter a repository name." );
222 containsError = true;
224 if ( !validator.validate( repository.getRefreshCronExpression() ) )
226 addFieldError( "repository.refreshCronExpression", "Invalid cron expression." );
227 containsError = true;
230 return containsError;
233 private void addRepository( AdminRepositoryConfiguration repository, Configuration configuration )
234 throws IOException, RoleManagerException
236 // Normalize the path
237 File file = new File( repository.getLocation() );
238 repository.setLocation( file.getCanonicalPath() );
239 if ( !file.exists() )
242 // TODO: error handling when this fails, or is not a directory!
245 configuration.addManagedRepository( repository );
247 // TODO: double check these are configured on start up
248 // TODO: belongs in the business logic
249 roleManager.createTemplatedRole( "archiva-repository-manager", repository.getId() );
251 roleManager.createTemplatedRole( "archiva-repository-observer", repository.getId() );
254 private void removeContents( AdminRepositoryConfiguration existingRepository )
257 FileUtils.deleteDirectory( new File( existingRepository.getLocation() ) );
260 private void removeRepository( String repoId, Configuration configuration )
262 ManagedRepositoryConfiguration toremove = configuration.findManagedRepositoryById( repoId );
263 if ( toremove != null )
265 configuration.removeManagedRepository( toremove );
269 private void removeRepositoryRoles( ManagedRepositoryConfiguration existingRepository )
270 throws RoleManagerException
272 roleManager.removeTemplatedRole( "archiva-repository-manager", existingRepository.getId() );
273 roleManager.removeTemplatedRole( "archiva-repository-observer", existingRepository.getId() );
275 getLogger().debug( "removed user roles associated with repository " + existingRepository.getId() );
278 public void setRoleManager( RoleManager roleManager )
280 this.roleManager = roleManager;