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