]> source.dussan.org Git - archiva.git/blob
0b9578efdd421965fb9972f254363c7c2e9164df
[archiva.git] /
1 package org.apache.archiva.redback.management;
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.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.FileOutputStream;
25 import java.io.FileReader;
26 import java.io.IOException;
27 import java.io.OutputStreamWriter;
28 import java.io.Writer;
29 import java.nio.charset.Charset;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34
35 import javax.xml.stream.XMLStreamException;
36
37 import org.apache.archiva.redback.keys.AuthenticationKey;
38 import org.apache.archiva.redback.keys.KeyManager;
39 import org.apache.archiva.redback.rbac.Operation;
40 import org.apache.archiva.redback.rbac.Permission;
41 import org.apache.archiva.redback.rbac.RbacManagerException;
42 import org.apache.archiva.redback.rbac.Role;
43 import org.apache.archiva.redback.users.UserManager;
44 import org.apache.archiva.redback.keys.KeyManagerException;
45 import org.codehaus.plexus.redback.keys.jdo.AuthenticationKeyDatabase;
46 import org.codehaus.plexus.redback.keys.jdo.io.stax.RedbackKeyManagementJdoStaxReader;
47 import org.codehaus.plexus.redback.keys.jdo.io.stax.RedbackKeyManagementJdoStaxWriter;
48 import org.apache.archiva.redback.rbac.RBACManager;
49 import org.apache.archiva.redback.rbac.Resource;
50 import org.apache.archiva.redback.rbac.UserAssignment;
51 import org.codehaus.plexus.redback.rbac.jdo.RbacDatabase;
52 import org.codehaus.plexus.redback.rbac.jdo.io.stax.RbacJdoModelStaxReader;
53 import org.codehaus.plexus.redback.rbac.jdo.io.stax.RbacJdoModelStaxWriter;
54 import org.apache.archiva.redback.users.User;
55 import org.codehaus.plexus.redback.users.jdo.UserDatabase;
56 import org.codehaus.plexus.redback.users.jdo.io.stax.UsersManagementStaxReader;
57 import org.codehaus.plexus.redback.users.jdo.io.stax.UsersManagementStaxWriter;
58 import org.codehaus.plexus.util.IOUtil;
59 import org.springframework.stereotype.Service;
60
61 /**
62  * JDO implementation of the data management tool.
63  *
64  * @todo do we really need JDO specifics here? Could optimize by going straight to JDOFactory
65  * @todo check whether this current method logs everything unnecessarily.
66  */
67 @Service("dataManagementTool#jdo")
68 public class JdoDataManagementTool
69     implements DataManagementTool
70 {
71     private static final String USERS_XML_NAME = "users.xml";
72
73     private static final String KEYS_XML_NAME = "keys.xml";
74
75     private static final String RBAC_XML_NAME = "rbac.xml";
76
77     public void backupRBACDatabase( RBACManager manager, File backupDirectory )
78         throws RbacManagerException, IOException, XMLStreamException
79     {
80         RbacDatabase database = new RbacDatabase();
81         database.setRoles( manager.getAllRoles() );
82         database.setUserAssignments( manager.getAllUserAssignments() );
83         database.setPermissions( manager.getAllPermissions() );
84         database.setOperations( manager.getAllOperations() );
85         database.setResources( manager.getAllResources() );
86
87         RbacJdoModelStaxWriter writer = new RbacJdoModelStaxWriter();
88         Writer fileWriter = createWriter( backupDirectory, RBAC_XML_NAME, database.getModelEncoding() );
89         try
90         {
91             writer.write( fileWriter, database );
92         }
93         finally
94         {
95             IOUtil.close( fileWriter );
96         }
97     }
98
99     public void backupUserDatabase( UserManager manager, File backupDirectory )
100         throws IOException, XMLStreamException
101     {
102         UserDatabase database = new UserDatabase();
103         database.setUsers( manager.getUsers() );
104
105         UsersManagementStaxWriter writer = new UsersManagementStaxWriter();
106         Writer fileWriter = createWriter( backupDirectory, USERS_XML_NAME, database.getModelEncoding() );
107         try
108         {
109             writer.write( fileWriter, database );
110         }
111         finally
112         {
113             IOUtil.close( fileWriter );
114         }
115     }
116
117     public void backupKeyDatabase( KeyManager manager, File backupDirectory )
118         throws IOException, XMLStreamException
119     {
120         try
121         {
122             manager.removeExpiredKeys();
123         }
124         catch ( KeyManagerException e )
125         {
126             throw new IOException( "Error removing expired keys" );
127         }
128
129         AuthenticationKeyDatabase database = new AuthenticationKeyDatabase();
130         database.setKeys( manager.getAllKeys() );
131
132         RedbackKeyManagementJdoStaxWriter writer = new RedbackKeyManagementJdoStaxWriter();
133         Writer fileWriter = createWriter( backupDirectory, KEYS_XML_NAME, database.getModelEncoding() );
134         try
135         {
136             writer.write( fileWriter, database );
137         }
138         finally
139         {
140             IOUtil.close( fileWriter );
141         }
142     }
143
144     @SuppressWarnings("unchecked")
145     public void restoreRBACDatabase( RBACManager manager, File backupDirectory )
146         throws IOException, XMLStreamException, RbacManagerException
147     {
148         RbacJdoModelStaxReader reader = new RbacJdoModelStaxReader();
149
150         FileReader fileReader = new FileReader( new File( backupDirectory, RBAC_XML_NAME ) );
151
152         RbacDatabase database;
153         try
154         {
155             database = reader.read( fileReader );
156         }
157         finally
158         {
159             IOUtil.close( fileReader );
160         }
161
162         Map<String, Permission> permissionMap = new HashMap<String, Permission>();
163         Map<String, Resource> resources = new HashMap<String, Resource>();
164         Map<String, Operation> operations = new HashMap<String, Operation>();
165         for ( Role role : (List<Role>) database.getRoles() )
166         {
167             // TODO: this could be generally useful and put into saveRole itself as long as the performance penalty isn't too harsh.
168             //   Currently it always saves everything where it could pull pack the existing permissions, etc if they exist
169             List<Permission> permissions = new ArrayList<Permission>();
170             for ( Permission permission : role.getPermissions() )
171             {
172                 if ( permissionMap.containsKey( permission.getName() ) )
173                 {
174                     permission = permissionMap.get( permission.getName() );
175                 }
176                 else if ( manager.permissionExists( permission ) )
177                 {
178                     permission = manager.getPermission( permission.getName() );
179                     permissionMap.put( permission.getName(), permission );
180                 }
181                 else
182                 {
183                     Operation operation = permission.getOperation();
184                     if ( operations.containsKey( operation.getName() ) )
185                     {
186                         operation = operations.get( operation.getName() );
187                     }
188                     else if ( manager.operationExists( operation ) )
189                     {
190                         operation = manager.getOperation( operation.getName() );
191                         operations.put( operation.getName(), operation );
192                     }
193                     else
194                     {
195                         operation = manager.saveOperation( operation );
196                         operations.put( operation.getName(), operation );
197                     }
198                     permission.setOperation( operation );
199
200                     Resource resource = permission.getResource();
201                     if ( resources.containsKey( resource.getIdentifier() ) )
202                     {
203                         resource = resources.get( resource.getIdentifier() );
204                     }
205                     else if ( manager.resourceExists( resource ) )
206                     {
207                         resource = manager.getResource( resource.getIdentifier() );
208                         resources.put( resource.getIdentifier(), resource );
209                     }
210                     else
211                     {
212                         resource = manager.saveResource( resource );
213                         resources.put( resource.getIdentifier(), resource );
214                     }
215                     permission.setResource( resource );
216
217                     permission = manager.savePermission( permission );
218                     permissionMap.put( permission.getName(), permission );
219                 }
220                 permissions.add( permission );
221             }
222             role.setPermissions( permissions );
223
224             manager.saveRole( role );
225         }
226
227         for ( UserAssignment userAssignment : (List<UserAssignment>) database.getUserAssignments() )
228         {
229             manager.saveUserAssignment( userAssignment );
230         }
231     }
232
233     @SuppressWarnings("unchecked")
234     public void restoreUsersDatabase( UserManager manager, File backupDirectory )
235         throws IOException, XMLStreamException
236     {
237         UsersManagementStaxReader reader = new UsersManagementStaxReader();
238
239         FileReader fileReader = new FileReader( new File( backupDirectory, USERS_XML_NAME ) );
240
241         UserDatabase database;
242         try
243         {
244             database = reader.read( fileReader );
245         }
246         finally
247         {
248             IOUtil.close( fileReader );
249         }
250
251         for ( User user : (List<User>) database.getUsers() )
252         {
253             manager.addUserUnchecked( user );
254         }
255     }
256
257     @SuppressWarnings("unchecked")
258     public void restoreKeysDatabase( KeyManager manager, File backupDirectory )
259         throws IOException, XMLStreamException
260     {
261         RedbackKeyManagementJdoStaxReader reader = new RedbackKeyManagementJdoStaxReader();
262
263         FileReader fileReader = new FileReader( new File( backupDirectory, KEYS_XML_NAME ) );
264
265         AuthenticationKeyDatabase database;
266         try
267         {
268             database = reader.read( fileReader );
269         }
270         finally
271         {
272             IOUtil.close( fileReader );
273         }
274
275         for ( AuthenticationKey key : (List<AuthenticationKey>) database.getKeys() )
276         {
277             manager.addKey( key );
278         }
279     }
280
281     public void eraseRBACDatabase( RBACManager manager )
282     {
283         manager.eraseDatabase();
284     }
285
286     public void eraseUsersDatabase( UserManager manager )
287     {
288         manager.eraseDatabase();
289     }
290
291     public void eraseKeysDatabase( KeyManager manager )
292     {
293         manager.eraseDatabase();
294     }
295
296     private Writer createWriter( File directory, String file, String encoding )
297         throws FileNotFoundException
298     {
299         File f = new File( directory, file );
300         File parentFile = f.getParentFile();
301         parentFile.mkdirs();
302
303         FileOutputStream out = new FileOutputStream( f );
304         return new OutputStreamWriter( out, Charset.forName( encoding ) );
305     }
306 }