View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  package org.kuali.rice.ksb.messaging.bam;
17  
18  import org.kuali.rice.core.api.config.property.Config;
19  import org.kuali.rice.core.api.config.property.ConfigContext;
20  import org.kuali.rice.core.api.util.reflect.BaseTargetedInvocationHandler;
21  import org.kuali.rice.core.impl.resourceloader.ContextClassLoaderProxy;
22  import org.kuali.rice.ksb.api.bus.ServiceDefinition;
23  import org.kuali.rice.ksb.messaging.bam.service.BAMService;
24  import org.kuali.rice.ksb.service.KSBServiceLocator;
25  
26  import java.lang.reflect.InvocationTargetException;
27  import java.lang.reflect.Method;
28  import java.lang.reflect.Proxy;
29  
30  
31  /**
32   * A service-side proxy for that records an entry in the BAM for invocations
33   * on the proxied service endpoint.
34   *
35   * @see BAMService
36   *
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   */
39  
40  public class BAMServerProxy extends BaseTargetedInvocationHandler {
41  
42  	private ServiceDefinition serviceDefinition;
43  	
44  	private BAMServerProxy(Object target, ServiceDefinition serviceDefinition) {
45  		super(target);
46  		this.serviceDefinition = serviceDefinition;
47  	}
48  	
49  	public static boolean isBamSupported() {
50  		return KSBServiceLocator.getBAMService() != null && Boolean.valueOf(ConfigContext.getCurrentContextConfig().getProperty(Config.BAM_ENABLED));
51  	}
52  	
53  	public static Object wrap(Object target, ServiceDefinition serviceDefinition) {
54  		if (!isBamSupported()) {
55  			return target;
56  		}
57  		return Proxy.newProxyInstance(target.getClass().getClassLoader(), ContextClassLoaderProxy.getInterfacesToProxy(target), new BAMServerProxy(target, serviceDefinition));
58  	}
59  	
60  	protected Object invokeInternal(Object proxiedObject, Method method, Object[] arguments) throws Throwable {
61  		BAMTargetEntry bamTargetEntry = KSBServiceLocator.getBAMService().recordServerInvocation(getTarget(), this.serviceDefinition, method, arguments);
62  		try {
63  			return method.invoke(getTarget(), arguments);	
64  		} catch (Throwable throwable) {
65  			if (throwable instanceof InvocationTargetException) {
66  				throwable = throwable.getCause();
67  			}
68  			KSBServiceLocator.getBAMService().recordServerInvocationError(throwable, bamTargetEntry);
69  			throw throwable;
70  		}
71  	}
72  }