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.spring.aop;
016    
017    import org.aopalliance.intercept.MethodInterceptor;
018    
019    /**
020     * @author Shuyang Zhou
021     */
022    public class ChainableMethodAdviceInjector {
023    
024            public void inject() {
025                    if (!_injectCondition) {
026                            return;
027                    }
028    
029                    _injectCondition = false;
030    
031                    if (_newChainableMethodAdvice == null) {
032                            throw new IllegalArgumentException(
033                                    "New Chainable method advice is null");
034                    }
035    
036                    if (_parentChainableMethodAdvice == null) {
037                            throw new IllegalArgumentException(
038                                    "Parent chainable method advice is null");
039                    }
040    
041                    if (_childMethodInterceptor == null) {
042                            _newChainableMethodAdvice.nextMethodInterceptor =
043                                    _parentChainableMethodAdvice.nextMethodInterceptor;
044                            _parentChainableMethodAdvice.nextMethodInterceptor =
045                                    _newChainableMethodAdvice;
046    
047                            return;
048                    }
049    
050                    ChainableMethodAdvice parentChainableMethodAdvice =
051                            _parentChainableMethodAdvice;
052    
053                    while ((parentChainableMethodAdvice != null) &&
054                               (parentChainableMethodAdvice.nextMethodInterceptor !=
055                                            _childMethodInterceptor)) {
056    
057                            MethodInterceptor methodInterceptor =
058                                    parentChainableMethodAdvice.nextMethodInterceptor;
059    
060                            if (!(methodInterceptor instanceof ChainableMethodAdvice)) {
061                                    break;
062                            }
063    
064                            parentChainableMethodAdvice =
065                                    (ChainableMethodAdvice)methodInterceptor;
066                    }
067    
068                    if (parentChainableMethodAdvice.nextMethodInterceptor !=
069                                    _childMethodInterceptor) {
070    
071                            throw new IllegalArgumentException(
072                                    "Unable to find " + _childMethodInterceptor + " from " +
073                                            _parentChainableMethodAdvice);
074                    }
075    
076                    _newChainableMethodAdvice.nextMethodInterceptor =
077                            parentChainableMethodAdvice.nextMethodInterceptor;
078    
079                    parentChainableMethodAdvice.nextMethodInterceptor =
080                            _newChainableMethodAdvice;
081            }
082    
083            public void setChildMethodInterceptor(
084                    MethodInterceptor childMethodInterceptor) {
085    
086                    _childMethodInterceptor = childMethodInterceptor;
087            }
088    
089            public void setInjectCondition(boolean injectCondition) {
090                    _injectCondition = injectCondition;
091            }
092    
093            public void setNewChainableMethodAdvice(
094                    ChainableMethodAdvice newChainableMethodAdvice) {
095    
096                    _newChainableMethodAdvice = newChainableMethodAdvice;
097            }
098    
099            public void setParentChainableMethodAdvice(
100                    ChainableMethodAdvice parentChainableMethodAdvice) {
101    
102                    _parentChainableMethodAdvice = parentChainableMethodAdvice;
103            }
104    
105            private MethodInterceptor _childMethodInterceptor;
106            private boolean _injectCondition;
107            private ChainableMethodAdvice _newChainableMethodAdvice;
108            private ChainableMethodAdvice _parentChainableMethodAdvice;
109    
110    }