001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.dao.jdbc.aop;
016    
017    import com.liferay.portal.kernel.dao.jdbc.aop.DynamicDataSourceTargetSource;
018    import com.liferay.portal.kernel.dao.jdbc.aop.Operation;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    
022    import java.util.Stack;
023    
024    import javax.sql.DataSource;
025    
026    import org.springframework.aop.TargetSource;
027    
028    /**
029     * @author Michael Young
030     */
031    public class DefaultDynamicDataSourceTargetSource
032            implements DynamicDataSourceTargetSource, TargetSource {
033    
034            @Override
035            public Stack<String> getMethodStack() {
036                    Stack<String> methodStack = _methodStack.get();
037    
038                    if (methodStack == null) {
039                            methodStack = new Stack<>();
040    
041                            _methodStack.set(methodStack);
042                    }
043    
044                    return methodStack;
045            }
046    
047            @Override
048            public Operation getOperation() {
049                    Operation operation = _operationType.get();
050    
051                    if (operation == null) {
052                            operation = Operation.WRITE;
053    
054                            _operationType.set(operation);
055                    }
056    
057                    return operation;
058            }
059    
060            @Override
061            public DataSource getReadDataSource() {
062                    return _readDataSource;
063            }
064    
065            @Override
066            public Object getTarget() throws Exception {
067                    Operation operationType = getOperation();
068    
069                    if (operationType == Operation.READ) {
070                            if (_log.isTraceEnabled()) {
071                                    _log.trace("Returning read data source");
072                            }
073    
074                            return _readDataSource;
075                    }
076    
077                    if (_log.isTraceEnabled()) {
078                            _log.trace("Returning write data source");
079                    }
080    
081                    return _writeDataSource;
082            }
083    
084            @Override
085            public Class<DataSource> getTargetClass() {
086                    return DataSource.class;
087            }
088    
089            @Override
090            public DataSource getWriteDataSource() {
091                    return _writeDataSource;
092            }
093    
094            @Override
095            public boolean isStatic() {
096                    return false;
097            }
098    
099            @Override
100            public String popMethod() {
101                    Stack<String> methodStack = getMethodStack();
102    
103                    String method = methodStack.pop();
104    
105                    setOperation(Operation.WRITE);
106    
107                    return method;
108            }
109    
110            @Override
111            public void pushMethod(String method) {
112                    Stack<String> methodStack = getMethodStack();
113    
114                    methodStack.push(method);
115            }
116    
117            @Override
118            public void releaseTarget(Object target) throws Exception {
119            }
120    
121            @Override
122            public void setOperation(Operation operation) {
123                    if (_log.isDebugEnabled()) {
124                            _log.debug("Method stack " + getMethodStack());
125                    }
126    
127                    if (!inOperation() || (operation == Operation.WRITE)) {
128                            _operationType.set(operation);
129                    }
130            }
131    
132            @Override
133            public void setReadDataSource(DataSource readDataSource) {
134                    _readDataSource = readDataSource;
135            }
136    
137            @Override
138            public void setWriteDataSource(DataSource writeDataSource) {
139                    _writeDataSource = writeDataSource;
140            }
141    
142            protected boolean inOperation() {
143                    Stack<String> methodStack = getMethodStack();
144    
145                    return !methodStack.empty();
146            }
147    
148            private static final Log _log = LogFactoryUtil.getLog(
149                    DefaultDynamicDataSourceTargetSource.class);
150    
151            private static final ThreadLocal<Stack<String>> _methodStack =
152                    new ThreadLocal<>();
153            private static final ThreadLocal<Operation> _operationType =
154                    new ThreadLocal<>();
155    
156            private DataSource _readDataSource;
157            private DataSource _writeDataSource;
158    
159    }