001
014
015 package com.liferay.portal.search;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.model.BaseModel;
020 import com.liferay.portal.kernel.search.IndexWriterHelperUtil;
021 import com.liferay.portal.kernel.search.Indexable;
022 import com.liferay.portal.kernel.search.IndexableType;
023 import com.liferay.portal.kernel.search.Indexer;
024 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
025 import com.liferay.portal.kernel.security.auth.CompanyThreadLocal;
026 import com.liferay.portal.kernel.service.ServiceContext;
027 import com.liferay.portal.spring.aop.AnnotationChainableMethodAdvice;
028
029 import java.lang.annotation.Annotation;
030 import java.lang.reflect.Method;
031
032 import org.aopalliance.intercept.MethodInvocation;
033
034
037 public class IndexableAdvice
038 extends AnnotationChainableMethodAdvice<Indexable> {
039
040 @Override
041 public void afterReturning(MethodInvocation methodInvocation, Object result)
042 throws Throwable {
043
044 if (result == null) {
045 return;
046 }
047
048 if (IndexWriterHelperUtil.isIndexReadOnly() ||
049 CompanyThreadLocal.isDeleteInProcess()) {
050
051 return;
052 }
053
054 Indexable indexable = findAnnotation(methodInvocation);
055
056 if (indexable == _nullIndexable) {
057 return;
058 }
059
060 Method method = methodInvocation.getMethod();
061
062 Class<?> returnType = method.getReturnType();
063
064 if (!BaseModel.class.isAssignableFrom(returnType)) {
065 if (_log.isWarnEnabled()) {
066 _log.warn(
067 methodInvocation + " does not have a valid return type");
068 }
069
070 return;
071 }
072
073 Indexer<Object> indexer = IndexerRegistryUtil.getIndexer(
074 returnType.getName());
075
076 if (indexer == null) {
077 serviceBeanAopCacheManager.removeMethodInterceptor(
078 methodInvocation, this);
079
080 return;
081 }
082
083 Object[] arguments = methodInvocation.getArguments();
084
085 for (int i = arguments.length - 1; i >= 0; i--) {
086 if (arguments[i] instanceof ServiceContext) {
087 ServiceContext serviceContext = (ServiceContext)arguments[i];
088
089 if (serviceContext.isIndexingEnabled()) {
090 break;
091 }
092
093 return;
094 }
095 }
096
097 if (indexable.type() == IndexableType.DELETE) {
098 indexer.delete(result);
099 }
100 else {
101 indexer.reindex(result);
102 }
103 }
104
105 @Override
106 public Indexable getNullAnnotation() {
107 return _nullIndexable;
108 }
109
110 private static final Log _log = LogFactoryUtil.getLog(
111 IndexableAdvice.class);
112
113 private static final Indexable _nullIndexable = new Indexable() {
114
115 @Override
116 public Class<? extends Annotation> annotationType() {
117 return Indexable.class;
118 }
119
120 @Override
121 public IndexableType type() {
122 return null;
123 }
124
125 };
126
127 }