{
RbacJdoModelStaxReader reader = new RbacJdoModelStaxReader();
FileReader fileReader = new FileReader( new File( backupDirectory, RBAC_XML_NAME ) );
RbacDatabase database;
try
{
database = reader.read( fileReader );
}
finally
{
IOUtil.close( fileReader );
}
Map<String, JdoPermission> permissionMap = new HashMap<String, JdoPermission>();
Map<String, JdoResource> resources = new HashMap<String, JdoResource>();
Map<String, JdoOperation> operations = new HashMap<String, JdoOperation>();
for ( Iterator i = database.getRoles().iterator(); i.hasNext(); )
{
JdoRole role = (JdoRole) i.next();
// TODO: this could be generally useful and put into saveRole itself as long as the performance penalty isn't too harsh.
// Currently it always saves everything where it could pull pack the existing permissions, etc if they exist
List<JdoPermission> permissions = new ArrayList<JdoPermission>();
for ( Iterator j = role.getPermissions().iterator(); j.hasNext(); )
{
JdoPermission permission = (JdoPermission) j.next();
if ( permissionMap.containsKey( permission.getName() ) )
{
permission = permissionMap.get( permission.getName() );
}
else if ( objectExists( permission ) )
{
permission = (JdoPermission) PlexusJdoUtils.getObjectById( getPersistenceManager(),
JdoPermission.class,
permission.getName() );
permissionMap.put( permission.getName(), permission );
}
else
{
JdoOperation operation = (JdoOperation) permission.getOperation();
if ( operations.containsKey( operation.getName() ) )
{
operation = operations.get( operation.getName() );
}
else if ( objectExists( operation ) )
{
operation = (JdoOperation) PlexusJdoUtils.getObjectById( getPersistenceManager(),
JdoOperation.class,
operation.getName() );
operations.put( operation.getName(), operation );
}
else
{
RBACObjectAssertions.assertValid( operation );
operation = (JdoOperation) PlexusJdoUtils.saveObject( getPersistenceManager(), operation,
null );
operations.put( operation.getName(), operation );
}
permission.setOperation( operation );
JdoResource resource = (JdoResource) permission.getResource();
if ( resources.containsKey( resource.getIdentifier() ) )
{
resource = resources.get( resource.getIdentifier() );
}
else if ( objectExists( resource ) )
{
resource = (JdoResource) PlexusJdoUtils.getObjectById( getPersistenceManager(),
JdoResource.class,
resource.getIdentifier() );
resources.put( resource.getIdentifier(), resource );
}
else
{
RBACObjectAssertions.assertValid( resource );
resource = (JdoResource) PlexusJdoUtils.saveObject( getPersistenceManager(), resource, null );
resources.put( resource.getIdentifier(), resource );
}
permission.setResource( resource );
RBACObjectAssertions.assertValid( permission );
permission = (JdoPermission) PlexusJdoUtils.saveObject( getPersistenceManager(), permission, null );
permissionMap.put( permission.getName(), permission );
}
permissions.add( permission );
}
role.setPermissions( permissions );
RBACObjectAssertions.assertValid( role );
PlexusJdoUtils.saveObject( getPersistenceManager(), role, new String[]{null} );
}
for ( Iterator i = database.getUserAssignments().iterator(); i.hasNext(); )
{
JdoUserAssignment userAssignment = (JdoUserAssignment) i.next();
RBACObjectAssertions.assertValid( "Save User Assignment", userAssignment );