01: // $Id: JACCConfiguration.java 7592 2005-07-21 04:56:17Z oneovthafew $
02: package org.hibernate.secure;
03:
04: import java.util.StringTokenizer;
05:
06: import javax.security.jacc.EJBMethodPermission;
07: import javax.security.jacc.PolicyConfiguration;
08: import javax.security.jacc.PolicyConfigurationFactory;
09: import javax.security.jacc.PolicyContextException;
10:
11: import org.apache.commons.logging.Log;
12: import org.apache.commons.logging.LogFactory;
13: import org.hibernate.HibernateException;
14:
15: /**
16: * Adds Hibernate permissions to roles via JACC
17: *
18: * @author Gavin King
19: */
20: public class JACCConfiguration {
21:
22: private static final Log log = LogFactory
23: .getLog(JACCConfiguration.class);
24:
25: private final PolicyConfiguration policyConfiguration;
26:
27: public JACCConfiguration(String contextId)
28: throws HibernateException {
29: try {
30: policyConfiguration = PolicyConfigurationFactory
31: .getPolicyConfigurationFactory()
32: .getPolicyConfiguration(contextId, false);
33: } catch (ClassNotFoundException cnfe) {
34: throw new HibernateException(
35: "JACC provider class not found", cnfe);
36: } catch (PolicyContextException pce) {
37: throw new HibernateException(
38: "policy context exception occurred", pce);
39: }
40: }
41:
42: public void addPermission(String role, String entityName,
43: String action) {
44:
45: if (action.equals("*")) {
46: action = "insert,read,update,delete";
47: }
48:
49: StringTokenizer tok = new StringTokenizer(action, ",");
50:
51: while (tok.hasMoreTokens()) {
52: String methodName = tok.nextToken().trim();
53: EJBMethodPermission permission = new EJBMethodPermission(
54: entityName, methodName, null, // interfaces
55: null // arguments
56: );
57:
58: if (log.isDebugEnabled()) {
59: log.debug("adding permission to role " + role + ": "
60: + permission);
61: }
62: try {
63: policyConfiguration.addToRole(role, permission);
64: } catch (PolicyContextException pce) {
65: throw new HibernateException(
66: "policy context exception occurred", pce);
67: }
68: }
69: }
70:
71: }
|