1 package org.codehaus.plexus.redback.management;
4 * Copyright 2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 import java.io.FileNotFoundException;
21 import java.io.FileOutputStream;
22 import java.io.FileReader;
23 import java.io.IOException;
24 import java.io.OutputStreamWriter;
25 import java.io.Writer;
26 import java.nio.charset.Charset;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
32 import javax.xml.stream.XMLStreamException;
34 import org.codehaus.plexus.redback.keys.AuthenticationKey;
35 import org.codehaus.plexus.redback.keys.KeyManager;
36 import org.codehaus.plexus.redback.keys.KeyManagerException;
37 import org.codehaus.plexus.redback.keys.jdo.AuthenticationKeyDatabase;
38 import org.codehaus.plexus.redback.keys.jdo.io.stax.RedbackKeyManagementJdoStaxReader;
39 import org.codehaus.plexus.redback.keys.jdo.io.stax.RedbackKeyManagementJdoStaxWriter;
40 import org.codehaus.plexus.redback.rbac.Operation;
41 import org.codehaus.plexus.redback.rbac.Permission;
42 import org.codehaus.plexus.redback.rbac.RBACManager;
43 import org.codehaus.plexus.redback.rbac.RbacManagerException;
44 import org.codehaus.plexus.redback.rbac.Resource;
45 import org.codehaus.plexus.redback.rbac.Role;
46 import org.codehaus.plexus.redback.rbac.UserAssignment;
47 import org.codehaus.plexus.redback.rbac.jdo.RbacDatabase;
48 import org.codehaus.plexus.redback.rbac.jdo.io.stax.RbacJdoModelStaxReader;
49 import org.codehaus.plexus.redback.rbac.jdo.io.stax.RbacJdoModelStaxWriter;
50 import org.codehaus.plexus.redback.users.User;
51 import org.codehaus.plexus.redback.users.UserManager;
52 import org.codehaus.plexus.redback.users.jdo.UserDatabase;
53 import org.codehaus.plexus.redback.users.jdo.io.stax.UsersManagementStaxReader;
54 import org.codehaus.plexus.redback.users.jdo.io.stax.UsersManagementStaxWriter;
55 import org.codehaus.plexus.util.IOUtil;
56 import org.springframework.stereotype.Service;
59 * JDO implementation of the data management tool.
61 * @todo do we really need JDO specifics here? Could optimize by going straight to JDOFactory
62 * @todo check whether this current method logs everything unnecessarily.
64 @Service("dataManagementTool#jdo")
65 public class JdoDataManagementTool
66 implements DataManagementTool
68 private static final String USERS_XML_NAME = "users.xml";
70 private static final String KEYS_XML_NAME = "keys.xml";
72 private static final String RBAC_XML_NAME = "rbac.xml";
74 public void backupRBACDatabase( RBACManager manager, File backupDirectory )
75 throws RbacManagerException, IOException, XMLStreamException
77 RbacDatabase database = new RbacDatabase();
78 database.setRoles( manager.getAllRoles() );
79 database.setUserAssignments( manager.getAllUserAssignments() );
80 database.setPermissions( manager.getAllPermissions() );
81 database.setOperations( manager.getAllOperations() );
82 database.setResources( manager.getAllResources() );
84 RbacJdoModelStaxWriter writer = new RbacJdoModelStaxWriter();
85 Writer fileWriter = createWriter( backupDirectory, RBAC_XML_NAME, database.getModelEncoding() );
88 writer.write( fileWriter, database );
92 IOUtil.close( fileWriter );
96 public void backupUserDatabase( UserManager manager, File backupDirectory )
97 throws IOException, XMLStreamException
99 UserDatabase database = new UserDatabase();
100 database.setUsers( manager.getUsers() );
102 UsersManagementStaxWriter writer = new UsersManagementStaxWriter();
103 Writer fileWriter = createWriter( backupDirectory, USERS_XML_NAME, database.getModelEncoding() );
106 writer.write( fileWriter, database );
110 IOUtil.close( fileWriter );
114 public void backupKeyDatabase( KeyManager manager, File backupDirectory )
115 throws IOException, XMLStreamException
119 manager.removeExpiredKeys();
121 catch ( KeyManagerException e )
123 throw new IOException( "Error removing expired keys" );
126 AuthenticationKeyDatabase database = new AuthenticationKeyDatabase();
127 database.setKeys( manager.getAllKeys() );
129 RedbackKeyManagementJdoStaxWriter writer = new RedbackKeyManagementJdoStaxWriter();
130 Writer fileWriter = createWriter( backupDirectory, KEYS_XML_NAME, database.getModelEncoding() );
133 writer.write( fileWriter, database );
137 IOUtil.close( fileWriter );
141 @SuppressWarnings("unchecked")
142 public void restoreRBACDatabase( RBACManager manager, File backupDirectory )
143 throws IOException, XMLStreamException, RbacManagerException
145 RbacJdoModelStaxReader reader = new RbacJdoModelStaxReader();
147 FileReader fileReader = new FileReader( new File( backupDirectory, RBAC_XML_NAME ) );
149 RbacDatabase database;
152 database = reader.read( fileReader );
156 IOUtil.close( fileReader );
159 Map<String, Permission> permissionMap = new HashMap<String, Permission>();
160 Map<String, Resource> resources = new HashMap<String, Resource>();
161 Map<String, Operation> operations = new HashMap<String, Operation>();
162 for ( Role role : (List<Role>) database.getRoles() )
164 // TODO: this could be generally useful and put into saveRole itself as long as the performance penalty isn't too harsh.
165 // Currently it always saves everything where it could pull pack the existing permissions, etc if they exist
166 List<Permission> permissions = new ArrayList<Permission>();
167 for ( Permission permission : role.getPermissions() )
169 if ( permissionMap.containsKey( permission.getName() ) )
171 permission = permissionMap.get( permission.getName() );
173 else if ( manager.permissionExists( permission ) )
175 permission = manager.getPermission( permission.getName() );
176 permissionMap.put( permission.getName(), permission );
180 Operation operation = permission.getOperation();
181 if ( operations.containsKey( operation.getName() ) )
183 operation = operations.get( operation.getName() );
185 else if ( manager.operationExists( operation ) )
187 operation = manager.getOperation( operation.getName() );
188 operations.put( operation.getName(), operation );
192 operation = manager.saveOperation( operation );
193 operations.put( operation.getName(), operation );
195 permission.setOperation( operation );
197 Resource resource = permission.getResource();
198 if ( resources.containsKey( resource.getIdentifier() ) )
200 resource = resources.get( resource.getIdentifier() );
202 else if ( manager.resourceExists( resource ) )
204 resource = manager.getResource( resource.getIdentifier() );
205 resources.put( resource.getIdentifier(), resource );
209 resource = manager.saveResource( resource );
210 resources.put( resource.getIdentifier(), resource );
212 permission.setResource( resource );
214 permission = manager.savePermission( permission );
215 permissionMap.put( permission.getName(), permission );
217 permissions.add( permission );
219 role.setPermissions( permissions );
221 manager.saveRole( role );
224 for ( UserAssignment userAssignment : (List<UserAssignment>) database.getUserAssignments() )
226 manager.saveUserAssignment( userAssignment );
230 @SuppressWarnings("unchecked")
231 public void restoreUsersDatabase( UserManager manager, File backupDirectory )
232 throws IOException, XMLStreamException
234 UsersManagementStaxReader reader = new UsersManagementStaxReader();
236 FileReader fileReader = new FileReader( new File( backupDirectory, USERS_XML_NAME ) );
238 UserDatabase database;
241 database = reader.read( fileReader );
245 IOUtil.close( fileReader );
248 for ( User user : (List<User>) database.getUsers() )
250 manager.addUserUnchecked( user );
254 @SuppressWarnings("unchecked")
255 public void restoreKeysDatabase( KeyManager manager, File backupDirectory )
256 throws IOException, XMLStreamException
258 RedbackKeyManagementJdoStaxReader reader = new RedbackKeyManagementJdoStaxReader();
260 FileReader fileReader = new FileReader( new File( backupDirectory, KEYS_XML_NAME ) );
262 AuthenticationKeyDatabase database;
265 database = reader.read( fileReader );
269 IOUtil.close( fileReader );
272 for ( AuthenticationKey key : (List<AuthenticationKey>) database.getKeys() )
274 manager.addKey( key );
278 public void eraseRBACDatabase( RBACManager manager )
280 manager.eraseDatabase();
283 public void eraseUsersDatabase( UserManager manager )
285 manager.eraseDatabase();
288 public void eraseKeysDatabase( KeyManager manager )
290 manager.eraseDatabase();
293 private Writer createWriter( File directory, String file, String encoding )
294 throws FileNotFoundException
296 File f = new File( directory, file );
297 File parentFile = f.getParentFile();
300 FileOutputStream out = new FileOutputStream( f );
301 return new OutputStreamWriter( out, Charset.forName( encoding ) );