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 Object getTarget() throws Exception {
062                    Operation operationType = getOperation();
063    
064                    if (operationType == Operation.READ) {
065                            if (_log.isTraceEnabled()) {
066                                    _log.trace("Returning read data source");
067                            }
068    
069                            return _readDataSource;
070                    }
071    
072                    if (_log.isTraceEnabled()) {
073                            _log.trace("Returning write data source");
074                    }
075    
076                    return _writeDataSource;
077            }
078    
079            @Override
080            public Class<DataSource> getTargetClass() {
081                    return DataSource.class;
082            }
083    
084            @Override
085            public boolean isStatic() {
086                    return false;
087            }
088    
089            @Override
090            public String popMethod() {
091                    Stack<String> methodStack = getMethodStack();
092    
093                    String method = methodStack.pop();
094    
095                    setOperation(Operation.WRITE);
096    
097                    return method;
098            }
099    
100            @Override
101            public void pushMethod(String method) {
102                    Stack<String> methodStack = getMethodStack();
103    
104                    methodStack.push(method);
105            }
106    
107            @Override
108            public void releaseTarget(Object target) throws Exception {
109            }
110    
111            @Override
112            public void setOperation(Operation operation) {
113                    if (_log.isDebugEnabled()) {
114                            _log.debug("Method stack " + getMethodStack());
115                    }
116    
117                    if (!inOperation() || (operation == Operation.WRITE)) {
118                            _operationType.set(operation);
119                    }
120            }
121    
122            @Override
123            public void setReadDataSource(DataSource readDataSource) {
124                    _readDataSource = readDataSource;
125            }
126    
127            @Override
128            public void setWriteDataSource(DataSource writeDataSource) {
129                    _writeDataSource = writeDataSource;
130            }
131    
132            protected boolean inOperation() {
133                    Stack<String> methodStack = getMethodStack();
134    
135                    return !methodStack.empty();
136            }
137    
138            private static final Log _log = LogFactoryUtil.getLog(
139                    DefaultDynamicDataSourceTargetSource.class);
140    
141            private static final ThreadLocal<Stack<String>> _methodStack =
142                    new ThreadLocal<>();
143            private static final ThreadLocal<Operation> _operationType =
144                    new ThreadLocal<>();
145    
146            private DataSource _readDataSource;
147            private DataSource _writeDataSource;
148    
149    }