001
014
015 package com.liferay.portal.spring.util;
016
017 import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
018 import com.liferay.portal.kernel.spring.util.FactoryBean;
019 import com.liferay.portal.kernel.spring.util.SpringFactory;
020 import com.liferay.portal.kernel.spring.util.SpringFactoryException;
021 import com.liferay.portal.kernel.util.InstanceFactory;
022 import com.liferay.portal.kernel.util.SetUtil;
023 import com.liferay.portal.kernel.util.StringUtil;
024 import com.liferay.portal.security.lang.PortalSecurityManagerThreadLocal;
025 import com.liferay.portal.util.ClassLoaderUtil;
026
027 import java.util.HashMap;
028 import java.util.Map;
029 import java.util.Set;
030
031
034 public class SpringFactoryImpl implements SpringFactory {
035
036 public Object newBean(String className) throws SpringFactoryException {
037 return newBean(className, null);
038 }
039
040 public Object newBean(String className, Map<String, Object> properties)
041 throws SpringFactoryException {
042
043 try {
044 return doNewBean(className, properties);
045 }
046 catch (SpringFactoryException se) {
047 throw se;
048 }
049 catch (Exception e) {
050 throw new SpringFactoryException(e);
051 }
052 }
053
054 public void setBeanDefinitions(Map<String, String> beanDefinitions) {
055 _beanDefinitions = new HashMap<String, Set<String>>();
056
057 for (Map.Entry<String, String> entry : beanDefinitions.entrySet()) {
058 String className = entry.getKey();
059
060 Set<String> properties = SetUtil.fromArray(
061 StringUtil.split(entry.getValue()));
062
063 _beanDefinitions.put(className, properties);
064 }
065 }
066
067 protected Object doNewBean(String className, Map<String, Object> properties)
068 throws Exception {
069
070 Set<String> allowedProperties = _beanDefinitions.get(className);
071
072 if (allowedProperties == null) {
073 throw new SpringFactoryException("Undefined class " + className);
074 }
075
076 boolean enabled = PortalSecurityManagerThreadLocal.isEnabled();
077
078 try {
079 PortalSecurityManagerThreadLocal.setEnabled(false);
080
081 Object bean = InstanceFactory.newInstance(
082 ClassLoaderUtil.getPortalClassLoader(), className);
083
084 if (bean instanceof FactoryBean) {
085 FactoryBean<Object> factoryBean = (FactoryBean<Object>)bean;
086
087 bean = factoryBean.create();
088 }
089
090 if (properties == null) {
091 return bean;
092 }
093
094 for (Map.Entry<String, Object> entry : properties.entrySet()) {
095 String name = entry.getKey();
096
097 if (!allowedProperties.contains(name)) {
098 throw new SpringFactoryException(
099 "Undefined property " + name + " for class " +
100 className);
101 }
102
103 Object value = entry.getValue();
104
105 BeanPropertiesUtil.setProperty(bean, name, value);
106 }
107
108 return bean;
109 }
110 finally {
111 PortalSecurityManagerThreadLocal.setEnabled(enabled);
112 }
113 }
114
115 private Map<String, Set<String>> _beanDefinitions;
116
117 }