KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > security > util > ConfigurationUtil


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.util;
19
20 import java.lang.reflect.Constructor JavaDoc;
21 import java.security.AccessController JavaDoc;
22 import java.security.PrivilegedActionException JavaDoc;
23 import java.security.PrivilegedExceptionAction JavaDoc;
24 import java.util.Set JavaDoc;
25 import javax.security.auth.Subject JavaDoc;
26 import javax.security.auth.x500.X500Principal JavaDoc;
27 import javax.security.jacc.PolicyContext JavaDoc;
28 import javax.security.jacc.PolicyContextException JavaDoc;
29 import javax.security.jacc.PolicyContextHandler JavaDoc;
30
31 import org.apache.geronimo.common.DeploymentException;
32 import org.apache.geronimo.common.GeronimoSecurityException;
33 import org.apache.geronimo.security.DomainPrincipal;
34 import org.apache.geronimo.security.PrimaryDomainPrincipal;
35 import org.apache.geronimo.security.PrimaryPrincipal;
36 import org.apache.geronimo.security.PrimaryRealmPrincipal;
37 import org.apache.geronimo.security.RealmPrincipal;
38 import org.apache.geronimo.security.deploy.DefaultDomainPrincipal;
39 import org.apache.geronimo.security.deploy.DefaultPrincipal;
40 import org.apache.geronimo.security.deploy.DefaultRealmPrincipal;
41 import org.apache.geronimo.security.deploy.PrincipalInfo;
42
43
44 /**
45  * A collection of utility functions that assist with the configuration of
46  * <code>PolicyConfiguration</code>s.
47  *
48  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
49  * @see javax.security.jacc.PolicyConfiguration
50  * @see "JSR 115" Java Authorization Contract for Containers
51  */

52 public class ConfigurationUtil {
53
54     /**
55      * Create an X500Principal from a deployment description.
56      *
57      * @param name the distinguished name of the principal
58      * @return an X500Principal from a deployment description
59      */

60     public static X500Principal JavaDoc generateX500Principal(String JavaDoc name) {
61         return new X500Principal JavaDoc(name);
62     }
63
64     /**
65      * Create a Principal from a deployment description.
66      *
67      * @param principalInfo the deployment description of the principal to be created.
68      * @param classLoader
69      * @return a RealmPrincipal from a deployment description
70      */

71     public static java.security.Principal JavaDoc generatePrincipal(final PrincipalInfo principalInfo, ClassLoader JavaDoc classLoader) {
72         return generatePrincipal(principalInfo.getClassName(), principalInfo.getPrincipalName(), classLoader);
73     }
74
75     public static java.security.Principal JavaDoc generatePrincipal(final String JavaDoc className, final String JavaDoc principalName, final ClassLoader JavaDoc classLoader) {
76         try {
77             return (java.security.Principal JavaDoc) AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc() {
78                 public Object JavaDoc run() throws Exception JavaDoc {
79                     Class JavaDoc clazz = classLoader.loadClass(className);
80                     Constructor JavaDoc constructor = clazz.getDeclaredConstructor(new Class JavaDoc[]{String JavaDoc.class});
81                     return (java.security.Principal JavaDoc) constructor.newInstance(new Object JavaDoc[]{principalName});
82                 }
83             });
84         } catch (PrivilegedActionException JavaDoc e) {
85             e.printStackTrace();
86             if (e.getException() != null) {
87                 e.getException().printStackTrace();
88             }
89             return null;
90         }
91     }
92
93     /**
94      * Create a RealmPrincipal from a deployment description.
95      *
96      * @param principalInfo the deployment description of the principal to be created.
97      * @param classLoader
98      * @return a RealmPrincipal from a deployment description
99      */

100     public static RealmPrincipal generateRealmPrincipal(final String JavaDoc realm, final String JavaDoc loginDomain, final PrincipalInfo principalInfo, ClassLoader JavaDoc classLoader) {
101         return generateRealmPrincipal(realm, loginDomain, principalInfo.getClassName(), principalInfo.getPrincipalName(), classLoader);
102     }
103
104     public static RealmPrincipal generateRealmPrincipal(final String JavaDoc realm, final String JavaDoc loginDomain, final String JavaDoc className, final String JavaDoc principalName,
105                                                         ClassLoader JavaDoc classLoader)
106     {
107         return new RealmPrincipal(realm, loginDomain, generatePrincipal(className, principalName, classLoader));
108     }
109
110     /**
111      * Create a DomainPrincipal from a deployment description.
112      *
113      * @param principalInfo the deployment description of the principal to be created.
114      * @param classLoader
115      * @return a RealmPrincipal from a deployment description
116      */

117     public static DomainPrincipal generateDomainPrincipal(final String JavaDoc loginDomain, final PrincipalInfo principalInfo, ClassLoader JavaDoc classLoader) {
118         return generateDomainPrincipal(loginDomain, principalInfo.getClassName(), principalInfo.getPrincipalName(), classLoader);
119     }
120
121     public static DomainPrincipal generateDomainPrincipal(final String JavaDoc loginDomain, final String JavaDoc className, final String JavaDoc principalName, ClassLoader JavaDoc classLoader) {
122         return new DomainPrincipal(loginDomain, generatePrincipal(className, principalName, classLoader));
123     }
124
125     /**
126      * Create a RealmPrincipal from a deployment description.
127      *
128      * @param principalInfo the deployment description of the principal to be created.
129      * @param classLoader
130      * @return a PrimaryRealmPrincipal from a deployment description
131      */

132     public static PrimaryRealmPrincipal generatePrimaryRealmPrincipal(final String JavaDoc realm, final String JavaDoc domain, final PrincipalInfo principalInfo, ClassLoader JavaDoc classLoader) throws DeploymentException {
133         return generatePrimaryRealmPrincipal(realm, domain, principalInfo.getClassName(), principalInfo.getPrincipalName(), classLoader);
134     }
135
136     public static PrimaryRealmPrincipal generatePrimaryRealmPrincipal(final String JavaDoc realm, final String JavaDoc domain, final String JavaDoc className, final String JavaDoc principalName,
137                                                                       final ClassLoader JavaDoc classLoader) throws DeploymentException
138     {
139         try {
140             return (PrimaryRealmPrincipal) AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc() {
141                 public Object JavaDoc run() throws Exception JavaDoc {
142                     java.security.Principal JavaDoc p = null;
143                     Class JavaDoc clazz = classLoader.loadClass(className);
144                     Constructor JavaDoc constructor = clazz.getDeclaredConstructor(new Class JavaDoc[]{String JavaDoc.class});
145                     p = (java.security.Principal JavaDoc) constructor.newInstance(new Object JavaDoc[]{principalName});
146
147                     return new PrimaryRealmPrincipal(realm, domain, p);
148                 }
149             });
150         } catch (PrivilegedActionException JavaDoc pae) {
151             throw new DeploymentException("Unable to create realm principal", pae.getException());
152         }
153     }
154
155     /**
156      * Create a DomainPrincipal from a deployment description.
157      *
158      * @param principalInfo the deployment description of the principal to be created.
159      * @param classLoader
160      * @return a PrimaryDomainPrincipal from a deployment description
161      */

162     public static PrimaryDomainPrincipal generatePrimaryDomainPrincipal(final String JavaDoc domain, final PrincipalInfo principalInfo, ClassLoader JavaDoc classLoader) throws DeploymentException {
163         return generatePrimaryDomainPrincipal(domain, principalInfo.getClassName(), principalInfo.getPrincipalName(), classLoader);
164     }
165
166     public static PrimaryDomainPrincipal generatePrimaryDomainPrincipal(final String JavaDoc domain, final String JavaDoc className, final String JavaDoc principalName,
167                                                                         final ClassLoader JavaDoc classLoader) throws DeploymentException
168     {
169         try {
170             return (PrimaryDomainPrincipal) AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc() {
171                 public Object JavaDoc run() throws Exception JavaDoc {
172                     java.security.Principal JavaDoc p = null;
173                     Class JavaDoc clazz = classLoader.loadClass(className);
174                     Constructor JavaDoc constructor = clazz.getDeclaredConstructor(new Class JavaDoc[]{String JavaDoc.class});
175                     p = (java.security.Principal JavaDoc) constructor.newInstance(new Object JavaDoc[]{principalName});
176
177                     return new PrimaryDomainPrincipal(domain, p);
178                 }
179             });
180         } catch (PrivilegedActionException JavaDoc pae) {
181             throw new DeploymentException("Unable to create domain principal", pae.getException());
182         }
183     }
184
185     /**
186      * Create a Principal from a deployment description.
187      *
188      * @param principalInfo the deployment description of the principal to be created.
189      * @param classLoader
190      * @return a Principal from a deployment description
191      */

192     public static PrimaryPrincipal generatePrimaryPrincipal(final PrincipalInfo principalInfo, ClassLoader JavaDoc classLoader) throws DeploymentException {
193         return generatePrimaryPrincipal(principalInfo.getClassName(), principalInfo.getPrincipalName(), classLoader);
194     }
195
196     public static PrimaryPrincipal generatePrimaryPrincipal(final String JavaDoc className, final String JavaDoc principalName, final ClassLoader JavaDoc classLoader) throws DeploymentException {
197         try {
198             return (PrimaryPrincipal) AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc() {
199                 public Object JavaDoc run() throws Exception JavaDoc {
200                     java.security.Principal JavaDoc p = null;
201                     Class JavaDoc clazz = classLoader.loadClass(className);
202                     Constructor JavaDoc constructor = clazz.getDeclaredConstructor(new Class JavaDoc[]{String JavaDoc.class});
203                     p = (java.security.Principal JavaDoc) constructor.newInstance(new Object JavaDoc[]{principalName});
204
205                     return new PrimaryPrincipal(p);
206                 }
207             });
208         } catch (PrivilegedActionException JavaDoc pae) {
209             throw new DeploymentException("Unable to create principal", pae.getException());
210         }
211     }
212
213     /**
214      * Generate the default principal from the security config.
215      *
216      * @param defaultPrincipal
217      * @param classLoader
218      * @return the default principal
219      */

220     public static Subject JavaDoc generateDefaultSubject(DefaultPrincipal defaultPrincipal, ClassLoader JavaDoc classLoader) throws DeploymentException {
221         if (defaultPrincipal == null) {
222             throw new GeronimoSecurityException("No DefaultPrincipal configuration supplied");
223         }
224         Subject JavaDoc defaultSubject = new Subject JavaDoc();
225         java.security.Principal JavaDoc principal;
226         java.security.Principal JavaDoc primaryPrincipal;
227
228         if (defaultPrincipal instanceof DefaultRealmPrincipal) {
229             DefaultRealmPrincipal defaultRealmPrincipal = (DefaultRealmPrincipal) defaultPrincipal;
230             principal = generateRealmPrincipal(defaultRealmPrincipal.getRealm(), defaultRealmPrincipal.getDomain(), defaultRealmPrincipal.getPrincipal(), classLoader);
231             primaryPrincipal = generatePrimaryRealmPrincipal(defaultRealmPrincipal.getRealm(), defaultRealmPrincipal.getDomain(), defaultRealmPrincipal.getPrincipal(), classLoader);
232         } else if (defaultPrincipal instanceof DefaultDomainPrincipal) {
233             DefaultDomainPrincipal defaultDomainPrincipal = (DefaultDomainPrincipal) defaultPrincipal;
234             principal = generateDomainPrincipal(defaultDomainPrincipal.getDomain(), defaultDomainPrincipal.getPrincipal(), classLoader);
235             primaryPrincipal = generatePrimaryDomainPrincipal(defaultDomainPrincipal.getDomain(), defaultDomainPrincipal.getPrincipal(), classLoader);
236         } else {
237             principal = generatePrincipal(defaultPrincipal.getPrincipal(), classLoader);
238             primaryPrincipal = generatePrimaryPrincipal(defaultPrincipal.getPrincipal(), classLoader);
239
240         }
241         defaultSubject.getPrincipals().add(principal);
242         defaultSubject.getPrincipals().add(primaryPrincipal);
243
244         Set JavaDoc namedUserPasswordCredentials = defaultPrincipal.getNamedUserPasswordCredentials();
245         if (namedUserPasswordCredentials != null) {
246             defaultSubject.getPrivateCredentials().addAll(namedUserPasswordCredentials);
247         }
248
249         return defaultSubject;
250     }
251
252
253     /**
254      * A simple helper method to register PolicyContextHandlers
255      *
256      * @param handler an object that implements the <code>PolicyContextHandler</code>
257      * interface. The value of this parameter must not be null.
258      * @param replace this boolean value defines the behavior of this method
259      * if, when it is called, a <code>PolicyContextHandler</code> has already
260      * been registered to handle the same key. In that case, and if the value
261      * of this argument is true, the existing handler is replaced with the
262      * argument handler. If the value of this parameter is false the existing
263      * registration is preserved and an exception is thrown.
264      */

265     public static void registerPolicyContextHandler(PolicyContextHandler JavaDoc handler, boolean replace) throws PolicyContextException JavaDoc {
266         String JavaDoc[] keys = handler.getKeys();
267
268         for (int i = 0; i < keys.length; i++) {
269             PolicyContext.registerHandler(keys[i], handler, replace);
270         }
271     }
272
273
274 }
275
Popular Tags