KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > security > PolicyProvider


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@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  * Initial developer: Florent BENOIT
22  * --------------------------------------------------------------------------
23  * $Id: PolicyProvider.java,v 1.5 2004/07/08 13:59:02 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.security;
28
29 import org.objectweb.jonas_lib.security.jacc.JPolicy;
30
31 import org.objectweb.jonas.common.Log;
32
33 import org.objectweb.util.monolog.api.BasicLevel;
34 import org.objectweb.util.monolog.api.Logger;
35
36 /**
37  * Helper class for initializing the JACC provider
38  * @author Florent Benoit
39  */

40 public class PolicyProvider {
41
42     /**
43      * Only internal constructor, as it is an utility class
44      */

45     private PolicyProvider() {
46
47     }
48
49     /**
50      * Logger which is used
51      */

52     private static Logger logger = null;
53
54     /**
55      * Init the JACC configuration
56      * Defines in JACC Section 2
57      * @throws SecurityException if JACC policy provider can not be set
58      */

59     public static void init() throws SecurityException JavaDoc {
60
61         if (logger == null) {
62             logger = Log.getLogger(Log.JONAS_SECURITY_PREFIX);
63         }
64
65         // Check if we have to use an existing policy provider
66
// Section 2.7
67

68         String JavaDoc javaPolicy = System.getProperty("javax.security.jacc.policy.provider");
69
70         if (javaPolicy != null) {
71             try {
72                 java.security.Policy.setPolicy((java.security.Policy JavaDoc) Class.forName(javaPolicy).newInstance());
73             } catch (ClassNotFoundException JavaDoc cnfe) {
74                 // problem with property value of classpath
75
throw new SecurityException JavaDoc(cnfe.getMessage());
76             } catch (IllegalAccessException JavaDoc iae) {
77                 // problem with policy class definition
78
throw new SecurityException JavaDoc(iae.getMessage());
79             } catch (InstantiationException JavaDoc ie) {
80                 // problem with policy instantiation
81
throw new SecurityException JavaDoc(ie.getMessage());
82             } catch (ClassCastException JavaDoc cce) {
83                 // Not instance of java.security.policy
84
throw new SecurityException JavaDoc(cce.getMessage());
85             }
86             logger.log(BasicLevel.INFO, "Using policy provider '" + javaPolicy + "'");
87         }
88
89         // Defines the JOnAS JACC provider if no provider is already defined
90
// Section 2.3
91
String JavaDoc jaccFactoryProvider = System.getProperty("javax.security.jacc.PolicyConfigurationFactory.provider");
92         if (jaccFactoryProvider == null) {
93             logger.log(BasicLevel.INFO, "Using JOnAS PolicyConfigurationFactory provider and JOnAS Policy provider");
94             System.setProperty("javax.security.jacc.PolicyConfigurationFactory.provider", "org.objectweb.jonas_lib.security.jacc.PolicyConfigurationFactoryWrapper");
95             // Add the JOnAS delegating policy provider
96
java.security.Policy.setPolicy(JPolicy.getInstance());
97         } else {
98             logger.log(BasicLevel.INFO, "Using factory '" + jaccFactoryProvider + "' as PolicyConfigurationFactory provider");
99         }
100
101         // TODO : Register Context Handler
102

103
104     }
105
106 }
107
Popular Tags