001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: PolicyProvider.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.security.jacc;
025:
026: import org.ow2.easybeans.security.jacc.provider.JPolicy;
027: import org.ow2.easybeans.security.jacc.provider.JPolicyConfigurationFactory;
028: import org.ow2.util.log.Log;
029: import org.ow2.util.log.LogFactory;
030:
031: /**
032: * Helper class for initializing the JACC provider.
033: * @author Florent Benoit
034: */
035: public final class PolicyProvider {
036:
037: /**
038: * JACC Policy Provider property.
039: */
040: private static final String JACC_POLICY_PROVIDER = "javax.security.jacc.policy.provider";
041:
042: /**
043: * JACC Policy Configuration Factory Provider property.
044: */
045: private static final String JACC_POLICY_CONFIG_FACTORY_PROVIDER = "javax.security.jacc.PolicyConfigurationFactory.provider";
046:
047: /**
048: * Only internal constructor, as it is an utility class.
049: */
050: private PolicyProvider() {
051:
052: }
053:
054: /**
055: * Logger.
056: */
057: private static Log logger = LogFactory.getLog(PolicyProvider.class);
058:
059: /**
060: * Init the JACC configuration.
061: * Defines in JACC chapter 2
062: * @throws SecurityException if JACC policy provider can not be set
063: */
064: public static void init() throws SecurityException {
065:
066: // Check if we have to use an existing policy provider
067: // Section 2.7
068: String javaPolicy = System.getProperty(JACC_POLICY_PROVIDER);
069:
070: if (javaPolicy != null) {
071: try {
072: java.security.Policy
073: .setPolicy((java.security.Policy) Class
074: .forName(javaPolicy).newInstance());
075: } catch (ClassNotFoundException cnfe) {
076: // problem with property value of classpath
077: throw new SecurityException(cnfe.getMessage());
078: } catch (IllegalAccessException iae) {
079: // problem with policy class definition
080: throw new SecurityException(iae.getMessage());
081: } catch (InstantiationException ie) {
082: // problem with policy instantiation
083: throw new SecurityException(ie.getMessage());
084: } catch (ClassCastException cce) {
085: // Not instance of java.security.policy
086: throw new SecurityException(cce.getMessage());
087: }
088: logger.info("Using policy provider ''{0}''", javaPolicy);
089: } else {
090: // Sets the EasyBeans delegating policy provider
091: logger.info("Using EasyBeans policy provider ''{0}''.",
092: JPolicy.class.getName());
093: java.security.Policy.setPolicy(JPolicy.getInstance());
094: }
095:
096: // Defines the EasyBeans JACC provider if no provider is already defined
097: // Section 2.3
098: String jaccFactoryProvider = System
099: .getProperty("JACC_POLICY_CONFIG_FACTORY_PROVIDER");
100: if (jaccFactoryProvider == null) {
101: // Set JACC provider
102: logger
103: .info("Using EasyBeans PolicyConfigurationFactory provider and EasyBeans Policy provider");
104: System.setProperty(JACC_POLICY_CONFIG_FACTORY_PROVIDER,
105: JPolicyConfigurationFactory.class.getName());
106: } else {
107: logger
108: .info(
109: "Using factory ''{0}'' as PolicyConfigurationFactory provider.",
110: jaccFactoryProvider);
111: }
112:
113: }
114:
115: }
|