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.management.DataManagementTool;
38 import org.apache.archiva.redback.users.UserManager;
39 import org.codehaus.plexus.redback.keys.AuthenticationKey;
40 import org.codehaus.plexus.redback.keys.KeyManager;
41 import org.codehaus.plexus.redback.keys.KeyManagerException;
42 import org.codehaus.plexus.redback.keys.jdo.AuthenticationKeyDatabase;
43 import org.codehaus.plexus.redback.keys.jdo.io.stax.RedbackKeyManagementJdoStaxReader;
44 import org.codehaus.plexus.redback.keys.jdo.io.stax.RedbackKeyManagementJdoStaxWriter;
45 import org.codehaus.plexus.redback.rbac.Operation;
46 import org.codehaus.plexus.redback.rbac.Permission;
47 import org.codehaus.plexus.redback.rbac.RBACManager;
48 import org.codehaus.plexus.redback.rbac.RbacManagerException;
49 import org.codehaus.plexus.redback.rbac.Resource;
50 import org.codehaus.plexus.redback.rbac.Role;
51 import org.codehaus.plexus.redback.rbac.UserAssignment;
52 import org.codehaus.plexus.redback.rbac.jdo.RbacDatabase;
53 import org.codehaus.plexus.redback.rbac.jdo.io.stax.RbacJdoModelStaxReader;
54 import org.codehaus.plexus.redback.rbac.jdo.io.stax.RbacJdoModelStaxWriter;
55 import org.apache.archiva.redback.users.User;
56 import org.codehaus.plexus.redback.users.jdo.UserDatabase;
57 import org.codehaus.plexus.redback.users.jdo.io.stax.UsersManagementStaxReader;
58 import org.codehaus.plexus.redback.users.jdo.io.stax.UsersManagementStaxWriter;
59 import org.codehaus.plexus.util.IOUtil;
60 import org.springframework.stereotype.Service;
63 * JDO implementation of the data management tool.
65 * @todo do we really need JDO specifics here? Could optimize by going straight to JDOFactory
66 * @todo check whether this current method logs everything unnecessarily.
68 @Service("dataManagementTool#jdo")
69 public class JdoDataManagementTool
70 implements DataManagementTool
72 private static final String USERS_XML_NAME = "users.xml";
74 private static final String KEYS_XML_NAME = "keys.xml";
76 private static final String RBAC_XML_NAME = "rbac.xml";
78 public void backupRBACDatabase( RBACManager manager, File backupDirectory )
79 throws RbacManagerException, IOException, XMLStreamException
81 RbacDatabase database = new RbacDatabase();
82 database.setRoles( manager.getAllRoles() );
83 database.setUserAssignments( manager.getAllUserAssignments() );
84 database.setPermissions( manager.getAllPermissions() );
85 database.setOperations( manager.getAllOperations() );
86 database.setResources( manager.getAllResources() );
88 RbacJdoModelStaxWriter writer = new RbacJdoModelStaxWriter();
89 Writer fileWriter = createWriter( backupDirectory, RBAC_XML_NAME, database.getModelEncoding() );
92 writer.write( fileWriter, database );
96 IOUtil.close( fileWriter );
100 public void backupUserDatabase( UserManager manager, File backupDirectory )
101 throws IOException, XMLStreamException
103 UserDatabase database = new UserDatabase();
104 database.setUsers( manager.getUsers() );
106 UsersManagementStaxWriter writer = new UsersManagementStaxWriter();
107 Writer fileWriter = createWriter( backupDirectory, USERS_XML_NAME, database.getModelEncoding() );
110 writer.write( fileWriter, database );
114 IOUtil.close( fileWriter );
118 public void backupKeyDatabase( KeyManager manager, File backupDirectory )
119 throws IOException, XMLStreamException
123 manager.removeExpiredKeys();
125 catch ( KeyManagerException e )
127 throw new IOException( "Error removing expired keys" );
130 AuthenticationKeyDatabase database = new AuthenticationKeyDatabase();
131 database.setKeys( manager.getAllKeys() );
133 RedbackKeyManagementJdoStaxWriter writer = new RedbackKeyManagementJdoStaxWriter();
134 Writer fileWriter = createWriter( backupDirectory, KEYS_XML_NAME, database.getModelEncoding() );
137 writer.write( fileWriter, database );
141 IOUtil.close( fileWriter );
145 @SuppressWarnings("unchecked")
146 public void restoreRBACDatabase( RBACManager manager, File backupDirectory )
147 throws IOException, XMLStreamException, RbacManagerException
149 RbacJdoModelStaxReader reader = new RbacJdoModelStaxReader();
151 FileReader fileReader = new FileReader( new File( backupDirectory, RBAC_XML_NAME ) );
153 RbacDatabase database;
156 database = reader.read( fileReader );
160 IOUtil.close( fileReader );
163 Map<String, Permission> permissionMap = new HashMap<String, Permission>();
164 Map<String, Resource> resources = new HashMap<String, Resource>();
165 Map<String, Operation> operations = new HashMap<String, Operation>();
166 for ( Role role : (List<Role>) database.getRoles() )
168 // TODO: this could be generally useful and put into saveRole itself as long as the performance penalty isn't too harsh.
169 // Currently it always saves everything where it could pull pack the existing permissions, etc if they exist
170 List<Permission> permissions = new ArrayList<Permission>();
171 for ( Permission permission : role.getPermissions() )
173 if ( permissionMap.containsKey( permission.getName() ) )
175 permission = permissionMap.get( permission.getName() );
177 else if ( manager.permissionExists( permission ) )
179 permission = manager.getPermission( permission.getName() );
180 permissionMap.put( permission.getName(), permission );
184 Operation operation = permission.getOperation();
185 if ( operations.containsKey( operation.getName() ) )
187 operation = operations.get( operation.getName() );
189 else if ( manager.operationExists( operation ) )
191 operation = manager.getOperation( operation.getName() );
192 operations.put( operation.getName(), operation );
196 operation = manager.saveOperation( operation );
197 operations.put( operation.getName(), operation );
199 permission.setOperation( operation );
201 Resource resource = permission.getResource();
202 if ( resources.containsKey( resource.getIdentifier() ) )
204 resource = resources.get( resource.getIdentifier() );
206 else if ( manager.resourceExists( resource ) )
208 resource = manager.getResource( resource.getIdentifier() );
209 resources.put( resource.getIdentifier(), resource );
213 resource = manager.saveResource( resource );
214 resources.put( resource.getIdentifier(), resource );
216 permission.setResource( resource );
218 permission = manager.savePermission( permission );
219 permissionMap.put( permission.getName(), permission );
221 permissions.add( permission );
223 role.setPermissions( permissions );
225 manager.saveRole( role );
228 for ( UserAssignment userAssignment : (List<UserAssignment>) database.getUserAssignments() )
230 manager.saveUserAssignment( userAssignment );
234 @SuppressWarnings("unchecked")
235 public void restoreUsersDatabase( UserManager manager, File backupDirectory )
236 throws IOException, XMLStreamException
238 UsersManagementStaxReader reader = new UsersManagementStaxReader();
240 FileReader fileReader = new FileReader( new File( backupDirectory, USERS_XML_NAME ) );
242 UserDatabase database;
245 database = reader.read( fileReader );
249 IOUtil.close( fileReader );
252 for ( User user : (List<User>) database.getUsers() )
254 manager.addUserUnchecked( user );
258 @SuppressWarnings("unchecked")
259 public void restoreKeysDatabase( KeyManager manager, File backupDirectory )
260 throws IOException, XMLStreamException
262 RedbackKeyManagementJdoStaxReader reader = new RedbackKeyManagementJdoStaxReader();
264 FileReader fileReader = new FileReader( new File( backupDirectory, KEYS_XML_NAME ) );
266 AuthenticationKeyDatabase database;
269 database = reader.read( fileReader );
273 IOUtil.close( fileReader );
276 for ( AuthenticationKey key : (List<AuthenticationKey>) database.getKeys() )
278 manager.addKey( key );
282 public void eraseRBACDatabase( RBACManager manager )
284 manager.eraseDatabase();
287 public void eraseUsersDatabase( UserManager manager )
289 manager.eraseDatabase();
292 public void eraseKeysDatabase( KeyManager manager )
294 manager.eraseDatabase();
297 private Writer createWriter( File directory, String file, String encoding )
298 throws FileNotFoundException
300 File f = new File( directory, file );
301 File parentFile = f.getParentFile();
304 FileOutputStream out = new FileOutputStream( f );
305 return new OutputStreamWriter( out, Charset.forName( encoding ) );