]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
fa59a890e3c6a67cc2742594011fe95eedbb77dc
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24 package org.collectionspace.services.authorization.importer;
25
26 import java.io.FileInputStream;
27 import java.io.InputStream;
28
29 import java.util.List;
30
31 import javax.xml.bind.JAXBContext;
32 import javax.xml.bind.Unmarshaller;
33
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import org.collectionspace.services.authorization.perms.Permission;
38 import org.collectionspace.services.authorization.PermissionRole;
39 import org.collectionspace.services.authorization.perms.PermissionsList;
40 import org.collectionspace.services.authorization.PermissionsRolesList;
41 import org.collectionspace.services.common.authorization_mgt.AuthorizationCommon;
42
43 /**
44  * AuthorizationSeed seeds authorizations (permission, role) into authz provider database
45  * @author 
46  */
47 public class AuthorizationSeed {
48
49     final Logger logger = LoggerFactory.getLogger(AuthorizationSeed.class);
50
51
52     /**
53      * seedPermissions seed permissions from given files
54      * @param permFileName permisison file name
55      * @param permRoleFileName permission role file name
56      * @throws Exception
57      */
58     public void seedPermissions(String permFileName, String permRoleFileName) throws Exception {
59         PermissionsRolesList permRoleList =
60                 (PermissionsRolesList) fromFile(PermissionsRolesList.class,
61                 permRoleFileName);
62         if (logger.isDebugEnabled()) {
63             logger.debug("read permissions-roles from " + permRoleFileName);
64         }
65         PermissionsList permList =
66             (PermissionsList) fromFile(PermissionsList.class,
67             permFileName);
68             if (logger.isDebugEnabled()) {
69                 logger.debug("read permissions from " + permFileName);
70             }
71
72         seedPermissions(permList, permRoleList);
73     }
74
75     /**
76      * seedPermissions seed permissions from given permisison and permission role lists
77      * @param permList
78      * @param permRoleList
79      * @throws Exception
80      */
81     public void seedPermissions(PermissionsList permList, PermissionsRolesList permRoleList)
82             throws Exception {
83         
84         seedPermissions(permList.getPermission(), permRoleList.getPermissionRole());
85     }
86     
87     /**
88      * seedPermissions seed permissions from given permisison and permission role lists
89      * @param permList
90      * @param permRoleList
91      * @throws Exception
92      */
93     public void seedPermissions(List<Permission> permList, List<PermissionRole> permRoleList)
94             throws Exception {
95         if (logger.isInfoEnabled() == true) {
96                 logger.info("Started seeding Spring Security Tables...");
97         }
98         int factor = permRoleList.size();
99         int permsToSeed = permList.size() * factor;
100         int permsSeeded = 0;
101         
102         for (Permission p : permList) {
103                 if (permsSeeded++ % 10 == 0 && logger.isInfoEnabled()) {
104                         logger.info(String.format("Seeded %d permissions of %d...", permsSeeded * factor, permsToSeed));
105                 }
106             for (PermissionRole pr : permRoleList) {
107                 if (pr.getPermission().get(0).getPermissionId().equals(p.getCsid())) {
108                         AuthorizationCommon.addPermissionsForUri(p, pr);
109                 }
110             }
111         }
112         
113         if (logger.isInfoEnabled() == true) {
114                 logger.info("Finished seeding Spring Security Tables.");
115         }
116     }
117     
118     /**
119      * getAction is a convenience method to get corresponding action for
120      * given ActionType
121      * @param action
122      * @return
123      *
124     private CSpaceAction getAction(ActionType action) {
125         if (ActionType.CREATE.equals(action)) {
126             return CSpaceAction.CREATE;
127         } else if (ActionType.READ.equals(action)) {
128             return CSpaceAction.READ;
129         } else if (ActionType.UPDATE.equals(action)) {
130             return CSpaceAction.UPDATE;
131         } else if (ActionType.DELETE.equals(action)) {
132             return CSpaceAction.DELETE;
133         } else if (ActionType.SEARCH.equals(action)) {
134             return CSpaceAction.SEARCH;
135         } else if (ActionType.ADMIN.equals(action)) {
136             return CSpaceAction.ADMIN;
137         } else if (ActionType.START.equals(action)) {
138             return CSpaceAction.START;
139         } else if (ActionType.STOP.equals(action)) {
140             return CSpaceAction.STOP;
141         }
142         throw new IllegalArgumentException("action = " + action.toString());
143     }
144     */
145
146     static Object fromFile(Class jaxbClass, String fileName) throws Exception {
147         InputStream is = new FileInputStream(fileName);
148         try {
149             JAXBContext context = JAXBContext.newInstance(jaxbClass);
150             Unmarshaller unmarshaller = context.createUnmarshaller();
151             //note: setting schema to null will turn validator off
152             unmarshaller.setSchema(null);
153             return jaxbClass.cast(unmarshaller.unmarshal(is));
154         } finally {
155             if (is != null) {
156                 try {
157                     is.close();
158                 } catch (Exception e) {
159                 }
160             }
161         }
162     }
163 }