KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > security > SecurityFactory


1 /*
2  * $Id: SecurityFactory.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.security;
26
27 import org.ofbiz.base.config.GenericConfigException;
28 import org.ofbiz.base.config.SecurityConfigUtil;
29 import org.ofbiz.base.util.Debug;
30 import org.ofbiz.base.util.UtilProperties;
31 import org.ofbiz.base.util.UtilValidate;
32 import org.ofbiz.entity.GenericDelegator;
33 import org.w3c.dom.Element JavaDoc;
34
35 /**
36  * <code>SecurityFactory</code>
37  *
38  * This Factory class returns an instance of a security implementation.
39  *
40  * Setting the security implementation className is done in security.xml.
41  * If no customiz security name is given, the default implementation will be used (OFBizSecurity)
42  *
43  * @author <a HREF="mailto:hermanns@aixcept.de">Rainer Hermanns</a>
44  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
45  * @version $Rev: 5462 $
46  * @since 2.0
47  */

48 public class SecurityFactory {
49     
50     public static final String JavaDoc module = SecurityFactory.class.getName();
51     public static final String JavaDoc DEFAULT_SECURITY = "org.ofbiz.security.OFBizSecurity";
52     
53     private static String JavaDoc securityName = null;
54     private static Element JavaDoc rootElement = null;
55     private static SecurityConfigUtil.SecurityInfo securityInfo = null;
56
57     /**
58      * Returns an instance of a Security implementation as defined in the security.xml by defined name
59      * in security.properties.
60      *
61      * @param delegator the generic delegator
62      * @return instance of security implementation (default: OFBizSecurity)
63      */

64     public static Security getInstance(GenericDelegator delegator) throws SecurityConfigurationException {
65         Security security = null;
66
67         // Make securityName a singleton
68
if (securityName == null) {
69             String JavaDoc _securityName = UtilProperties.getPropertyValue("security.properties", "security.context");
70             securityName = _securityName;
71         }
72
73         if (Debug.verboseOn()) Debug.logVerbose("[SecurityFactory.getInstance] Security implementation context name from security.properties: " + securityName, module);
74
75         synchronized (SecurityFactory.class) {
76             try {
77                 ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
78                 Class JavaDoc c = loader.loadClass(getSecurityClass(securityName));
79                 security = (Security) c.newInstance();
80                 security.setDelegator(delegator);
81             } catch (ClassNotFoundException JavaDoc cnf) {
82                 throw new SecurityConfigurationException("Cannot load security implementation class", cnf);
83             } catch (InstantiationException JavaDoc ie) {
84                 throw new SecurityConfigurationException("Cannot get instance of the security implementation", ie);
85             } catch (IllegalAccessException JavaDoc iae) {
86                 throw new SecurityConfigurationException(iae.getMessage(), iae);
87             }
88         }
89
90         if (Debug.verboseOn()) Debug.logVerbose("[SecurityFactory.getInstance] Security implementation successfully loaded!!!", module);
91
92         return security;
93     }
94
95     /**
96      * Returns the class name of a custom Security implementation.
97      * The default class name (org.ofbiz.security.OFBizSecurity) may be overridden by a customized implementation
98      * class name in security.xml.
99      *
100      * @param securityName the security context name to be looked up
101      * @return className the class name of the security implementatin
102      * @throws SecurityConfigurationException
103      */

104     private static String JavaDoc getSecurityClass(String JavaDoc securityName) throws SecurityConfigurationException {
105         String JavaDoc className = null;
106
107         if (Debug.verboseOn())
108             Debug.logVerbose("[SecurityFactory.getSecurityClass] Security implementation context name: " + securityName, module);
109
110         // Only load rootElement again, if not yet loaded (singleton)
111
if (rootElement == null) {
112             try {
113                 SecurityConfigUtil.getXmlDocument();
114                 Element JavaDoc _rootElement = SecurityConfigUtil.getXmlRootElement();
115
116                 rootElement = _rootElement;
117             } catch (GenericConfigException e) {
118                 Debug.logError(e, "Error getting Security Config XML root element", module);
119                 return null;
120             }
121         }
122
123         if (securityInfo == null) {
124             SecurityConfigUtil.SecurityInfo _securityInfo = SecurityConfigUtil.getSecurityInfo(securityName);
125
126             // Make sure, that the security conetxt name is defined and present
127
if (_securityInfo == null) {
128                 throw new SecurityConfigurationException("ERROR: no security definition was found with the name " + securityName + " in security.xml");
129             }
130             securityInfo = _securityInfo;
131         }
132
133         // This is the default implementation and uses org.ofbiz.security.OFBizSecurity
134
if (UtilValidate.isEmpty(securityInfo.className)) {
135             className = DEFAULT_SECURITY;
136         } else {
137             // Use a customized security
138
className = securityInfo.className;
139         }
140
141         if (Debug.verboseOn()) Debug.logVerbose("[SecurityFactory.getSecurity] Security implementation " + className + " for security name " + securityName + " successfully loaded!!!", module);
142         return className;
143     }
144 }
145
Popular Tags