KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > AppPolicy


1 /*
2  * JBoss, the OpenSource EJB server
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.security;
8
9 import java.security.AccessController JavaDoc;
10 import java.security.AllPermission JavaDoc;
11 import java.security.CodeSource JavaDoc;
12 import java.security.KeyStore JavaDoc;
13 import java.security.PermissionCollection JavaDoc;
14 import java.security.Permissions JavaDoc;
15 import javax.security.auth.Subject JavaDoc;
16 import javax.security.auth.login.AppConfigurationEntry JavaDoc;
17
18 /** A combination of keystore, authentication and authorization entries.
19
20 @author Scott.Stark@jboss.org
21 @version $Revision: 1.4 $
22 */

23 public class AppPolicy
24 {
25     /** A PermissionCollection that allows no permissions */
26     public static final PermissionCollection JavaDoc NO_PERMISSIONS = new Permissions JavaDoc();
27     /** A PermissionCollection that allows all permissions */
28     private static PermissionCollection JavaDoc ALL_PERMISSIONS;
29
30     /**
31      * @label defaultAppPolicy
32      */

33     private static AppPolicy defaultAppPolicy;
34
35     // Setup the class statics
36
static
37     {
38         // A PermissionCollection that allows all permissions
39
AllPermission JavaDoc all = new AllPermission JavaDoc();
40         ALL_PERMISSIONS = all.newPermissionCollection();
41         ALL_PERMISSIONS.add(all);
42         // A default policy with no authentication and NO_PERMISSIONS
43
defaultAppPolicy = new AppPolicy("other");
44     }
45
46     private String JavaDoc appName;
47     private KeyStore JavaDoc keyStore;
48
49     /**
50      * @label permissions
51      */

52     private AuthorizationInfo permissionInfo;
53
54     /**
55      * @label login
56      */

57     private AuthenticationInfo loginInfo;
58
59     public KeyStore JavaDoc getKeyStore()
60     {
61         return keyStore;
62     }
63     public void setKeyStore(KeyStore JavaDoc keyStore)
64     {
65         this.keyStore = keyStore;
66     }
67
68     public static void setDefaultAppPolicy(AppPolicy policy)
69     {
70         if( policy == null )
71             throw new IllegalArgumentException JavaDoc("The policy argument cannot be null");
72         defaultAppPolicy = policy;
73     }
74     public static AppPolicy getDefaultAppPolicy()
75     {
76         return defaultAppPolicy;
77     }
78
79     public AppPolicy(String JavaDoc appName)
80     {
81         this.appName = appName;
82     }
83
84     public AuthenticationInfo getLoginInfo()
85     {
86         AccessController.checkPermission(AuthenticationInfo.GET_CONFIG_ENTRY_PERM);
87         return loginInfo;
88     }
89     public void setLoginInfo(AuthenticationInfo loginInfo)
90     {
91         AccessController.checkPermission(AuthenticationInfo.SET_CONFIG_ENTRY_PERM);
92         this.loginInfo = loginInfo;
93     }
94     public AuthorizationInfo getPermissionInfo()
95     {
96         return permissionInfo;
97     }
98     public void setPermissionInfo(AuthorizationInfo permissionInfo)
99     {
100         this.permissionInfo = permissionInfo;
101     }
102
103     public AppConfigurationEntry JavaDoc[] getAppConfigurationEntry()
104     {
105         AppConfigurationEntry JavaDoc[] appConfig = null;
106         if( loginInfo != null )
107             appConfig = loginInfo.getAppConfigurationEntry();
108         if( appConfig == null && this != defaultAppPolicy )
109             appConfig = defaultAppPolicy.getAppConfigurationEntry();
110         AppConfigurationEntry JavaDoc[] copy = null;
111         if( appConfig != null )
112         {
113             copy = new AppConfigurationEntry JavaDoc[appConfig.length];
114             for(int c = 0; c < copy.length; c ++)
115             {
116                 AppConfigurationEntry JavaDoc e0 = appConfig[c];
117                 AppConfigurationEntry JavaDoc e1 = new AppConfigurationEntry JavaDoc(
118                     e0.getLoginModuleName(),
119                     e0.getControlFlag(),
120                     e0.getOptions()
121                     );
122                 copy[c] = e1;
123             }
124         }
125         return copy;
126     }
127     public PermissionCollection JavaDoc getPermissions(Subject JavaDoc subject, CodeSource JavaDoc codesource)
128     {
129         PermissionCollection JavaDoc perms = NO_PERMISSIONS;
130         AuthorizationInfo info = getPermissionInfo();
131         if( info == null )
132             info = defaultAppPolicy.getPermissionInfo();
133         if( info != null )
134         {
135             perms = info.getPermissions(subject, codesource);
136         }
137
138         return perms;
139     }
140
141     public String JavaDoc toString()
142     {
143         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(appName);
144         buffer.append('\n');
145         buffer.append("AuthenticationInfo:\n");
146         if( loginInfo != null )
147             buffer.append(loginInfo);
148         buffer.append("AuthorizationInfo:\n");
149         if( permissionInfo != null )
150             buffer.append(permissionInfo);
151         return buffer.toString();
152     }
153    
154 }
155
Popular Tags