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