]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
7ac92c819254fe45ca8d38b3e773f9520df88347
[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 import org.collectionspace.services.common.storage.jpa.JPATransactionContext;
43
44 /**
45  * AuthorizationSeed seeds authorizations (permission, role) into authz provider database
46  * @author 
47  */
48 public class AuthorizationSeed {
49
50     final Logger logger = LoggerFactory.getLogger(AuthorizationSeed.class);
51
52
53     /**
54      * seedPermissions seed permissions from given files
55      * @param permFileName permisison file name
56      * @param permRoleFileName permission role file name
57      * @throws Exception
58      */
59     public void seedPermissions(JPATransactionContext jpaTransactionContext, String permFileName, String permRoleFileName) throws Exception {
60         PermissionsRolesList permRoleList =
61                 (PermissionsRolesList) fromFile(PermissionsRolesList.class,
62                 permRoleFileName);
63         if (logger.isDebugEnabled()) {
64             logger.debug("read permissions-roles from " + permRoleFileName);
65         }
66         PermissionsList permList =
67             (PermissionsList) fromFile(PermissionsList.class,
68             permFileName);
69             if (logger.isDebugEnabled()) {
70                 logger.debug("read permissions from " + permFileName);
71             }
72
73         seedPermissions(jpaTransactionContext, permList, permRoleList);
74     }
75
76     /**
77      * seedPermissions seed permissions from given permisison and permission role lists
78      * @param permList
79      * @param permRoleList
80      * @throws Exception
81      */
82     public void seedPermissions(JPATransactionContext jpaTransactionContext, PermissionsList permList, PermissionsRolesList permRoleList)
83             throws Exception {
84         
85         seedPermissions(jpaTransactionContext, permList.getPermission(), permRoleList.getPermissionRole());
86     }
87     
88     /**
89      * seedPermissions seed permissions from given permisison and permission role lists
90      * @param permList
91      * @param permRoleList
92      * @throws Exception
93      */
94     public void seedPermissions(JPATransactionContext jpaTransactionContext, List<Permission> permList, List<PermissionRole> permRoleList)
95             throws Exception {
96         if (logger.isInfoEnabled() == true) {
97                 logger.info("Started seeding Spring Security Tables...");
98         }
99         int factor = permRoleList.size();
100         int permsToSeed = permList.size() * factor;
101         int permsSeeded = 0;
102         
103         for (Permission p : permList) {
104                 if (permsSeeded++ % 10 == 0 && logger.isInfoEnabled()) {
105                         logger.info(String.format("Seeded %d permissions of %d...", permsSeeded * factor, permsToSeed));
106                 }
107             for (PermissionRole pr : permRoleList) {
108                 if (pr.getPermission().get(0).getPermissionId().equals(p.getCsid())) {
109                         AuthorizationCommon.addPermissionsForUri(jpaTransactionContext, p, pr);
110                 }
111             }
112         }
113         
114         if (logger.isInfoEnabled() == true) {
115                 logger.info("Finished seeding Spring Security Tables.");
116         }
117     }
118     
119     /**
120      * getAction is a convenience method to get corresponding action for
121      * given ActionType
122      * @param action
123      * @return
124      *
125     private CSpaceAction getAction(ActionType action) {
126         if (ActionType.CREATE.equals(action)) {
127             return CSpaceAction.CREATE;
128         } else if (ActionType.READ.equals(action)) {
129             return CSpaceAction.READ;
130         } else if (ActionType.UPDATE.equals(action)) {
131             return CSpaceAction.UPDATE;
132         } else if (ActionType.DELETE.equals(action)) {
133             return CSpaceAction.DELETE;
134         } else if (ActionType.SEARCH.equals(action)) {
135             return CSpaceAction.SEARCH;
136         } else if (ActionType.ADMIN.equals(action)) {
137             return CSpaceAction.ADMIN;
138         } else if (ActionType.START.equals(action)) {
139             return CSpaceAction.START;
140         } else if (ActionType.STOP.equals(action)) {
141             return CSpaceAction.STOP;
142         }
143         throw new IllegalArgumentException("action = " + action.toString());
144     }
145     */
146
147     static Object fromFile(Class jaxbClass, String fileName) throws Exception {
148         InputStream is = new FileInputStream(fileName);
149         try {
150             JAXBContext context = JAXBContext.newInstance(jaxbClass);
151             Unmarshaller unmarshaller = context.createUnmarshaller();
152             //note: setting schema to null will turn validator off
153             unmarshaller.setSchema(null);
154             return jaxbClass.cast(unmarshaller.unmarshal(is));
155         } finally {
156             if (is != null) {
157                 try {
158                     is.close();
159                 } catch (Exception e) {
160                 }
161             }
162         }
163     }
164 }