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 javax.sql.DataSource;
22  
23  import org.springframework.aop.TargetSource;
24  
25  /**
26   * <a href="ShardDataSourceTargetSource.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Michael Young
29   */
30  public class ShardDataSourceTargetSource implements TargetSource {
31  
32      public DataSource getDataSource() {
33          return _dataSource.get();
34      }
35  
36      public Object getTarget() throws Exception {
37          return getDataSource();
38      }
39  
40      public Class<DataSource> getTargetClass() {
41          return DataSource.class;
42      }
43  
44      public boolean isStatic() {
45          return false;
46      }
47  
48      public void releaseTarget(Object target) throws Exception {
49      }
50  
51      public void setDataSource(String shardName) {
52          _dataSource.set(_dataSources.get(shardName));
53      }
54  
55      public void setDataSources(Map<String, DataSource> dataSources) {
56          _dataSources = dataSources;
57      }
58  
59      private static ThreadLocal<DataSource> _dataSource =
60          new ThreadLocal<DataSource>() {
61  
62          protected DataSource initialValue() {
63              return _dataSources.get(PropsValues.SHARD_DEFAULT_NAME);
64          }
65  
66      };
67  
68      private static Map<String, DataSource> _dataSources;
69  
70  }