KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > web > security > WebSecurityManagerFactory


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.web.security;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28 import com.sun.enterprise.deployment.WebBundleDescriptor;
29 import java.util.logging.*;
30 import com.sun.logging.LogDomains;
31 import java.util.List JavaDoc;
32 import java.util.ArrayList JavaDoc;
33
34 /** @author JeanFrancois Arcand
35  * @author Harpreet Singh
36  */

37 public class WebSecurityManagerFactory {
38     private static Logger logger =
39     Logger.getLogger(LogDomains.SECURITY_LOGGER);
40     
41     private Map JavaDoc securityManagerPool = new HashMap JavaDoc();
42     // stores the context ids to appnames for standalone web apps
43
private Map JavaDoc CONTEXT_ID = new HashMap JavaDoc();
44     private static WebSecurityManagerFactory factory = null;
45         
46     private WebSecurityManagerFactory() {
47     }
48     // returns the singleton instance of WebSecurityManagerFactory
49
public static synchronized WebSecurityManagerFactory getInstance(){
50         if (factory == null){
51         factory = new WebSecurityManagerFactory();
52         }
53         return factory;
54     }
55     // generates a webSecurityManager
56
public WebSecurityManager newWebSecurityManager(WebBundleDescriptor wbd){
57         String JavaDoc contextId = WebSecurityManager.getContextID(wbd);
58         String JavaDoc appname = wbd.getApplication().getRegistrationName();
59
60     synchronized (CONTEXT_ID) {
61         List JavaDoc lst = (List JavaDoc)CONTEXT_ID.get(appname);
62         if(lst == null){
63         lst = new ArrayList JavaDoc();
64         CONTEXT_ID.put(appname, lst);
65         }
66         if (!lst.contains(contextId)) {
67         lst.add(contextId);
68         }
69     }
70
71         if(logger.isLoggable(Level.FINE)){
72             logger.log(Level.FINE, "[Web-Security] Web Security:Creating WebSecurityManager for contextId = "+contextId);
73         }
74
75         WebSecurityManager wsManager = getWebSecurityManager(contextId);
76     if (wsManager == null) {
77
78         // we should see if it is safe to do the security manager
79
// construction within the synchronize block.
80
// for the time being, we will just make sure that we
81
// synchronize access to the pool.
82
try{
83         wsManager = new WebSecurityManager(wbd);
84         } catch (javax.security.jacc.PolicyContextException JavaDoc e){
85         logger.log(Level.FINE, "[Web-Security] FATAl Exception. Unable to create WebSecurityManager: " + e.getMessage() );
86         throw new RuntimeException JavaDoc(e);
87         }
88
89         synchronized (securityManagerPool) {
90         WebSecurityManager other =
91             (WebSecurityManager)securityManagerPool.get(contextId);
92         if (other == null) {
93             securityManagerPool.put(contextId, wsManager);
94         } else {
95             wsManager = other;
96         }
97         }
98     }
99     return wsManager;
100     }
101         
102     public WebSecurityManager getWebSecurityManager(String JavaDoc contextId){
103     synchronized (securityManagerPool) {
104         return (WebSecurityManager)securityManagerPool.get(contextId);
105     }
106     }
107     
108     public void removeWebSecurityManager(String JavaDoc contextId){
109         synchronized (securityManagerPool){
110         securityManagerPool.remove(contextId);
111         }
112     }
113
114     /**
115      * valid for standalone web apps
116      */

117     public String JavaDoc[] getContextIdsOfApp(String JavaDoc appName){
118     synchronized(CONTEXT_ID) {
119         List JavaDoc contextId = (List JavaDoc) CONTEXT_ID.get(appName);
120         if(contextId == null)
121         return null;
122         String JavaDoc[] arrayContext = new String JavaDoc[contextId.size()];
123         arrayContext = (String JavaDoc[])contextId.toArray(arrayContext);
124         return arrayContext;
125     }
126     }
127
128     /**
129      * valid for standalone web apps
130      */

131     public String JavaDoc[] getAndRemoveContextIdForWebAppName(String JavaDoc appName){
132     synchronized(CONTEXT_ID) {
133         String JavaDoc [] rvalue = getContextIdsOfApp(appName);
134         CONTEXT_ID.remove(appName);
135         return rvalue;
136     }
137     }
138         
139 }
140
Popular Tags