]> source.dussan.org Git - archiva.git/blob
99f7ff9f19259c9a01ceb915fe37ae989551f5ee
[archiva.git] /
1 package org.apache.archiva.redback.struts2.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 com.opensymphony.xwork2.Preparable;
23 import org.apache.archiva.redback.rbac.RBACManager;
24 import org.apache.archiva.redback.rbac.Resource;
25 import org.apache.archiva.redback.struts2.action.AbstractSecurityAction;
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.archiva.redback.keys.KeyManager;
28 import org.apache.archiva.redback.management.DataManagementTool;
29 import org.apache.archiva.redback.users.UserManager;
30 import org.apache.archiva.redback.integration.interceptor.SecureAction;
31 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
32 import org.apache.archiva.redback.integration.interceptor.SecureActionException;
33 import org.apache.archiva.redback.integration.role.RoleConstants;
34 import org.springframework.context.annotation.Scope;
35 import org.springframework.stereotype.Controller;
36
37 import javax.inject.Inject;
38 import javax.inject.Named;
39 import java.io.File;
40 import java.io.IOException;
41 import java.text.SimpleDateFormat;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.Date;
45 import java.util.List;
46 import java.util.Locale;
47
48
49 /**
50  * BackupRestoreAction
51  */
52 @Controller( "backup-restore" )
53 @Scope( "prototype" )
54 public class BackupRestoreAction
55     extends AbstractSecurityAction
56     implements SecureAction, Preparable
57 {
58     public final static String CUSTOM_ERROR = "custom_error";
59
60     /**
61      *
62      */
63     private File applicationHome = new File( "data" );
64
65     /**
66      * role-hint="jdo"
67      */
68     @Inject
69     private DataManagementTool dataManagementTool;
70
71     /**
72      * role-hint="jdo"
73      */
74     @Inject
75     @Named( value = "rBACManager#jdo" )
76     private RBACManager rbacManager;
77
78     /**
79      * role-hint="jdo"
80      */
81     @Inject
82     @Named( value = "userManager#jdo" )
83     private UserManager userManager;
84
85     /**
86      * role-hint="jdo"
87      */
88     @Inject
89     @Named( value = "keyManager#jdo" )
90     private KeyManager keyManager;
91
92     private File backupDirectory;
93
94     private String restoreDirectory;
95
96     private List<BackupRecord> previousBackups;
97
98     private boolean confirmed;
99
100     public static final String BACKUP_DIRECTORY = "user-backup-directory";
101
102     public String view()
103         throws Exception
104     {
105
106         retrievePreviousBackups();
107
108         return SUCCESS;
109     }
110
111     public String backup()
112         throws Exception
113     {
114
115         File backupDirectory = getTimestampedBackupDirectory();
116         backupDirectory.mkdirs();
117
118         log.info( "Backing up security database to {}", backupDirectory );
119         this.backupDatabase( backupDirectory );
120
121         log.info( "Done backing up security database" );
122
123         return SUCCESS;
124     }
125
126     public String restore()
127         throws Exception
128     {
129         if ( StringUtils.isEmpty( restoreDirectory ) )
130         {
131             addActionError( getText( "backupRestore.backup.empty.error" ) );
132             return CUSTOM_ERROR;
133         }
134
135         File restoreDirectory = new File( this.restoreDirectory );
136
137         boolean fileExists = restoreDirectory.exists() && restoreDirectory.isDirectory();
138         boolean isValidBackup = false;
139
140         if ( fileExists )
141         {
142             BackupRecord record = new BackupRecord( restoreDirectory );
143             isValidBackup = record.isValidBackup();
144         }
145
146         if ( !fileExists )
147         {
148             log.warn( "Backup: " + this.restoreDirectory + " not found." );
149             addActionError( getText( "backupRestore.backup.error" ) );
150             retrievePreviousBackups();
151             return CUSTOM_ERROR;
152         }
153         else if ( !isValidBackup )
154         {
155             log.warn( "Backup: " + this.restoreDirectory + " is not a valid backup directory." );
156             addActionError( getText( "backupRestore.backup.error" ) );
157             retrievePreviousBackups();
158             return CUSTOM_ERROR;
159         }
160
161         log.info( "Restoring security database from {}", this.restoreDirectory );
162         this.eraseDatabase();
163         this.restoreDatabase( restoreDirectory );
164         log.info( "Done restoring security database" );
165
166         return SUCCESS;
167     }
168
169
170     private void backupDatabase( File backupDirectory )
171         throws Exception
172     {
173
174         dataManagementTool.backupKeyDatabase( keyManager, backupDirectory );
175         dataManagementTool.backupRBACDatabase( rbacManager, backupDirectory );
176         dataManagementTool.backupUserDatabase( userManager, backupDirectory );
177     }
178
179     private void eraseDatabase()
180     {
181         dataManagementTool.eraseKeysDatabase( keyManager );
182         dataManagementTool.eraseRBACDatabase( rbacManager );
183         dataManagementTool.eraseUsersDatabase( userManager );
184     }
185
186     private void restoreDatabase( File backupDirectory )
187         throws Exception
188     {
189
190         dataManagementTool.restoreKeysDatabase( keyManager, backupDirectory );
191         dataManagementTool.restoreRBACDatabase( rbacManager, backupDirectory );
192         dataManagementTool.restoreUsersDatabase( userManager, backupDirectory );
193     }
194
195     public String getRestoreDirectory()
196     {
197         return restoreDirectory;
198     }
199
200     public void setRestoreDirectory( String restoreDirectory )
201     {
202         this.restoreDirectory = restoreDirectory;
203     }
204
205     private File getTimestampedBackupDirectory()
206     {
207         SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMdd.HHmmss", Locale.US );
208         return new File( this.backupDirectory, dateFormat.format( new Date() ) );
209     }
210
211     public File getBackupDirectory()
212     {
213         return backupDirectory;
214     }
215
216     public List<BackupRecord> getPreviousBackups()
217     {
218         return previousBackups;
219     }
220
221     public void prepare()
222     {
223         backupDirectory = this.getFile( BACKUP_DIRECTORY );
224         retrievePreviousBackups();
225     }
226
227     private void retrievePreviousBackups()
228     {
229         previousBackups = new ArrayList<BackupRecord>();
230         File[] files = backupDirectory.listFiles();
231         if ( files != null )
232         {
233             for ( int i = 0; i < files.length; i++ )
234             {
235                 File f = files[i];
236
237                 if ( f.isDirectory() && !f.getName().startsWith( "." ) )
238                 {
239                     BackupRecord record = new BackupRecord( f );
240
241                     if ( record.isValidBackup() )
242                     {
243                         previousBackups.add( record );
244                     }
245                 }
246             }
247         }
248         Collections.sort( previousBackups );
249     }
250
251     public boolean isConfirmed()
252     {
253         return confirmed;
254     }
255
256     public void setConfirmed( boolean confirmed )
257     {
258         this.confirmed = confirmed;
259     }
260
261     @Override
262     public SecureActionBundle initSecureActionBundle()
263         throws SecureActionException
264     {
265         SecureActionBundle bundle = new SecureActionBundle();
266         bundle.setRequiresAuthentication( true );
267         bundle.addRequiredAuthorization( RoleConstants.USER_MANAGEMENT_MANAGE_DATA, Resource.GLOBAL );
268         return bundle;
269     }
270
271     public File getFile( String filename )
272     {
273         if ( filename == null )
274         {
275             return null;
276         }
277
278         File f = null;
279
280         if ( filename != null && filename.length() != 0 )
281         {
282             f = new File( filename );
283
284             if ( !f.isAbsolute() )
285             {
286                 f = new File( applicationHome, filename );
287             }
288         }
289
290         try
291         {
292             return f.getCanonicalFile();
293         }
294         catch ( IOException e )
295         {
296             return f;
297         }
298     }
299 }