]> source.dussan.org Git - archiva.git/blob
c0cb68933d09a14e046497aa9fa672df9ebe3c4c
[archiva.git] /
1 package org.apache.archiva.redback.management;
2
3 /*
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import java.io.File;
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;
33 import java.util.Map;
34
35 import javax.xml.stream.XMLStreamException;
36
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;
61
62 /**
63  * JDO implementation of the data management tool.
64  *
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.
67  */
68 @Service("dataManagementTool#jdo")
69 public class JdoDataManagementTool
70     implements DataManagementTool
71 {
72     private static final String USERS_XML_NAME = "users.xml";
73
74     private static final String KEYS_XML_NAME = "keys.xml";
75
76     private static final String RBAC_XML_NAME = "rbac.xml";
77
78     public void backupRBACDatabase( RBACManager manager, File backupDirectory )
79         throws RbacManagerException, IOException, XMLStreamException
80     {
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() );
87
88         RbacJdoModelStaxWriter writer = new RbacJdoModelStaxWriter();
89         Writer fileWriter = createWriter( backupDirectory, RBAC_XML_NAME, database.getModelEncoding() );
90         try
91         {
92             writer.write( fileWriter, database );
93         }
94         finally
95         {
96             IOUtil.close( fileWriter );
97         }
98     }
99
100     public void backupUserDatabase( UserManager manager, File backupDirectory )
101         throws IOException, XMLStreamException
102     {
103         UserDatabase database = new UserDatabase();
104         database.setUsers( manager.getUsers() );
105
106         UsersManagementStaxWriter writer = new UsersManagementStaxWriter();
107         Writer fileWriter = createWriter( backupDirectory, USERS_XML_NAME, database.getModelEncoding() );
108         try
109         {
110             writer.write( fileWriter, database );
111         }
112         finally
113         {
114             IOUtil.close( fileWriter );
115         }
116     }
117
118     public void backupKeyDatabase( KeyManager manager, File backupDirectory )
119         throws IOException, XMLStreamException
120     {
121         try
122         {
123             manager.removeExpiredKeys();
124         }
125         catch ( KeyManagerException e )
126         {
127             throw new IOException( "Error removing expired keys" );
128         }
129
130         AuthenticationKeyDatabase database = new AuthenticationKeyDatabase();
131         database.setKeys( manager.getAllKeys() );
132
133         RedbackKeyManagementJdoStaxWriter writer = new RedbackKeyManagementJdoStaxWriter();
134         Writer fileWriter = createWriter( backupDirectory, KEYS_XML_NAME, database.getModelEncoding() );
135         try
136         {
137             writer.write( fileWriter, database );
138         }
139         finally
140         {
141             IOUtil.close( fileWriter );
142         }
143     }
144
145     @SuppressWarnings("unchecked")
146     public void restoreRBACDatabase( RBACManager manager, File backupDirectory )
147         throws IOException, XMLStreamException, RbacManagerException
148     {
149         RbacJdoModelStaxReader reader = new RbacJdoModelStaxReader();
150
151         FileReader fileReader = new FileReader( new File( backupDirectory, RBAC_XML_NAME ) );
152
153         RbacDatabase database;
154         try
155         {
156             database = reader.read( fileReader );
157         }
158         finally
159         {
160             IOUtil.close( fileReader );
161         }
162
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() )
167         {
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() )
172             {
173                 if ( permissionMap.containsKey( permission.getName() ) )
174                 {
175                     permission = permissionMap.get( permission.getName() );
176                 }
177                 else if ( manager.permissionExists( permission ) )
178                 {
179                     permission = manager.getPermission( permission.getName() );
180                     permissionMap.put( permission.getName(), permission );
181                 }
182                 else
183                 {
184                     Operation operation = permission.getOperation();
185                     if ( operations.containsKey( operation.getName() ) )
186                     {
187                         operation = operations.get( operation.getName() );
188                     }
189                     else if ( manager.operationExists( operation ) )
190                     {
191                         operation = manager.getOperation( operation.getName() );
192                         operations.put( operation.getName(), operation );
193                     }
194                     else
195                     {
196                         operation = manager.saveOperation( operation );
197                         operations.put( operation.getName(), operation );
198                     }
199                     permission.setOperation( operation );
200
201                     Resource resource = permission.getResource();
202                     if ( resources.containsKey( resource.getIdentifier() ) )
203                     {
204                         resource = resources.get( resource.getIdentifier() );
205                     }
206                     else if ( manager.resourceExists( resource ) )
207                     {
208                         resource = manager.getResource( resource.getIdentifier() );
209                         resources.put( resource.getIdentifier(), resource );
210                     }
211                     else
212                     {
213                         resource = manager.saveResource( resource );
214                         resources.put( resource.getIdentifier(), resource );
215                     }
216                     permission.setResource( resource );
217
218                     permission = manager.savePermission( permission );
219                     permissionMap.put( permission.getName(), permission );
220                 }
221                 permissions.add( permission );
222             }
223             role.setPermissions( permissions );
224
225             manager.saveRole( role );
226         }
227
228         for ( UserAssignment userAssignment : (List<UserAssignment>) database.getUserAssignments() )
229         {
230             manager.saveUserAssignment( userAssignment );
231         }
232     }
233
234     @SuppressWarnings("unchecked")
235     public void restoreUsersDatabase( UserManager manager, File backupDirectory )
236         throws IOException, XMLStreamException
237     {
238         UsersManagementStaxReader reader = new UsersManagementStaxReader();
239
240         FileReader fileReader = new FileReader( new File( backupDirectory, USERS_XML_NAME ) );
241
242         UserDatabase database;
243         try
244         {
245             database = reader.read( fileReader );
246         }
247         finally
248         {
249             IOUtil.close( fileReader );
250         }
251
252         for ( User user : (List<User>) database.getUsers() )
253         {
254             manager.addUserUnchecked( user );
255         }
256     }
257
258     @SuppressWarnings("unchecked")
259     public void restoreKeysDatabase( KeyManager manager, File backupDirectory )
260         throws IOException, XMLStreamException
261     {
262         RedbackKeyManagementJdoStaxReader reader = new RedbackKeyManagementJdoStaxReader();
263
264         FileReader fileReader = new FileReader( new File( backupDirectory, KEYS_XML_NAME ) );
265
266         AuthenticationKeyDatabase database;
267         try
268         {
269             database = reader.read( fileReader );
270         }
271         finally
272         {
273             IOUtil.close( fileReader );
274         }
275
276         for ( AuthenticationKey key : (List<AuthenticationKey>) database.getKeys() )
277         {
278             manager.addKey( key );
279         }
280     }
281
282     public void eraseRBACDatabase( RBACManager manager )
283     {
284         manager.eraseDatabase();
285     }
286
287     public void eraseUsersDatabase( UserManager manager )
288     {
289         manager.eraseDatabase();
290     }
291
292     public void eraseKeysDatabase( KeyManager manager )
293     {
294         manager.eraseDatabase();
295     }
296
297     private Writer createWriter( File directory, String file, String encoding )
298         throws FileNotFoundException
299     {
300         File f = new File( directory, file );
301         File parentFile = f.getParentFile();
302         parentFile.mkdirs();
303
304         FileOutputStream out = new FileOutputStream( f );
305         return new OutputStreamWriter( out, Charset.forName( encoding ) );
306     }
307 }