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