View Javadoc

1   /*
2    * Copyright 2006-2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
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.springframework.batch.sample.jmx;
18  
19  import org.aspectj.lang.JoinPoint;
20  import org.springframework.batch.core.StepExecution;
21  import org.springframework.context.ApplicationEvent;
22  import org.springframework.context.ApplicationEventPublisher;
23  import org.springframework.context.ApplicationEventPublisherAware;
24  
25  /**
26   * Wraps calls for methods taking {@link StepExecution} as an argument and
27   * publishes notifications in the form of {@link ApplicationEvent}.
28   * 
29   * @author Dave Syer
30   */
31  public class StepExecutionApplicationEventAdvice implements ApplicationEventPublisherAware {
32  
33  	private ApplicationEventPublisher applicationEventPublisher;
34  
35  	/*
36  	 * (non-Javadoc)
37  	 * @see org.springframework.context.ApplicationEventPublisherAware#setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher)
38  	 */
39  	public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
40  		this.applicationEventPublisher = applicationEventPublisher;
41  	}
42  
43  	public void before(JoinPoint jp, StepExecution stepExecution) {
44  		String msg = "Before: " + jp.toShortString() + " with: " + stepExecution;
45  		publish(jp.getTarget(), msg);
46  	}
47  
48  	public void after(JoinPoint jp, StepExecution stepExecution) {
49  		String msg = "After: " + jp.toShortString() + " with: " + stepExecution;
50  		publish(jp.getTarget(), msg);
51  	}
52  
53  	public void onError(JoinPoint jp, StepExecution stepExecution, Throwable t) {
54  		String msg = "Error in: " + jp.toShortString() + " with: " + stepExecution + " (" + t.getClass() + ":" + t.getMessage() + ")";
55  		publish(jp.getTarget(), msg);
56  	}
57  
58  	/*
59  	 * Publish a {@link SimpleMessageApplicationEvent} with the given
60  	 * parameters.
61  	 */
62  	private void publish(Object source, String message) {
63  		applicationEventPublisher.publishEvent(new SimpleMessageApplicationEvent(source, message));
64  	}
65  
66  }