KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > security > jacc > GeronimoPolicy


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.security.jacc;
19
20 import java.security.CodeSource JavaDoc;
21 import java.security.Permission JavaDoc;
22 import java.security.PermissionCollection JavaDoc;
23 import java.security.Policy JavaDoc;
24 import java.security.ProtectionDomain JavaDoc;
25 import javax.security.jacc.PolicyContext JavaDoc;
26 import javax.security.jacc.PolicyContextException JavaDoc;
27
28
29 /**
30  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
31  */

32 public class GeronimoPolicy extends Policy JavaDoc {
33     private final Policy JavaDoc root;
34     private GeronimoPolicyConfigurationFactory factory;
35     private boolean loaded;
36
37     public GeronimoPolicy() {
38         String JavaDoc provider = System.getProperty("org.apache.geronimo.jacc.policy.provider");
39
40         if (provider == null) {
41             root = Policy.getPolicy();
42         } else {
43             try {
44                 Object JavaDoc obj = Class.forName(provider).newInstance();
45                 if (obj instanceof Policy JavaDoc) {
46                     root = (Policy JavaDoc) obj;
47                 } else {
48                     throw new RuntimeException JavaDoc(provider + "is not a type of java.security.Policy");
49                 }
50             } catch (InstantiationException JavaDoc e) {
51                 throw new RuntimeException JavaDoc("Unable to create an instance of " + provider, e);
52             } catch (IllegalAccessException JavaDoc e) {
53                 throw new RuntimeException JavaDoc("Unable to create an instance of " + provider, e);
54             } catch (ClassNotFoundException JavaDoc e) {
55                 throw new RuntimeException JavaDoc("Unable to create an instance of " + provider, e);
56             }
57         }
58         root.refresh();
59     }
60
61     public PermissionCollection JavaDoc getPermissions(CodeSource JavaDoc codesource) {
62
63         if (root != null) return root.getPermissions(codesource);
64
65         return null;
66     }
67
68     public void refresh() {
69     }
70
71     public boolean implies(ProtectionDomain JavaDoc domain, Permission JavaDoc permission) {
72
73         if (!loaded) {
74             factory = GeronimoPolicyConfigurationFactory.getSingleton();
75             loaded = true;
76         }
77
78         if (factory != null) {
79             String JavaDoc contextID = PolicyContext.getContextID();
80             if (contextID != null) {
81                 try {
82                     GeronimoPolicyConfiguration configuration = factory.getGeronimoPolicyConfiguration(contextID);
83
84                     if (configuration.inService()) {
85                         if (configuration.implies(domain, permission)) return true;
86                     } else {
87                         return false;
88                     }
89                 } catch (PolicyContextException JavaDoc e) {
90                 }
91             }
92         }
93         if (root != null) return root.implies(domain, permission);
94
95         return false;
96     }
97 }
98
Popular Tags