1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.spring.aop;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.model.BaseModel;
28  import com.liferay.portal.model.ModelExtensionHandler;
29  
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  import org.aspectj.lang.ProceedingJoinPoint;
34  import org.aspectj.lang.Signature;
35  
36  /**
37   * <a href="ModelExtensionAdvice.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Raymond Augé
40   */
41  public class ModelExtensionAdvice<T> {
42  
43      public ModelExtensionHandler<T> getModelExtensionHandler() {
44          return _modelExtensionHandler;
45      }
46  
47      public Object invoke(ProceedingJoinPoint proceedingJoinPoint)
48          throws Throwable {
49  
50          Object result = proceedingJoinPoint.proceed();
51  
52          Signature signature = proceedingJoinPoint.getSignature();
53  
54          if (_extensionMethodNames.isEmpty() ||
55              _extensionMethodNames.contains(signature.getName())) {
56  
57              if (_log.isDebugEnabled()) {
58                  StringBuilder sb = new StringBuilder();
59  
60                  sb.append("Extending method ");
61                  sb.append(signature.getDeclaringTypeName());
62                  sb.append(".");
63                  sb.append(signature.getName());
64                  sb.append("(");
65  
66                  Object[] args = proceedingJoinPoint.getArgs();
67  
68                  for (int i = 0; i < args.length; i++) {
69                      if (i > 0) {
70                          sb.append(", ");
71                      }
72  
73                      sb.append(args[i].getClass().getSimpleName());
74                  }
75  
76                  sb.append(")");
77  
78                  _log.debug(sb.toString());
79              }
80  
81              if (result instanceof BaseModel) {
82                  result = _modelExtensionHandler.extendSingle(
83                      (BaseModel<T>)result);
84              }
85              else if (result instanceof List) {
86                  result = _modelExtensionHandler.extendList(
87                      (List<BaseModel<T>>)result);
88              }
89          }
90  
91          return result;
92      }
93  
94      public void setModelExtensionHandler(
95          ModelExtensionHandler<T> modelExtensionHandler) {
96  
97          _modelExtensionHandler = modelExtensionHandler;
98  
99          if (_modelExtensionHandler.getExtensionMethodNames() != null) {
100             _extensionMethodNames =
101                 _modelExtensionHandler.getExtensionMethodNames();
102         }
103         else {
104             _extensionMethodNames = new ArrayList<String>();
105         }
106     }
107 
108     private static final Log _log =
109         LogFactoryUtil.getLog(ModelExtensionAdvice.class);
110 
111     private List<String> _extensionMethodNames;
112     private ModelExtensionHandler<T> _modelExtensionHandler;
113 
114 }