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