Skip to content

Commit c7adb5f

Browse files
committed
added new aspects with dynamic pointcut
1 parent 42c2234 commit c7adb5f

File tree

4 files changed

+224
-36
lines changed

4 files changed

+224
-36
lines changed

src/main/java/org/audit4j/integration/spring/AuditAdvice.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,44 @@
2424
import org.audit4j.core.AuditManager;
2525
import org.springframework.aop.MethodBeforeAdvice;
2626

27-
2827
/**
2928
* The Class AuditAdvice.
3029
*
31-
* @author Janith Bandara
30+
* <p>
31+
* </p>
32+
* Usage:
33+
*
34+
* <pre>
35+
* {@code
36+
* <bean id="auditAdvice" class="org.audit4j.integration.spring.AuditAdvice" />
37+
*
38+
* <bean id="serviceClass" class="com.xyz.myapp.service.ServiceImpl">
39+
* <!-- properties here -->
40+
* </bean>
41+
*
42+
* <bean id="serviceImplProxy"
43+
* class="org.springframework.aop.framework.ProxyFactoryBean">
44+
* <property name="target" ref="serviceClass" />
45+
*
46+
* <property name="interceptorNames">
47+
* <list>
48+
* <value>auditAdvice</value>
49+
* </list>
50+
* </property>
51+
* </bean>
52+
* }
53+
* </pre>
54+
*
55+
* @author <a href="mailto:[email protected]">Janith Bandara</a>
3256
*/
3357
public class AuditAdvice implements MethodBeforeAdvice {
3458

35-
/* (non-Javadoc)
36-
* @see org.springframework.aop.MethodBeforeAdvice#before(java.lang.reflect.Method, java.lang.Object[], java.lang.Object)
59+
/*
60+
* (non-Javadoc)
61+
*
62+
* @see
63+
* org.springframework.aop.MethodBeforeAdvice#before(java.lang.reflect.Method
64+
* , java.lang.Object[], java.lang.Object)
3765
*/
3866
@Override
3967
public void before(final Method method, final Object[] arg1, final Object arg2) throws Throwable {
Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
package org.audit4j.integration.spring;
2+
13
/*
2-
* Copyright 2014 Janith Bandara, This source is a part of Audit4j -
3-
* An open-source audit platform for Enterprise java platform.
4-
* http://mechanizedspace.com/audit4j
4+
* Copyright (c) 2014-2015 Janith Bandara, This source is a part of
5+
* Audit4j - An open source auditing framework.
56
* http://audit4j.org
67
*
78
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,50 +18,48 @@
1718
* limitations under the License.
1819
*/
1920

20-
package org.audit4j.integration.spring;
21-
2221
import java.lang.reflect.Method;
2322

24-
import org.aspectj.lang.ProceedingJoinPoint;
25-
import org.aspectj.lang.annotation.Around;
26-
import org.aspectj.lang.annotation.Aspect;
27-
import org.aspectj.lang.annotation.Pointcut;
23+
import org.aspectj.lang.JoinPoint;
2824
import org.aspectj.lang.reflect.MethodSignature;
2925
import org.audit4j.core.AuditManager;
3026

31-
32-
3327
/**
3428
* The Class AuditAspect.
29+
* <p>
30+
* Configuration:
31+
* </p>
32+
*
33+
* <pre>
34+
* {@code
35+
* <aop:config>
36+
* <aop:aspect id="audit" ref="auditAspect">
37+
* <aop:pointcut id="packages"
38+
* expression="execution(* com.xyz.myapp.service.*.*(..))" />
39+
* <aop:before pointcut-ref="packages" method="audit" />
40+
* </aop:aspect>
41+
* </aop:config>
3542
*
36-
* @author Janith Bandara
43+
* <bean id="auditAspect" class="org.audit4j.integration.spring.AuditAspect"/>
44+
* }
45+
* </pre>
46+
*
47+
* @author <a href="mailto:[email protected]">Janith Bandara</a>
3748
*/
38-
@Aspect
3949
public class AuditAspect {
4050

4151
/**
42-
* Execute public method.
43-
*/
44-
@Pointcut(value = "execution(public * *(..))")
45-
public void executePublicMethod() {
46-
}
47-
48-
/**
49-
* Log action.
52+
* Audit.
5053
*
51-
* @param pjp the pjp
54+
* @param jointPoint the pjp
5255
* @return the object
5356
* @throws Throwable the throwable
5457
*/
55-
@Around("executePublicMethod()")
56-
public Object logAction(final ProceedingJoinPoint pjp) throws Throwable {
57-
AuditManager manager = AuditManager.getInstance();
58-
Class<?> clazz = pjp.getClass();
59-
60-
MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
61-
Method method = methodSignature.getMethod();
62-
63-
manager.audit(clazz, method, pjp.getArgs());
64-
return pjp.proceed();
58+
public void audit (final JoinPoint jointPoint) throws Throwable {
59+
AuditManager manager = AuditManager.getInstance();
60+
Class<?> clazz = jointPoint.getClass();
61+
MethodSignature methodSignature = (MethodSignature) jointPoint.getSignature();
62+
Method method = methodSignature.getMethod();
63+
manager.audit(clazz, method, jointPoint.getArgs());
6564
}
6665
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.audit4j.integration.spring;
2+
3+
import java.lang.reflect.Method;
4+
5+
import org.aspectj.lang.JoinPoint;
6+
import org.aspectj.lang.annotation.Aspect;
7+
import org.aspectj.lang.annotation.Before;
8+
import org.aspectj.lang.annotation.Pointcut;
9+
import org.aspectj.lang.reflect.MethodSignature;
10+
import org.audit4j.core.AuditManager;
11+
12+
@Aspect
13+
public class AuditAspect2 {
14+
15+
/**
16+
* Execute public method.
17+
*/
18+
@Pointcut("execution(@org.audit4j.core.annotation.Audit * *(..))")
19+
public void executeAuditMethod() {
20+
}
21+
22+
@Pointcut("within(@org.audit4j.core.annotation.Audit *)")
23+
public void executeAuditClass() {
24+
}
25+
26+
/**
27+
* Log action.
28+
*
29+
* @param pjp
30+
* the pjp
31+
* @return the object
32+
* @throws Throwable
33+
* the throwable
34+
*/
35+
@Before("executeAuditMethod() || executeAuditClass()")
36+
public void logAction(final JoinPoint pjp) throws Throwable {
37+
MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
38+
Method method = methodSignature.getMethod();
39+
AuditManager.getInstance().audit(pjp.getClass(), method, pjp.getArgs());
40+
}
41+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright 2014 Janith Bandara, This source is a part of
3+
* Audit4j - An open source auditing framework.
4+
* http://audit4j.org
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.audit4j.integration.spring;
20+
21+
import java.util.List;
22+
23+
import org.audit4j.core.AuditManager;
24+
import org.audit4j.core.Configuration;
25+
import org.audit4j.core.MetaData;
26+
import org.audit4j.core.filter.AuditEventFilter;
27+
import org.audit4j.core.handler.Handler;
28+
import org.audit4j.core.layout.Layout;
29+
import org.springframework.beans.factory.DisposableBean;
30+
import org.springframework.beans.factory.InitializingBean;
31+
32+
/**
33+
* The Class SpringAudit4jConfig.
34+
*
35+
* @author <a href="mailto:[email protected]">Janith Bandara</a>
36+
*/
37+
public class SpringAudit4jConfig implements InitializingBean, DisposableBean {
38+
39+
/** The layout. */
40+
private Layout layout;
41+
42+
/** The handlers. */
43+
private List<Handler> handlers;
44+
45+
/** The meta data. */
46+
private MetaData metaData;
47+
48+
/** The filters. */
49+
private List<AuditEventFilter> filters;
50+
51+
/** The options. */
52+
private String options;
53+
54+
/* (non-Javadoc)
55+
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
56+
*/
57+
@Override
58+
public void afterPropertiesSet() throws Exception {
59+
Configuration configuration = new Configuration();
60+
configuration.setLayout(layout);
61+
configuration.setHandlers(handlers);
62+
configuration.setMetaData(metaData);
63+
configuration.setFilters(filters);
64+
configuration.setOptions(options);
65+
AuditManager.getConfigurationInstance(configuration);
66+
}
67+
68+
/* (non-Javadoc)
69+
* @see org.springframework.beans.factory.DisposableBean#destroy()
70+
*/
71+
@Override
72+
public void destroy() throws Exception {
73+
AuditManager.getInstance().shutdown();
74+
}
75+
76+
/**
77+
* Sets the layout.
78+
*
79+
* @param layout the new layout
80+
*/
81+
public void setLayout(Layout layout) {
82+
this.layout = layout;
83+
}
84+
85+
/**
86+
* Sets the meta data.
87+
*
88+
* @param metaData the new meta data
89+
*/
90+
public void setMetaData(MetaData metaData) {
91+
this.metaData = metaData;
92+
}
93+
94+
/**
95+
* Sets the handlers.
96+
*
97+
* @param handlers the new handlers
98+
*/
99+
public void setHandlers(List<Handler> handlers) {
100+
this.handlers = handlers;
101+
}
102+
103+
/**
104+
* Sets the filters.
105+
*
106+
* @param filters the new filters
107+
*/
108+
public void setFilters(List<AuditEventFilter> filters) {
109+
this.filters = filters;
110+
}
111+
112+
/**
113+
* Sets the options.
114+
*
115+
* @param options the new options
116+
*/
117+
public void setOptions(String options) {
118+
this.options = options;
119+
}
120+
}

0 commit comments

Comments
 (0)