001    /**
002     * Copyright (c) 2000-2013 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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.util.Stack;
021    
022    import javax.sql.DataSource;
023    
024    import org.springframework.aop.TargetSource;
025    
026    /**
027     * @author Michael Young
028     */
029    public class DynamicDataSourceTargetSource implements TargetSource {
030    
031            public Stack<String> getMethodStack() {
032                    Stack<String> methodStack = _methodStack.get();
033    
034                    if (methodStack == null) {
035                            methodStack = new Stack<String>();
036    
037                            _methodStack.set(methodStack);
038                    }
039    
040                    return methodStack;
041            }
042    
043            public Operation getOperation() {
044                    Operation operation = _operationType.get();
045    
046                    if (operation == null) {
047                            operation = Operation.WRITE;
048    
049                            _operationType.set(operation);
050                    }
051    
052                    return operation;
053            }
054    
055            @Override
056            public Object getTarget() throws Exception {
057                    Operation operationType = getOperation();
058    
059                    if (operationType == Operation.READ) {
060                            if (_log.isTraceEnabled()) {
061                                    _log.trace("Returning read data source");
062                            }
063    
064                            return _readDataSource;
065                    }
066    
067                    if (_log.isTraceEnabled()) {
068                            _log.trace("Returning write data source");
069                    }
070    
071                    return _writeDataSource;
072            }
073    
074            @Override
075            public Class<DataSource> getTargetClass() {
076                    return DataSource.class;
077            }
078    
079            @Override
080            public boolean isStatic() {
081                    return false;
082            }
083    
084            public String popMethod() {
085                    Stack<String> methodStack = getMethodStack();
086    
087                    String method = methodStack.pop();
088    
089                    setOperation(Operation.WRITE);
090    
091                    return method;
092            }
093    
094            public void pushMethod(String method) {
095                    Stack<String> methodStack = getMethodStack();
096    
097                    methodStack.push(method);
098            }
099    
100            @Override
101            public void releaseTarget(Object target) throws Exception {
102            }
103    
104            public void setOperation(Operation operation) {
105                    if (_log.isDebugEnabled()) {
106                            _log.debug("Method stack " + getMethodStack());
107                    }
108    
109                    if (!inOperation() || (operation == Operation.WRITE)) {
110                            _operationType.set(operation);
111                    }
112            }
113    
114            public void setReadDataSource(DataSource readDataSource) {
115                    _readDataSource = readDataSource;
116            }
117    
118            public void setWriteDataSource(DataSource writeDataSource) {
119                    _writeDataSource = writeDataSource;
120            }
121    
122            protected boolean inOperation() {
123                    Stack<String> methodStack = getMethodStack();
124    
125                    return !methodStack.empty();
126            }
127    
128            private static Log _log = LogFactoryUtil.getLog(
129                    DynamicDataSourceTargetSource.class);
130    
131            private static ThreadLocal<Stack<String>> _methodStack =
132                    new ThreadLocal<Stack<String>>();
133            private static ThreadLocal<Operation> _operationType =
134                    new ThreadLocal<Operation>();
135    
136            private DataSource _readDataSource;
137            private DataSource _writeDataSource;
138    
139    }