1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portal.bean;
16  
17  import com.liferay.portal.kernel.bean.BeanLocator;
18  import com.liferay.portal.kernel.bean.BeanLocatorException;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.ListUtil;
22  
23  import java.lang.reflect.Proxy;
24  
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.concurrent.ConcurrentHashMap;
29  
30  import org.springframework.context.ApplicationContext;
31  
32  /**
33   * <a href="BeanLocatorImpl.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   */
37  public class BeanLocatorImpl implements BeanLocator {
38  
39      public static final String VELOCITY_SUFFIX = ".velocity";
40  
41      public BeanLocatorImpl(
42          ClassLoader classLoader, ApplicationContext applicationContext) {
43  
44          _classLoader = classLoader;
45          _applicationContext = applicationContext;
46      }
47  
48      public ApplicationContext getApplicationContext() {
49          return _applicationContext;
50      }
51  
52      public ClassLoader getClassLoader() {
53          return _classLoader;
54      }
55  
56      public Object locate(String name) throws BeanLocatorException {
57          if (_log.isDebugEnabled()) {
58              _log.debug("Locating " + name);
59          }
60  
61          if (name.endsWith(VELOCITY_SUFFIX)) {
62              Object bean = _velocityBeans.get(name);
63  
64              if (bean == null) {
65                  String originalName = name.substring(
66                      0, name.length() - VELOCITY_SUFFIX.length());
67  
68                  bean = _applicationContext.getBean(originalName);
69  
70                  Class<?>[] interfaces = bean.getClass().getInterfaces();
71  
72                  List<Class<?>> interfacesList = ListUtil.fromArray(interfaces);
73  
74                  Iterator<Class<?>> itr = interfacesList.iterator();
75  
76                  while (itr.hasNext()) {
77                      Class<?> classObj = itr.next();
78  
79                      try {
80                          _classLoader.loadClass(classObj.getName());
81                      }
82                      catch (Exception e) {
83                          itr.remove();
84                      }
85                  }
86  
87                  bean = Proxy.newProxyInstance(
88                      _classLoader,
89                      interfacesList.toArray(new Class<?>[interfacesList.size()]),
90                      new VelocityBeanHandler(bean, _classLoader));
91  
92                  _velocityBeans.put(name, bean);
93              }
94  
95              return bean;
96          }
97          else {
98              return _applicationContext.getBean(name);
99          }
100     }
101 
102     private static Log _log = LogFactoryUtil.getLog(BeanLocatorImpl.class);
103 
104     private ClassLoader _classLoader;
105     private ApplicationContext _applicationContext;
106     private Map<String, Object> _velocityBeans =
107         new ConcurrentHashMap<String, Object>();
108 
109 }