]> source.dussan.org Git - archiva.git/blob
d040980efe350ade04a4ef2fba62e6bacd236111
[archiva.git] /
1 package org.apache.maven.archiva.web.action.admin.database;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import java.util.Collections;
23 import java.util.List;
24
25 import org.apache.commons.collections.CollectionUtils;
26 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
27 import org.apache.maven.archiva.configuration.Configuration;
28 import org.apache.maven.archiva.configuration.DatabaseScanningConfiguration;
29 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
30 import org.apache.maven.archiva.database.updater.DatabaseConsumers;
31 import org.apache.maven.archiva.security.ArchivaRoleConstants;
32 import org.apache.maven.archiva.web.action.PlexusActionSupport;
33 import org.codehaus.plexus.redback.rbac.Resource;
34 import org.codehaus.plexus.registry.RegistryException;
35 import org.codehaus.redback.integration.interceptor.SecureAction;
36 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
37 import org.codehaus.redback.integration.interceptor.SecureActionException;
38
39 import com.opensymphony.xwork2.Preparable;
40
41 /**
42  * DatabaseAction
43  *
44  * @version $Id$
45  * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="databaseAction" instantiation-strategy="per-lookup"
46  */
47 public class DatabaseAction
48     extends PlexusActionSupport
49     implements Preparable, SecureAction
50 {
51     /**
52      * @plexus.requirement
53      */
54     private ArchivaConfiguration archivaConfiguration;
55
56     /**
57      * @plexus.requirement
58      */
59     private DatabaseConsumers databaseConsumers;
60
61     private String cron;
62
63     /**
64      * List of available {@link AdminDatabaseConsumer} objects for unprocessed artifacts.
65      */
66     private List<AdminDatabaseConsumer> unprocessedConsumers;
67
68     /**
69      * List of enabled {@link AdminDatabaseConsumer} objects for unprocessed artifacts.
70      */
71     private List<String> enabledUnprocessedConsumers;
72
73     /**
74      * List of {@link AdminDatabaseConsumer} objects for "to cleanup" artifacts.
75      */
76     private List<AdminDatabaseConsumer> cleanupConsumers;
77
78     /**
79      * List of enabled {@link AdminDatabaseConsumer} objects for "to cleanup" artifacts.
80      */
81     private List<String> enabledCleanupConsumers;
82
83     public void prepare()
84         throws Exception
85     {
86         Configuration config = archivaConfiguration.getConfiguration();
87         DatabaseScanningConfiguration dbscanning = config.getDatabaseScanning();
88
89         this.cron = dbscanning.getCronExpression();
90
91         AddAdminDatabaseConsumerClosure addAdminDbConsumer;
92
93         addAdminDbConsumer = new AddAdminDatabaseConsumerClosure( dbscanning.getUnprocessedConsumers() );
94         CollectionUtils.forAllDo( databaseConsumers.getAvailableUnprocessedConsumers(), addAdminDbConsumer );
95         this.unprocessedConsumers = addAdminDbConsumer.getList();
96         Collections.sort( this.unprocessedConsumers, AdminDatabaseConsumerComparator.getInstance() );
97
98         addAdminDbConsumer = new AddAdminDatabaseConsumerClosure( dbscanning.getCleanupConsumers() );
99         CollectionUtils.forAllDo( databaseConsumers.getAvailableCleanupConsumers(), addAdminDbConsumer );
100         this.cleanupConsumers = addAdminDbConsumer.getList();
101         Collections.sort( this.cleanupConsumers, AdminDatabaseConsumerComparator.getInstance() );
102     }
103
104     public String updateUnprocessedConsumers()
105     {
106         archivaConfiguration.getConfiguration().getDatabaseScanning().setUnprocessedConsumers(
107             enabledUnprocessedConsumers );
108
109         return saveConfiguration();
110     }
111
112     public String updateCleanupConsumers()
113     {
114         archivaConfiguration.getConfiguration().getDatabaseScanning().setCleanupConsumers( enabledCleanupConsumers );
115
116         return saveConfiguration();
117     }
118
119     public String updateSchedule()
120     {
121         archivaConfiguration.getConfiguration().getDatabaseScanning().setCronExpression( cron );
122
123         return saveConfiguration();
124     }
125
126     private String saveConfiguration()
127     {
128         try
129         {
130             archivaConfiguration.save( archivaConfiguration.getConfiguration() );
131             addActionMessage( "Successfully saved configuration" );
132         }
133         catch ( RegistryException e )
134         {
135             log.error( e.getMessage(), e );
136             addActionError( "Error in saving configuration" );
137             return INPUT;
138         }
139         catch ( IndeterminateConfigurationException e )
140         {
141             addActionError( e.getMessage() );
142             return INPUT;
143         }
144
145         return SUCCESS;
146     }
147
148     public SecureActionBundle getSecureActionBundle()
149         throws SecureActionException
150     {
151         SecureActionBundle bundle = new SecureActionBundle();
152
153         bundle.setRequiresAuthentication( true );
154         bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION, Resource.GLOBAL );
155
156         return bundle;
157     }
158
159     public String getCron()
160     {
161         return cron;
162     }
163
164     public void setCron( String cron )
165     {
166         this.cron = cron;
167     }
168
169     public List<AdminDatabaseConsumer> getCleanupConsumers()
170     {
171         return cleanupConsumers;
172     }
173
174     public List<AdminDatabaseConsumer> getUnprocessedConsumers()
175     {
176         return unprocessedConsumers;
177     }
178
179     public List<String> getEnabledUnprocessedConsumers()
180     {
181         return enabledUnprocessedConsumers;
182     }
183
184     public void setEnabledUnprocessedConsumers( List<String> enabledUnprocessedConsumers )
185     {
186         this.enabledUnprocessedConsumers = enabledUnprocessedConsumers;
187     }
188
189     public List<String> getEnabledCleanupConsumers()
190     {
191         return enabledCleanupConsumers;
192     }
193
194     public void setEnabledCleanupConsumers( List<String> enabledCleanupConsumers )
195     {
196         this.enabledCleanupConsumers = enabledCleanupConsumers;
197     }
198 }