01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.compass.spring.aop;
18:
19: import org.compass.core.Compass;
20: import org.compass.core.CompassTemplate;
21: import org.springframework.beans.factory.InitializingBean;
22:
23: /**
24: * A base class for all Compass Aop interceptors. Holds a reference to a
25: * <code>Compass</code> instance.
26: * <p>
27: * It also holds information regarding the "location" of the data object (the
28: * one that will be saved/created/deleted). It can be the return value if the
29: * flag <code>useReturnValue</code> is set (it is not set bt default), or one
30: * of the method parameters (defaults to the first one - <code>0</code>).
31: *
32: * @author kimchy
33: *
34: */
35: public abstract class AbstractCompassInterceptor implements
36: InitializingBean {
37:
38: private Compass compass;
39:
40: protected CompassTemplate compassTemplate;
41:
42: private int parameterIndex = 0;
43:
44: private boolean useReturnValue = false;
45:
46: public void afterPropertiesSet() throws Exception {
47: if (compass == null) {
48: throw new IllegalArgumentException(
49: "compass property is required");
50: }
51: compassTemplate = new CompassTemplate(compass);
52: }
53:
54: /**
55: * A helper method that based on the configuration, returns the actual data
56: * object.
57: */
58: protected Object findObject(Object returnValue, Object[] args) {
59: if (useReturnValue) {
60: return returnValue;
61: }
62: if (parameterIndex >= args.length) {
63: throw new IllegalArgumentException("Set parameter index ["
64: + parameterIndex + "] for a method with ["
65: + args.length + "] arguments");
66: }
67: return args[parameterIndex];
68: }
69:
70: public Compass getCompass() {
71: return compass;
72: }
73:
74: public void setCompass(Compass compass) {
75: this .compass = compass;
76: }
77:
78: public int getParameterIndex() {
79: return parameterIndex;
80: }
81:
82: public void setParameterIndex(int parameterIndex) {
83: this .parameterIndex = parameterIndex;
84: }
85:
86: public boolean isUseReturnValue() {
87: return useReturnValue;
88: }
89:
90: public void setUseReturnValue(boolean useReturnValue) {
91: this.useReturnValue = useReturnValue;
92: }
93: }
|