1 package org.apache.archiva.redback.management;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
35 import javax.xml.stream.XMLStreamException;
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;
62 * JDO implementation of the data management tool.
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.
67 @Service("dataManagementTool#jdo")
68 public class JdoDataManagementTool
69 implements DataManagementTool
71 private static final String USERS_XML_NAME = "users.xml";
73 private static final String KEYS_XML_NAME = "keys.xml";
75 private static final String RBAC_XML_NAME = "rbac.xml";
77 public void backupRBACDatabase( RBACManager manager, File backupDirectory )
78 throws RbacManagerException, IOException, XMLStreamException
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() );
87 RbacJdoModelStaxWriter writer = new RbacJdoModelStaxWriter();
88 Writer fileWriter = createWriter( backupDirectory, RBAC_XML_NAME, database.getModelEncoding() );
91 writer.write( fileWriter, database );
95 IOUtil.close( fileWriter );
99 public void backupUserDatabase( UserManager manager, File backupDirectory )
100 throws IOException, XMLStreamException
102 UserDatabase database = new UserDatabase();
103 database.setUsers( manager.getUsers() );
105 UsersManagementStaxWriter writer = new UsersManagementStaxWriter();
106 Writer fileWriter = createWriter( backupDirectory, USERS_XML_NAME, database.getModelEncoding() );
109 writer.write( fileWriter, database );
113 IOUtil.close( fileWriter );
117 public void backupKeyDatabase( KeyManager manager, File backupDirectory )
118 throws IOException, XMLStreamException
122 manager.removeExpiredKeys();
124 catch ( KeyManagerException e )
126 throw new IOException( "Error removing expired keys" );
129 AuthenticationKeyDatabase database = new AuthenticationKeyDatabase();
130 database.setKeys( manager.getAllKeys() );
132 RedbackKeyManagementJdoStaxWriter writer = new RedbackKeyManagementJdoStaxWriter();
133 Writer fileWriter = createWriter( backupDirectory, KEYS_XML_NAME, database.getModelEncoding() );
136 writer.write( fileWriter, database );
140 IOUtil.close( fileWriter );
144 @SuppressWarnings("unchecked")
145 public void restoreRBACDatabase( RBACManager manager, File backupDirectory )
146 throws IOException, XMLStreamException, RbacManagerException
148 RbacJdoModelStaxReader reader = new RbacJdoModelStaxReader();
150 FileReader fileReader = new FileReader( new File( backupDirectory, RBAC_XML_NAME ) );
152 RbacDatabase database;
155 database = reader.read( fileReader );
159 IOUtil.close( fileReader );
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() )
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() )
172 if ( permissionMap.containsKey( permission.getName() ) )
174 permission = permissionMap.get( permission.getName() );
176 else if ( manager.permissionExists( permission ) )
178 permission = manager.getPermission( permission.getName() );
179 permissionMap.put( permission.getName(), permission );
183 Operation operation = permission.getOperation();
184 if ( operations.containsKey( operation.getName() ) )
186 operation = operations.get( operation.getName() );
188 else if ( manager.operationExists( operation ) )
190 operation = manager.getOperation( operation.getName() );
191 operations.put( operation.getName(), operation );
195 operation = manager.saveOperation( operation );
196 operations.put( operation.getName(), operation );
198 permission.setOperation( operation );
200 Resource resource = permission.getResource();
201 if ( resources.containsKey( resource.getIdentifier() ) )
203 resource = resources.get( resource.getIdentifier() );
205 else if ( manager.resourceExists( resource ) )
207 resource = manager.getResource( resource.getIdentifier() );
208 resources.put( resource.getIdentifier(), resource );
212 resource = manager.saveResource( resource );
213 resources.put( resource.getIdentifier(), resource );
215 permission.setResource( resource );
217 permission = manager.savePermission( permission );
218 permissionMap.put( permission.getName(), permission );
220 permissions.add( permission );
222 role.setPermissions( permissions );
224 manager.saveRole( role );
227 for ( UserAssignment userAssignment : (List<UserAssignment>) database.getUserAssignments() )
229 manager.saveUserAssignment( userAssignment );
233 @SuppressWarnings("unchecked")
234 public void restoreUsersDatabase( UserManager manager, File backupDirectory )
235 throws IOException, XMLStreamException
237 UsersManagementStaxReader reader = new UsersManagementStaxReader();
239 FileReader fileReader = new FileReader( new File( backupDirectory, USERS_XML_NAME ) );
241 UserDatabase database;
244 database = reader.read( fileReader );
248 IOUtil.close( fileReader );
251 for ( User user : (List<User>) database.getUsers() )
253 manager.addUserUnchecked( user );
257 @SuppressWarnings("unchecked")
258 public void restoreKeysDatabase( KeyManager manager, File backupDirectory )
259 throws IOException, XMLStreamException
261 RedbackKeyManagementJdoStaxReader reader = new RedbackKeyManagementJdoStaxReader();
263 FileReader fileReader = new FileReader( new File( backupDirectory, KEYS_XML_NAME ) );
265 AuthenticationKeyDatabase database;
268 database = reader.read( fileReader );
272 IOUtil.close( fileReader );
275 for ( AuthenticationKey key : (List<AuthenticationKey>) database.getKeys() )
277 manager.addKey( key );
281 public void eraseRBACDatabase( RBACManager manager )
283 manager.eraseDatabase();
286 public void eraseUsersDatabase( UserManager manager )
288 manager.eraseDatabase();
291 public void eraseKeysDatabase( KeyManager manager )
293 manager.eraseDatabase();
296 private Writer createWriter( File directory, String file, String encoding )
297 throws FileNotFoundException
299 File f = new File( directory, file );
300 File parentFile = f.getParentFile();
303 FileOutputStream out = new FileOutputStream( f );
304 return new OutputStreamWriter( out, Charset.forName( encoding ) );