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