1 package org.apache.maven.archiva.web.action.admin.scanning;
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 com.opensymphony.xwork2.Validateable;
24 import org.apache.commons.collections.CollectionUtils;
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
27 import org.apache.maven.archiva.configuration.Configuration;
28 import org.apache.maven.archiva.configuration.FileType;
29 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
30 import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
31 import org.apache.maven.archiva.configuration.functors.FiletypeSelectionPredicate;
32 import org.apache.maven.archiva.configuration.functors.FiletypeToMapClosure;
33 import org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers;
34 import org.apache.maven.archiva.security.ArchivaRoleConstants;
35 import org.apache.maven.archiva.web.action.PlexusActionSupport;
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.codehaus.plexus.registry.RegistryException;
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.List;
49 * RepositoryScanningAction
52 * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="repositoryScanningAction"
54 public class RepositoryScanningAction
55 extends PlexusActionSupport
56 implements Preparable, Validateable, SecureAction
61 private ArchivaConfiguration archivaConfiguration;
66 private RepositoryContentConsumers repoconsumerUtil;
68 private Map fileTypeMap;
70 private List fileTypeIds;
73 * List of {@link AdminRepositoryConsumer} objects for consumers of known content.
75 private List knownContentConsumers;
78 * List of enabled {@link AdminRepositoryConsumer} objects for consumers of known content.
80 private List enabledKnownContentConsumers;
83 * List of {@link AdminRepositoryConsumer} objects for consumers of invalid/unknown content.
85 private List invalidContentConsumers;
88 * List of enabled {@link AdminRepositoryConsumer} objects for consumers of invalid/unknown content.
90 private List enabledInvalidContentConsumers;
92 private String pattern;
94 private String fileTypeId;
96 public void addActionError( String anErrorMessage )
98 super.addActionError( anErrorMessage );
99 getLogger().warn( "[ActionError] " + anErrorMessage );
102 public void addActionMessage( String aMessage )
104 super.addActionMessage( aMessage );
105 getLogger().info( "[ActionMessage] " + aMessage );
108 public String addFiletypePattern()
110 getLogger().info( "Add New File Type Pattern [" + getFileTypeId() + ":" + getPattern() + "]" );
112 if ( !isValidFiletypeCommand() )
117 String id = getFileTypeId();
118 String pattern = getPattern();
120 FileType filetype = findFileType( id );
121 if ( filetype == null )
123 addActionError( "Pattern not added, unable to find filetype " + id );
127 if ( filetype.getPatterns().contains( pattern ) )
129 addActionError( "Not adding pattern \"" + pattern + "\" to filetype " + id + " as it already exists." );
133 filetype.addPattern( pattern );
134 addActionMessage( "Added pattern \"" + pattern + "\" to filetype " + id );
136 return saveConfiguration();
139 public String getFileTypeId()
144 public List getFileTypeIds()
149 public Map getFileTypeMap()
154 public List getInvalidContentConsumers()
156 return invalidContentConsumers;
159 public List getKnownContentConsumers()
161 return knownContentConsumers;
164 public String getPattern()
169 public SecureActionBundle getSecureActionBundle()
170 throws SecureActionException
172 SecureActionBundle bundle = new SecureActionBundle();
174 bundle.setRequiresAuthentication( true );
175 bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION, Resource.GLOBAL );
180 public void prepare()
183 Configuration config = archivaConfiguration.getConfiguration();
184 RepositoryScanningConfiguration reposcanning = config.getRepositoryScanning();
186 FiletypeToMapClosure filetypeToMapClosure = new FiletypeToMapClosure();
188 CollectionUtils.forAllDo( reposcanning.getFileTypes(), filetypeToMapClosure );
189 fileTypeMap = filetypeToMapClosure.getMap();
191 AddAdminRepoConsumerClosure addAdminRepoConsumer;
193 addAdminRepoConsumer = new AddAdminRepoConsumerClosure( reposcanning.getKnownContentConsumers() );
194 CollectionUtils.forAllDo( repoconsumerUtil.getAvailableKnownConsumers(), addAdminRepoConsumer );
195 this.knownContentConsumers = addAdminRepoConsumer.getList();
196 Collections.sort( knownContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
198 addAdminRepoConsumer = new AddAdminRepoConsumerClosure( reposcanning.getInvalidContentConsumers() );
199 CollectionUtils.forAllDo( repoconsumerUtil.getAvailableInvalidConsumers(), addAdminRepoConsumer );
200 this.invalidContentConsumers = addAdminRepoConsumer.getList();
201 Collections.sort( invalidContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
203 fileTypeIds = new ArrayList();
204 fileTypeIds.addAll( fileTypeMap.keySet() );
205 Collections.sort( fileTypeIds );
208 public String removeFiletypePattern()
210 getLogger().info( "Remove File Type Pattern [" + getFileTypeId() + ":" + getPattern() + "]" );
212 if ( !isValidFiletypeCommand() )
217 FileType filetype = findFileType( getFileTypeId() );
218 if ( filetype == null )
220 addActionError( "Pattern not removed, unable to find filetype " + getFileTypeId() );
224 filetype.removePattern( getPattern() );
226 return saveConfiguration();
229 public void setFileTypeId( String fileTypeId )
231 this.fileTypeId = fileTypeId;
234 public void setPattern( String pattern )
236 this.pattern = pattern;
239 public String updateInvalidConsumers()
241 addActionMessage( "Update Invalid Consumers" );
243 archivaConfiguration.getConfiguration().getRepositoryScanning().setInvalidContentConsumers(
244 enabledInvalidContentConsumers );
246 return saveConfiguration();
249 public String updateKnownConsumers()
251 addActionMessage( "Update Known Consumers" );
253 archivaConfiguration.getConfiguration().getRepositoryScanning().setKnownContentConsumers(
254 enabledKnownContentConsumers );
256 return saveConfiguration();
259 private FileType findFileType( String id )
261 RepositoryScanningConfiguration scanning = archivaConfiguration.getConfiguration().getRepositoryScanning();
262 return (FileType) CollectionUtils.find( scanning.getFileTypes(), new FiletypeSelectionPredicate( id ) );
265 private boolean isValidFiletypeCommand()
267 if ( StringUtils.isBlank( getFileTypeId() ) )
269 addActionError( "Unable to process blank filetype id." );
272 if ( StringUtils.isBlank( getPattern() ) )
274 addActionError( "Unable to process blank pattern." );
277 return !hasActionErrors();
280 private String saveConfiguration()
284 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
285 addActionMessage( "Successfully saved configuration" );
287 catch ( RegistryException e )
289 addActionError( "Unable to save configuration: " + e.getMessage() );
292 catch ( IndeterminateConfigurationException e )
294 addActionError( e.getMessage() );
301 public List getEnabledInvalidContentConsumers()
303 return enabledInvalidContentConsumers;
306 public void setEnabledInvalidContentConsumers( List enabledInvalidContentConsumers )
308 this.enabledInvalidContentConsumers = enabledInvalidContentConsumers;
311 public List getEnabledKnownContentConsumers()
313 return enabledKnownContentConsumers;
316 public void setEnabledKnownContentConsumers( List enabledKnownContentConsumers )
318 this.enabledKnownContentConsumers = enabledKnownContentConsumers;