KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > secure > JACCConfiguration


1 // $Id: JACCConfiguration.java,v 1.6 2005/07/21 04:56:17 oneovthafew Exp $
2
package org.hibernate.secure;
3
4 import java.util.StringTokenizer JavaDoc;
5
6 import javax.security.jacc.EJBMethodPermission JavaDoc;
7 import javax.security.jacc.PolicyConfiguration JavaDoc;
8 import javax.security.jacc.PolicyConfigurationFactory JavaDoc;
9 import javax.security.jacc.PolicyContextException JavaDoc;
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.getLog( JACCConfiguration.class );
23
24     private final PolicyConfiguration JavaDoc policyConfiguration;
25
26     public JACCConfiguration(String JavaDoc contextId) throws HibernateException {
27         try {
28             policyConfiguration = PolicyConfigurationFactory
29                     .getPolicyConfigurationFactory()
30                     .getPolicyConfiguration( contextId, false );
31         }
32         catch (ClassNotFoundException JavaDoc cnfe) {
33             throw new HibernateException( "JACC provider class not found", cnfe );
34         }
35         catch (PolicyContextException JavaDoc pce) {
36             throw new HibernateException( "policy context exception occurred", pce );
37         }
38     }
39
40     public void addPermission(String JavaDoc role, String JavaDoc entityName, String JavaDoc action) {
41
42         if ( action.equals( "*" ) ) {
43             action = "insert,read,update,delete";
44         }
45
46         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc( action, "," );
47
48         while ( tok.hasMoreTokens() ) {
49             String JavaDoc methodName = tok.nextToken().trim();
50             EJBMethodPermission JavaDoc permission = new EJBMethodPermission JavaDoc(
51                     entityName,
52                     methodName,
53                     null, // interfaces
54
null // arguments
55
);
56
57             if ( log.isDebugEnabled() ) {
58                 log.debug( "adding permission to role " + role + ": " + permission );
59             }
60             try {
61                 policyConfiguration.addToRole( role, permission );
62             }
63             catch (PolicyContextException JavaDoc pce) {
64                 throw new HibernateException( "policy context exception occurred", pce );
65             }
66         }
67     }
68
69 }
70
Popular Tags