All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.portal.spring.util.SpringFactoryImpl Maven / Gradle / Ivy

There is a newer version: 7.4.3.112-ga112
Show newest version
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.portal.spring.util;

import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
import com.liferay.portal.kernel.spring.util.FactoryBean;
import com.liferay.portal.kernel.spring.util.SpringFactory;
import com.liferay.portal.kernel.spring.util.SpringFactoryException;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
import com.liferay.portal.kernel.util.SetUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author Brian Wing Shun Chan
 */
public class SpringFactoryImpl implements SpringFactory {

	@Override
	public Object newBean(String className) throws SpringFactoryException {
		return newBean(className, null);
	}

	@Override
	public Object newBean(String className, Map properties)
		throws SpringFactoryException {

		try {
			return doNewBean(className, properties);
		}
		catch (SpringFactoryException sfe) {
			throw sfe;
		}
		catch (Exception e) {
			throw new SpringFactoryException(e);
		}
	}

	public void setBeanDefinitions(Map beanDefinitions) {
		_beanDefinitions = new HashMap<>();

		for (Map.Entry entry : beanDefinitions.entrySet()) {
			String className = entry.getKey();

			Set properties = SetUtil.fromArray(
				StringUtil.split(entry.getValue()));

			_beanDefinitions.put(className, properties);
		}
	}

	protected Object doNewBean(String className, Map properties)
		throws Exception {

		Set allowedProperties = _beanDefinitions.get(className);

		if (allowedProperties == null) {
			throw new SpringFactoryException("Undefined class " + className);
		}

		Object bean = InstanceFactory.newInstance(
			PortalClassLoaderUtil.getClassLoader(), className);

		FactoryBean factoryBean = null;

		if (bean instanceof FactoryBean) {
			factoryBean = (FactoryBean)bean;

			bean = factoryBean.create();
		}

		if (properties != null) {
			for (Map.Entry entry : properties.entrySet()) {
				String name = entry.getKey();

				if (!allowedProperties.contains(name)) {
					throw new SpringFactoryException(
						StringBundler.concat(
							"Undefined property ", name, " for class ",
							className));
				}

				Object value = entry.getValue();

				BeanPropertiesUtil.setProperty(bean, name, value);
			}
		}

		if (factoryBean != null) {
			bean = factoryBean.postProcessing(bean);
		}

		return bean;
	}

	private Map> _beanDefinitions;

}