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