1 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 ; 34 35 48 public class SecurityFactory { 49 50 public static final String module = SecurityFactory.class.getName(); 51 public static final String DEFAULT_SECURITY = "org.ofbiz.security.OFBizSecurity"; 52 53 private static String securityName = null; 54 private static Element rootElement = null; 55 private static SecurityConfigUtil.SecurityInfo securityInfo = null; 56 57 64 public static Security getInstance(GenericDelegator delegator) throws SecurityConfigurationException { 65 Security security = null; 66 67 if (securityName == null) { 69 String _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 loader = Thread.currentThread().getContextClassLoader(); 78 Class c = loader.loadClass(getSecurityClass(securityName)); 79 security = (Security) c.newInstance(); 80 security.setDelegator(delegator); 81 } catch (ClassNotFoundException cnf) { 82 throw new SecurityConfigurationException("Cannot load security implementation class", cnf); 83 } catch (InstantiationException ie) { 84 throw new SecurityConfigurationException("Cannot get instance of the security implementation", ie); 85 } catch (IllegalAccessException 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 104 private static String getSecurityClass(String securityName) throws SecurityConfigurationException { 105 String className = null; 106 107 if (Debug.verboseOn()) 108 Debug.logVerbose("[SecurityFactory.getSecurityClass] Security implementation context name: " + securityName, module); 109 110 if (rootElement == null) { 112 try { 113 SecurityConfigUtil.getXmlDocument(); 114 Element _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 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 if (UtilValidate.isEmpty(securityInfo.className)) { 135 className = DEFAULT_SECURITY; 136 } else { 137 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 |