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