View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    * http://www.opensource.org/licenses/ecl2.php
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.ksb.messaging.bam;
18  
19  import java.lang.reflect.Method;
20  import java.lang.reflect.Proxy;
21  
22  import org.kuali.rice.core.proxy.BaseTargetedInvocationHandler;
23  import org.kuali.rice.core.resourceloader.ContextClassLoaderProxy;
24  import org.kuali.rice.core.util.ExceptionUtils;
25  import org.kuali.rice.ksb.messaging.ServiceInfo;
26  import org.kuali.rice.ksb.messaging.bam.service.BAMService;
27  import org.kuali.rice.ksb.service.KSBServiceLocator;
28  
29  
30  /**
31   * A service-side proxy for that records an entry in the BAM for invocations
32   * on the proxied service endpoint.
33   *
34   * @see BAMService
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  
39  public class BAMServerProxy extends BaseTargetedInvocationHandler {
40  
41  	private ServiceInfo entry;
42  	
43  	private BAMServerProxy(Object target, ServiceInfo entry) {
44  		super(target);
45  		this.entry = entry;
46  	}
47  	
48  	public static Object wrap(Object target, ServiceInfo entry) {
49  		return Proxy.newProxyInstance(target.getClass().getClassLoader(), ContextClassLoaderProxy.getInterfacesToProxyIncludeSpring(target), new BAMServerProxy(target, entry));
50  	}
51  	
52  	
53  	protected Object invokeInternal(Object proxiedObject, Method method, Object[] arguments) throws Throwable {
54  		BAMTargetEntry bamTargetEntry = KSBServiceLocator.getBAMService().recordServerInvocation(getTarget(), this.entry, method, arguments);
55  		try {
56  			return method.invoke(getTarget(), arguments);	
57  		} catch (Throwable throwable) {
58  			throwable = ExceptionUtils.unwrapActualCause(throwable);
59  			KSBServiceLocator.getBAMService().recordServerInvocationError(throwable, bamTargetEntry);
60  			throw throwable;
61  		}
62  	}
63  }