]> source.dussan.org Git - archiva.git/blob
c81b7c2345da0e826bdac8748935451473f4a09c
[archiva.git] /
1 package org.apache.maven.archiva.web.action.admin;
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 org.apache.commons.lang.StringUtils;
23 import org.apache.maven.archiva.common.ArchivaException;
24 import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler;
25 import org.apache.maven.archiva.scheduled.DefaultArchivaTaskScheduler;
26 import org.apache.maven.archiva.scheduled.tasks.ArchivaTask;
27 import org.apache.maven.archiva.scheduled.tasks.DatabaseTask;
28 import org.apache.maven.archiva.scheduled.tasks.RepositoryTask;
29 import org.apache.maven.archiva.scheduled.tasks.TaskCreator;
30 import org.apache.maven.archiva.security.ArchivaRoleConstants;
31 import org.apache.maven.archiva.web.action.PlexusActionSupport;
32 import org.codehaus.plexus.redback.rbac.Resource;
33 import org.codehaus.plexus.taskqueue.TaskQueueException;
34 import org.codehaus.redback.integration.interceptor.SecureAction;
35 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
36 import org.codehaus.redback.integration.interceptor.SecureActionException;
37
38 /**
39  * Configures the application.
40  *
41  * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="schedulerAction" instantiation-strategy="per-lookup"
42  */
43 public class SchedulerAction
44     extends PlexusActionSupport
45     implements SecureAction
46 {
47     /**
48      * @plexus.requirement
49      */
50     private ArchivaTaskScheduler taskScheduler;
51
52     private String repoid;
53
54     public String scanRepository()
55     {
56         if ( StringUtils.isBlank( repoid ) )
57         {
58             addActionError( "Cannot run indexer on blank repository id." );
59             return SUCCESS;
60         }
61
62         RepositoryTask task = TaskCreator.createRepositoryTask( repoid, "" ); 
63         
64         boolean scheduleTask = false;
65
66         try
67         {
68             if ( taskScheduler.isProcessingAnyRepositoryTask() )
69             {
70                 if ( taskScheduler.isProcessingRepositoryTask( repoid ) )
71                 {
72                     addActionError( "Repository [" + repoid + "] task was already queued." );
73                 }
74                 else
75                 {
76                     scheduleTask = true;
77                 }
78             }
79             else
80             {
81                 scheduleTask = true;
82             }
83         }
84         catch ( ArchivaException e )
85         {
86             scheduleTask = false;
87             addActionError( e.getMessage() );
88         }
89
90         if ( scheduleTask )
91         {
92             try
93             {
94                 taskScheduler.queueRepositoryTask( task );
95                 addActionMessage( "Your request to have repository [" + repoid + "] be indexed has been queued." );
96             }
97             catch ( TaskQueueException e )
98             {
99                 addActionError( "Unable to queue your request to have repository [" + repoid + "] be indexed: "
100                     + e.getMessage() );
101             }
102         }
103
104         // Return to the repositories screen.
105         return SUCCESS;
106     }
107
108     public String updateDatabase()
109     {
110         DatabaseTask task = new DatabaseTask();
111         task.setName( DefaultArchivaTaskScheduler.DATABASE_JOB + ":user-requested" );
112         task.setQueuePolicy( ArchivaTask.QUEUE_POLICY_WAIT );
113
114         boolean scheduleTask = false;
115
116         try
117         {
118             if ( taskScheduler.isProcessingDatabaseTask() )
119             {
120                 addActionError( "Database task was already queued." );
121             }
122             else
123             {
124                 scheduleTask = true;
125             }
126         }
127         catch ( ArchivaException e )
128         {
129             scheduleTask = false;
130             addActionError( e.getMessage() );
131         }
132
133         if ( scheduleTask )
134         {
135             try
136             {
137                 taskScheduler.queueDatabaseTask( task );
138                 addActionMessage( "Your request to update the database has been queued." );
139             }
140             catch ( TaskQueueException e )
141             {
142                 addActionError( "Unable to queue your request to update the database: " + e.getMessage() );
143             }
144         }
145
146         // Return to the database screen.
147         return SUCCESS;
148     }
149
150     @Override
151     public void addActionMessage( String aMessage )
152     {
153         super.addActionMessage( aMessage );
154         log.info( "[ActionMessage] " + aMessage );
155     }
156
157     @Override
158     public void addActionError( String anErrorMessage )
159     {
160         super.addActionError( anErrorMessage );
161         log.warn( "[ActionError] " + anErrorMessage );
162     }
163
164     public SecureActionBundle getSecureActionBundle()
165         throws SecureActionException
166     {
167         SecureActionBundle bundle = new SecureActionBundle();
168
169         bundle.setRequiresAuthentication( true );
170         bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_RUN_INDEXER, Resource.GLOBAL );
171
172         return bundle;
173     }
174
175     public String getRepoid()
176     {
177         return repoid;
178     }
179
180     public void setRepoid( String repoid )
181     {
182         this.repoid = repoid;
183     }
184 }