Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.apache.archiva.redback.management;
  
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
  *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 
 import java.io.File;
 import java.io.Writer;
 import java.util.List;
 import java.util.Map;
 
 
JDO implementation of the data management tool.

Todo:
do we really need JDO specifics here? Could optimize by going straight to JDOFactory
Todo:
check whether this current method logs everything unnecessarily.
 
 @Service("dataManagementTool#jdo")
 public class JdoDataManagementTool
     implements DataManagementTool
 {
     private static final String USERS_XML_NAME = "users.xml";
 
     private static final String KEYS_XML_NAME = "keys.xml";
 
     private static final String RBAC_XML_NAME = "rbac.xml";
 
     public void backupRBACDatabaseRBACManager managerFile backupDirectory )
     {
         RbacDatabase database = new RbacDatabase();
         database.setRolesmanager.getAllRoles() );
         database.setUserAssignmentsmanager.getAllUserAssignments() );
         database.setPermissionsmanager.getAllPermissions() );
         database.setOperationsmanager.getAllOperations() );
         database.setResourcesmanager.getAllResources() );
 
         RbacJdoModelStaxWriter writer = new RbacJdoModelStaxWriter();
         Writer fileWriter = createWriterbackupDirectorydatabase.getModelEncoding() );
         try
         {
             writer.writefileWriterdatabase );
         }
         finally
         {
             IOUtil.closefileWriter );
         }
     }
 
     public void backupUserDatabaseUserManager managerFile backupDirectory )
        throws IOExceptionXMLStreamException
    {
        UserDatabase database = new UserDatabase();
        database.setUsersmanager.getUsers() );
        UsersManagementStaxWriter writer = new UsersManagementStaxWriter();
        Writer fileWriter = createWriterbackupDirectorydatabase.getModelEncoding() );
        try
        {
            writer.writefileWriterdatabase );
        }
        finally
        {
            IOUtil.closefileWriter );
        }
    }
    public void backupKeyDatabaseKeyManager managerFile backupDirectory )
        throws IOExceptionXMLStreamException
    {
        try
        {
            manager.removeExpiredKeys();
        }
        catch ( KeyManagerException e )
        {
            throw new IOException"Error removing expired keys" );
        }
        AuthenticationKeyDatabase database = new AuthenticationKeyDatabase();
        database.setKeysmanager.getAllKeys() );
        Writer fileWriter = createWriterbackupDirectorydatabase.getModelEncoding() );
        try
        {
            writer.writefileWriterdatabase );
        }
        finally
        {
            IOUtil.closefileWriter );
        }
    }
    @SuppressWarnings("unchecked")
    public void restoreRBACDatabaseRBACManager managerFile backupDirectory )
    {
        RbacJdoModelStaxReader reader = new RbacJdoModelStaxReader();
        FileReader fileReader = new FileReadernew FilebackupDirectory ) );
        RbacDatabase database;
        try
        {
            database = reader.readfileReader );
        }
        finally
        {
            IOUtil.closefileReader );
        }
        Map<StringPermissionpermissionMap = new HashMap<StringPermission>();
        Map<StringResourceresources = new HashMap<StringResource>();
        Map<StringOperationoperations = new HashMap<StringOperation>();
        for ( Role role : (List<Role>) database.getRoles() )
        {
            // 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<Permissionpermissions = new ArrayList<Permission>();
            for ( Permission permission : role.getPermissions() )
            {
                if ( permissionMap.containsKeypermission.getName() ) )
                {
                    permission = permissionMap.getpermission.getName() );
                }
                else if ( manager.permissionExistspermission ) )
                {
                    permission = manager.getPermissionpermission.getName() );
                    permissionMap.putpermission.getName(), permission );
                }
                else
                {
                    Operation operation = permission.getOperation();
                    if ( operations.containsKeyoperation.getName() ) )
                    {
                        operation = operations.getoperation.getName() );
                    }
                    else if ( manager.operationExistsoperation ) )
                    {
                        operation = manager.getOperationoperation.getName() );
                        operations.putoperation.getName(), operation );
                    }
                    else
                    {
                        operation = manager.saveOperationoperation );
                        operations.putoperation.getName(), operation );
                    }
                    permission.setOperationoperation );
                    Resource resource = permission.getResource();
                    if ( resources.containsKeyresource.getIdentifier() ) )
                    {
                        resource = resources.getresource.getIdentifier() );
                    }
                    else if ( manager.resourceExistsresource ) )
                    {
                        resource = manager.getResourceresource.getIdentifier() );
                        resources.putresource.getIdentifier(), resource );
                    }
                    else
                    {
                        resource = manager.saveResourceresource );
                        resources.putresource.getIdentifier(), resource );
                    }
                    permission.setResourceresource );
                    permission = manager.savePermissionpermission );
                    permissionMap.putpermission.getName(), permission );
                }
                permissions.addpermission );
            }
            role.setPermissionspermissions );
            manager.saveRolerole );
        }
        for ( UserAssignment userAssignment : (List<UserAssignment>) database.getUserAssignments() )
        {
            manager.saveUserAssignmentuserAssignment );
        }
    }
    @SuppressWarnings("unchecked")
    public void restoreUsersDatabaseUserManager managerFile backupDirectory )
        throws IOExceptionXMLStreamException
    {
        UsersManagementStaxReader reader = new UsersManagementStaxReader();
        FileReader fileReader = new FileReadernew FilebackupDirectory ) );
        UserDatabase database;
        try
        {
            database = reader.readfileReader );
        }
        finally
        {
            IOUtil.closefileReader );
        }
        for ( User user : (List<User>) database.getUsers() )
        {
            manager.addUserUncheckeduser );
        }
    }
    @SuppressWarnings("unchecked")
    public void restoreKeysDatabaseKeyManager managerFile backupDirectory )
        throws IOExceptionXMLStreamException
    {
        FileReader fileReader = new FileReadernew FilebackupDirectory ) );
        AuthenticationKeyDatabase database;
        try
        {
            database = reader.readfileReader );
        }
        finally
        {
            IOUtil.closefileReader );
        }
        for ( AuthenticationKey key : (List<AuthenticationKey>) database.getKeys() )
        {
            manager.addKeykey );
        }
    }
    public void eraseRBACDatabaseRBACManager manager )
    {
        manager.eraseDatabase();
    }
    public void eraseUsersDatabaseUserManager manager )
    {
        manager.eraseDatabase();
    }
    public void eraseKeysDatabaseKeyManager manager )
    {
        manager.eraseDatabase();
    }
    private Writer createWriterFile directoryString fileString encoding )
        throws FileNotFoundException
    {
        File f = new Filedirectoryfile );
        File parentFile = f.getParentFile();
        parentFile.mkdirs();
        FileOutputStream out = new FileOutputStreamf );
        return new OutputStreamWriterout, Charset.forNameencoding ) );
    }
New to GrepCode? Check out our FAQ X