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 ManagedRepositoryConfiguration repository;
51 * @plexus.requirement role-hint="default"
53 protected RoleManager roleManager;
59 this.repository.setReleases( true );
60 this.repository.setScanned( true );
65 public String delete()
67 String result = SUCCESS;
68 if ( StringUtils.equals( mode, "delete-entry" ) || StringUtils.equals( mode, "delete-contents" ) )
70 ManagedRepositoryConfiguration existingRepository = repository;
71 if ( existingRepository == null )
73 addActionError( "A repository with that id does not exist" );
79 Configuration configuration = archivaConfiguration.getConfiguration();
80 removeRepository( repoid, configuration );
81 result = saveConfiguration( configuration );
83 if ( result.equals( SUCCESS ) )
85 removeRepositoryRoles( existingRepository );
86 if ( StringUtils.equals( mode, "delete-contents" ) )
88 removeContents( existingRepository );
92 catch ( IOException e )
94 addActionError( "Unable to delete repository: " + e.getMessage() );
97 catch ( RoleManagerException e )
99 addActionError( "Unable to delete repository: " + e.getMessage() );
102 catch ( InvalidConfigurationException e )
104 addActionError( "Unable to delete repository: " + e.getMessage() );
107 catch ( RegistryException e )
109 addActionError( "Unable to delete repository: " + e.getMessage() );
117 public ManagedRepositoryConfiguration getRepository()
122 public void prepare()
127 this.repository = new ManagedRepositoryConfiguration();
128 this.repository.setReleases( false );
129 this.repository.setScanned( false );
133 repository = archivaConfiguration.getConfiguration().findManagedRepositoryById( id );
139 String repoId = repository.getId();
141 Configuration configuration = archivaConfiguration.getConfiguration();
142 boolean containsError = validateFields( configuration );
144 if ( containsError && StringUtils.equalsIgnoreCase( "add", mode ) )
148 else if ( containsError && StringUtils.equalsIgnoreCase( "edit", this.mode ) )
153 if ( StringUtils.equalsIgnoreCase( "edit", this.mode ) )
155 removeRepository( repoId, configuration );
161 addRepository( repository, configuration );
162 result = saveConfiguration( configuration );
164 catch ( IOException e )
166 addActionError( "I/O Exception: " + e.getMessage() );
169 catch ( RoleManagerException e )
171 addActionError( "Role Manager Exception: " + e.getMessage() );
174 catch ( InvalidConfigurationException e )
176 addActionError( "Invalid Configuration Exception: " + e.getMessage() );
179 catch ( RegistryException e )
181 addActionError( "Configuration Registry Exception: " + e.getMessage() );
188 private boolean validateFields( Configuration config )
190 boolean containsError = false;
191 CronExpressionValidator validator = new CronExpressionValidator();
192 String repoId = repository.getId();
194 if ( StringUtils.isBlank( repoId ) )
196 addFieldError( "repository.id", "You must enter a repository identifier." );
197 containsError = true;
199 //if edit mode, do not validate existence of repoId
200 else if ( ( config.getManagedRepositoriesAsMap().containsKey( repoId ) ||
201 config.getRemoteRepositoriesAsMap().containsKey( repoId ) ) &&
202 !StringUtils.equalsIgnoreCase( mode, "edit" ) )
204 addFieldError( "repository.id",
205 "Unable to add new repository with id [" + repoId + "], that id already exists." );
206 containsError = true;
209 if ( StringUtils.isBlank( repository.getLocation() ) )
211 addFieldError( "repository.location", "You must enter a directory." );
212 containsError = true;
214 if ( StringUtils.isBlank( repository.getName() ) )
216 addFieldError( "repository.name", "You must enter a repository name." );
217 containsError = true;
219 if ( !validator.validate( repository.getRefreshCronExpression() ) )
221 addFieldError( "repository.refreshCronExpression", "Invalid cron expression." );
222 containsError = true;
225 return containsError;
228 private void addRepository( ManagedRepositoryConfiguration repository, Configuration configuration )
229 throws IOException, RoleManagerException
231 // Normalize the path
232 File file = new File( repository.getLocation() );
233 repository.setLocation( file.getCanonicalPath() );
234 if ( !file.exists() )
238 if ( !file.exists() || !file.isDirectory() )
240 throw new IOException( "unable to add repository - can not create the root directory: " + file );
243 configuration.addManagedRepository( repository );
245 // TODO: double check these are configured on start up
246 // TODO: belongs in the business logic
247 roleManager.createTemplatedRole( "archiva-repository-manager", repository.getId() );
249 roleManager.createTemplatedRole( "archiva-repository-observer", repository.getId() );
252 private void removeContents( ManagedRepositoryConfiguration existingRepository )
255 FileUtils.deleteDirectory( new File( existingRepository.getLocation() ) );
258 private void removeRepository( String repoId, Configuration configuration )
260 ManagedRepositoryConfiguration toremove = configuration.findManagedRepositoryById( repoId );
261 if ( toremove != null )
263 configuration.removeManagedRepository( toremove );
267 private void removeRepositoryRoles( ManagedRepositoryConfiguration existingRepository )
268 throws RoleManagerException
270 roleManager.removeTemplatedRole( "archiva-repository-manager", existingRepository.getId() );
271 roleManager.removeTemplatedRole( "archiva-repository-observer", existingRepository.getId() );
273 getLogger().debug( "removed user roles associated with repository " + existingRepository.getId() );
276 public void setRoleManager( RoleManager roleManager )
278 this.roleManager = roleManager;