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.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  }