001
014
015 package com.liferay.portal.bean;
016
017 import com.liferay.portal.kernel.bean.BeanLocator;
018 import com.liferay.portal.kernel.bean.BeanLocatorException;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.util.ProxyUtil;
022
023 import java.util.ArrayList;
024 import java.util.List;
025 import java.util.Map;
026 import java.util.concurrent.ConcurrentHashMap;
027
028 import org.springframework.context.ApplicationContext;
029
030
034 public class BeanLocatorImpl implements BeanLocator {
035
036 public static final String VELOCITY_SUFFIX = ".velocity";
037
038 public BeanLocatorImpl(
039 ClassLoader classLoader, ApplicationContext applicationContext) {
040
041 _classLoader = classLoader;
042 _applicationContext = applicationContext;
043 }
044
045 public ApplicationContext getApplicationContext() {
046 return _applicationContext;
047 }
048
049 public ClassLoader getClassLoader() {
050 return _classLoader;
051 }
052
053 public String[] getNames() {
054 return _applicationContext.getBeanDefinitionNames();
055 }
056
057 public Class<?> getType(String name) {
058 try {
059 return _applicationContext.getType(name);
060 }
061 catch (Exception e) {
062 throw new BeanLocatorException(e);
063 }
064 }
065
066 public <T> Map<String, T> locate(Class<T> clazz) {
067 try {
068 return _applicationContext.getBeansOfType(clazz);
069 }
070 catch (Exception e) {
071 throw new BeanLocatorException(e);
072 }
073 }
074
075 public Object locate(String name) throws BeanLocatorException {
076 try {
077 return doLocate(name);
078 }
079 catch (Exception e) {
080 throw new BeanLocatorException(e);
081 }
082 }
083
084 protected Object doLocate(String name) throws Exception {
085 if (_log.isDebugEnabled()) {
086 _log.debug("Locating " + name);
087 }
088
089 if (name.endsWith(VELOCITY_SUFFIX)) {
090 Object bean = _velocityBeans.get(name);
091
092 if (bean == null) {
093 String originalName = name.substring(
094 0, name.length() - VELOCITY_SUFFIX.length());
095
096 bean = _applicationContext.getBean(originalName);
097
098 Class<?> beanClass = bean.getClass();
099
100 Class<?>[] interfaces = beanClass.getInterfaces();
101
102 List<Class<?>> interfacesList = new ArrayList<Class<?>>();
103
104 for (Class<?> clazz : interfaces) {
105 try {
106 interfacesList.add(
107 _classLoader.loadClass(clazz.getName()));
108 }
109 catch (ClassNotFoundException cnfe) {
110 }
111 }
112
113 bean = ProxyUtil.newProxyInstance(
114 _classLoader,
115 interfacesList.toArray(new Class<?>[interfacesList.size()]),
116 new VelocityBeanHandler(bean, _classLoader));
117
118 _velocityBeans.put(name, bean);
119 }
120
121 return bean;
122 }
123 else {
124 return _applicationContext.getBean(name);
125 }
126 }
127
128 private static Log _log = LogFactoryUtil.getLog(BeanLocatorImpl.class);
129
130 private ApplicationContext _applicationContext;
131 private ClassLoader _classLoader;
132 private Map<String, Object> _velocityBeans =
133 new ConcurrentHashMap<String, Object>();
134
135 }