KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > security > RealmConfig


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.security;
25
26 import java.util.*;
27 import java.util.logging.Logger JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import com.sun.logging.LogDomains;
30 import javax.security.auth.login.*;
31 import com.sun.enterprise.config.serverbeans.*;
32 import com.sun.enterprise.config.*;
33 import com.sun.enterprise.server.*;
34 import com.sun.enterprise.security.auth.realm.Realm;
35 import com.sun.enterprise.security.auth.realm.BadRealmException;
36 import com.sun.enterprise.util.LocalStringManagerImpl;
37
38
39 /**
40  * Process realm initial configuration.
41  *
42  * <P>This class contains S1AS-specific initialization code for Realms, used
43  * by RealmManager.
44  *
45  *
46  */

47
48 public class RealmConfig
49 {
50     private static Logger JavaDoc logger =
51         LogDomains.getLogger(LogDomains.SECURITY_LOGGER);
52
53     
54     public static void createRealms(String JavaDoc defaultRealm, AuthRealm[] realms)
55     {
56         assert(realms != null);
57
58         String JavaDoc goodRealm = null; // need at least one good realm
59

60         for (int i=0; i < realms.length; i++) {
61
62             AuthRealm aRealm = realms[i];
63             String JavaDoc realmName = aRealm.getName();
64             String JavaDoc realmClass = aRealm.getClassname();
65             assert (realmName != null);
66             assert (realmClass != null);
67
68             try {
69                 ElementProperty[] realmProps =
70                     aRealm.getElementProperty();
71
72                 Properties props = new Properties();
73
74                 for (int j=0; j < realmProps.length; j++) {
75                     ElementProperty p = realmProps[j];
76                     String JavaDoc name = p.getName();
77                     String JavaDoc value = p.getValue();
78                     props.setProperty(name, value);
79                 }
80                 Realm.instantiate(realmName, realmClass, props);
81
82                 logger.fine("Configured realm: " + realmName);
83
84                 if (goodRealm == null) {
85                     goodRealm = realmName;
86                 }
87             } catch (Exception JavaDoc e) {
88                 logger.log(Level.WARNING,
89                            "realmconfig.disable", realmName);
90                 logger.log(Level.WARNING, "security.exception", e);
91             }
92         }
93
94         // done loading all realms, check that there is at least one
95
// in place and that default is installed, or change default
96
// to the first one loaded (arbitrarily).
97

98         if (goodRealm == null) {
99             logger.severe("realmconfig.nogood");
100
101         } else {
102             try {
103                 Realm def = Realm.getInstance(defaultRealm);
104                 if (def == null) {
105                     defaultRealm = goodRealm;
106                 }
107             } catch (Exception JavaDoc e) {
108                 defaultRealm = goodRealm;
109             }
110             Realm.setDefaultRealm(defaultRealm);
111             logger.fine("Default realm is set to: "+ defaultRealm);
112         }
113     }
114     
115     /**
116      * Load all configured realms from server.xml and initialize each
117      * one. Initialization is done by calling Realm.initialize() with
118      * its name, class and properties. The name of the default realm
119      * is also saved in the Realm class for reference during server
120      * operation.
121      *
122      * <P>This method superceeds the RI RealmManager.createRealms() method.
123      *
124      * */

125     public static void createRealms()
126     {
127         
128         try {
129             logger.fine("Initializing configured realms.");
130             
131             ConfigContext configContext =
132                 ApplicationServer.getServerContext().getConfigContext();
133             assert(configContext != null);
134
135             Server configBean =
136                 ServerBeansFactory.getServerBean(configContext);
137             assert(configBean != null);
138
139             SecurityService securityBean =
140                 ServerBeansFactory.getSecurityServiceBean(configContext);
141             assert(securityBean != null);
142
143                                 // grab default realm name
144
String JavaDoc defaultRealm = securityBean.getDefaultRealm();
145
146                                 // get set of auth-realms and process each
147
AuthRealm[] realms = securityBean.getAuthRealm();
148             assert(realms != null);
149
150             createRealms(defaultRealm, realms);
151             
152         } catch (Exception JavaDoc e) {
153             logger.log(Level.SEVERE, "realmconfig.nogood", e);
154
155         }
156     }
157
158
159
160     
161 }
162
Popular Tags