KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > security > jacc > PolicyProvider


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: PolicyProvider.java 1121 2006-09-27 08:51:06Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.security.jacc;
27
28 import org.objectweb.easybeans.log.JLog;
29 import org.objectweb.easybeans.log.JLogFactory;
30 import org.objectweb.easybeans.security.jacc.provider.JPolicy;
31 import org.objectweb.easybeans.security.jacc.provider.JPolicyConfigurationFactory;
32
33
34 /**
35  * Helper class for initializing the JACC provider.
36  * @author Florent Benoit
37  */

38 public final class PolicyProvider {
39
40     /**
41      * JACC Policy Provider property.
42      */

43     private static final String JavaDoc JACC_POLICY_PROVIDER = "javax.security.jacc.policy.provider";
44
45     /**
46      * JACC Policy Configuration Factory Provider property.
47      */

48     private static final String JavaDoc JACC_POLICY_CONFIG_FACTORY_PROVIDER = "javax.security.jacc.PolicyConfigurationFactory.provider";
49
50
51     /**
52      * Only internal constructor, as it is an utility class.
53      */

54     private PolicyProvider() {
55
56     }
57
58     /**
59      * Logger.
60      */

61     private static JLog logger = JLogFactory.getLog(PolicyProvider.class);
62
63     /**
64      * Init the JACC configuration.
65      * Defines in JACC chapter 2
66      * @throws SecurityException if JACC policy provider can not be set
67      */

68     public static void init() throws SecurityException JavaDoc {
69
70         // Check if we have to use an existing policy provider
71
// Section 2.7
72
String JavaDoc javaPolicy = System.getProperty(JACC_POLICY_PROVIDER);
73
74         if (javaPolicy != null) {
75             try {
76                 java.security.Policy.setPolicy((java.security.Policy JavaDoc) Class.forName(javaPolicy).newInstance());
77             } catch (ClassNotFoundException JavaDoc cnfe) {
78                 // problem with property value of classpath
79
throw new SecurityException JavaDoc(cnfe.getMessage());
80             } catch (IllegalAccessException JavaDoc iae) {
81                 // problem with policy class definition
82
throw new SecurityException JavaDoc(iae.getMessage());
83             } catch (InstantiationException JavaDoc ie) {
84                 // problem with policy instantiation
85
throw new SecurityException JavaDoc(ie.getMessage());
86             } catch (ClassCastException JavaDoc cce) {
87                 // Not instance of java.security.policy
88
throw new SecurityException JavaDoc(cce.getMessage());
89             }
90             logger.info("Using policy provider ''{0}''", javaPolicy);
91         } else {
92             // Sets the EasyBeans delegating policy provider
93
logger.info("Using EasyBeans policy provider ''{0}''.", JPolicy.class.getName());
94             java.security.Policy.setPolicy(JPolicy.getInstance());
95         }
96
97         // Defines the EasyBeans JACC provider if no provider is already defined
98
// Section 2.3
99
String JavaDoc jaccFactoryProvider = System.getProperty("JACC_POLICY_CONFIG_FACTORY_PROVIDER");
100         if (jaccFactoryProvider == null) {
101             // Set JACC provider
102
logger.info("Using EasyBeans PolicyConfigurationFactory provider and EasyBeans Policy provider");
103             System.setProperty(JACC_POLICY_CONFIG_FACTORY_PROVIDER, JPolicyConfigurationFactory.class.getName());
104         } else {
105             logger.info("Using factory ''{0}'' as PolicyConfigurationFactory provider.", jaccFactoryProvider);
106         }
107
108     }
109
110 }
111
Popular Tags