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