001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.spring.util;
016    
017    import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
018    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
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.util.ClassLoaderUtil;
025    
026    import java.util.HashMap;
027    import java.util.Map;
028    import java.util.Set;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    @DoPrivileged
034    public class SpringFactoryImpl implements SpringFactory {
035    
036            @Override
037            public Object newBean(String className) throws SpringFactoryException {
038                    return newBean(className, null);
039            }
040    
041            @Override
042            public Object newBean(String className, Map<String, Object> properties)
043                    throws SpringFactoryException {
044    
045                    try {
046                            return doNewBean(className, properties);
047                    }
048                    catch (SpringFactoryException se) {
049                            throw se;
050                    }
051                    catch (Exception e) {
052                            throw new SpringFactoryException(e);
053                    }
054            }
055    
056            public void setBeanDefinitions(Map<String, String> beanDefinitions) {
057                    _beanDefinitions = new HashMap<String, Set<String>>();
058    
059                    for (Map.Entry<String, String> entry : beanDefinitions.entrySet()) {
060                            String className = entry.getKey();
061    
062                            Set<String> properties = SetUtil.fromArray(
063                                    StringUtil.split(entry.getValue()));
064    
065                            _beanDefinitions.put(className, properties);
066                    }
067            }
068    
069            protected Object doNewBean(String className, Map<String, Object> properties)
070                    throws Exception {
071    
072                    Set<String> allowedProperties = _beanDefinitions.get(className);
073    
074                    if (allowedProperties == null) {
075                            throw new SpringFactoryException("Undefined class " + className);
076                    }
077    
078                    Object bean = InstanceFactory.newInstance(
079                            ClassLoaderUtil.getPortalClassLoader(), className);
080    
081                    if (properties == null) {
082                            return bean;
083                    }
084    
085                    for (Map.Entry<String, Object> entry : properties.entrySet()) {
086                            String name = entry.getKey();
087    
088                            if (!allowedProperties.contains(name)) {
089                                    throw new SpringFactoryException(
090                                            "Undefined property " + name + " for class " +
091                                                    className);
092                            }
093    
094                            Object value = entry.getValue();
095    
096                            BeanPropertiesUtil.setProperty(bean, name, value);
097                    }
098    
099                    return bean;
100            }
101    
102            private Map<String, Set<String>> _beanDefinitions;
103    
104    }