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   */
42  public class ModelExtensionAdvice<T> {
43  
44      public ModelExtensionHandler<T> getModelExtensionHandler() {
45          return _modelExtensionHandler;
46      }
47  
48      public Object invoke(ProceedingJoinPoint proceedingJoinPoint)
49          throws Throwable {
50  
51          Object result = proceedingJoinPoint.proceed();
52  
53          Signature signature = proceedingJoinPoint.getSignature();
54  
55          if (_extensionMethodNames.isEmpty() ||
56              _extensionMethodNames.contains(signature.getName())) {
57  
58              if (_log.isDebugEnabled()) {
59                  StringBuilder sb = new StringBuilder();
60  
61                  sb.append("Extending method ");
62                  sb.append(signature.getDeclaringTypeName());
63                  sb.append(".");
64                  sb.append(signature.getName());
65                  sb.append("(");
66  
67                  Object[] args = proceedingJoinPoint.getArgs();
68  
69                  for (int i = 0; i < args.length; i++) {
70                      if (i > 0) {
71                          sb.append(", ");
72                      }
73  
74                      sb.append(args[i].getClass().getSimpleName());
75                  }
76  
77                  sb.append(")");
78  
79                  _log.debug(sb.toString());
80              }
81  
82              if (result instanceof BaseModel) {
83                  result = _modelExtensionHandler.extendSingle(
84                      (BaseModel<T>)result);
85              }
86              else if (result instanceof List) {
87                  result = _modelExtensionHandler.extendList(
88                      (List<BaseModel<T>>)result);
89              }
90          }
91  
92          return result;
93      }
94  
95      public void setModelExtensionHandler(
96          ModelExtensionHandler<T> modelExtensionHandler) {
97  
98          _modelExtensionHandler = modelExtensionHandler;
99  
100         if (_modelExtensionHandler.getExtensionMethodNames() != null) {
101             _extensionMethodNames =
102                 _modelExtensionHandler.getExtensionMethodNames();
103         }
104         else {
105             _extensionMethodNames = new ArrayList<String>();
106         }
107     }
108 
109     private static final Log _log =
110         LogFactoryUtil.getLog(ModelExtensionAdvice.class);
111 
112     private List<String> _extensionMethodNames;
113     private ModelExtensionHandler<T> _modelExtensionHandler;
114 
115 }