1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.dao.shard;
16  
17  import com.liferay.portal.util.PropsValues;
18  
19  import java.util.Map;
20  
21  import org.hibernate.SessionFactory;
22  
23  import org.springframework.aop.TargetSource;
24  
25  /**
26   * <a href="ShardSessionFactoryTargetSource.java.html"><b><i>View Source</i></b>
27   * </a>
28   *
29   * @author Michael Young
30   */
31  public class ShardSessionFactoryTargetSource implements TargetSource {
32  
33      public SessionFactory getSessionFactory() {
34          return _sessionFactory.get();
35      }
36  
37      public Object getTarget() throws Exception {
38          return getSessionFactory();
39      }
40  
41      public Class<?> getTargetClass() {
42          return _sessionFactories.get(PropsValues.SHARD_DEFAULT_NAME).getClass();
43      }
44  
45      public boolean isStatic() {
46          return false;
47      }
48  
49      public void releaseTarget(Object target) throws Exception {
50      }
51  
52      public void setSessionFactory(String shardName) {
53          _sessionFactory.set(_sessionFactories.get(shardName));
54      }
55  
56      public void setSessionFactories(
57          Map<String, SessionFactory> sessionFactories) {
58  
59          _sessionFactories = sessionFactories;
60      }
61  
62      private static ThreadLocal<SessionFactory> _sessionFactory =
63          new ThreadLocal<SessionFactory>() {
64  
65          protected SessionFactory initialValue() {
66              return _sessionFactories.get(PropsValues.SHARD_DEFAULT_NAME);
67          }
68  
69      };
70  
71      private static Map<String, SessionFactory> _sessionFactories;
72  
73  }