001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.ksb.messaging.bam;
017
018import org.kuali.rice.core.api.config.property.Config;
019import org.kuali.rice.core.api.config.property.ConfigContext;
020import org.kuali.rice.core.api.util.ContextClassLoaderProxy;
021import org.kuali.rice.core.api.util.reflect.BaseTargetedInvocationHandler;
022import org.kuali.rice.ksb.api.bus.ServiceDefinition;
023import org.kuali.rice.ksb.messaging.bam.service.BAMService;
024import org.kuali.rice.ksb.service.KSBServiceLocator;
025
026import java.lang.reflect.InvocationTargetException;
027import java.lang.reflect.Method;
028import java.lang.reflect.Proxy;
029
030
031/**
032 * A service-side proxy for that records an entry in the BAM for invocations
033 * on the proxied service endpoint.
034 *
035 * @see BAMService
036 *
037 * @author Kuali Rice Team (rice.collab@kuali.org)
038 */
039
040public class BAMServerProxy extends BaseTargetedInvocationHandler<Object> {
041
042        private ServiceDefinition serviceDefinition;
043        
044        private BAMServerProxy(Object target, ServiceDefinition serviceDefinition) {
045                super(target);
046                this.serviceDefinition = serviceDefinition;
047        }
048        
049        public static boolean isBamSupported() {
050                return KSBServiceLocator.getBAMService() != null && Boolean.valueOf(ConfigContext.getCurrentContextConfig().getProperty(Config.BAM_ENABLED));
051        }
052        
053        public static Object wrap(Object target, ServiceDefinition serviceDefinition) {
054                if (!isBamSupported()) {
055                        return target;
056                }
057                return Proxy.newProxyInstance(target.getClass().getClassLoader(), ContextClassLoaderProxy.getInterfacesToProxy(
058                target), new BAMServerProxy(target, serviceDefinition));
059        }
060        
061        protected Object invokeInternal(Object proxiedObject, Method method, Object[] arguments) throws Throwable {
062                BAMTargetEntry bamTargetEntry = KSBServiceLocator.getBAMService().recordServerInvocation(getTarget(), this.serviceDefinition, method, arguments);
063                try {
064                        return method.invoke(getTarget(), arguments);   
065                } catch (Throwable throwable) {
066                        if (throwable instanceof InvocationTargetException) {
067                                throwable = throwable.getCause();
068                        }
069                        KSBServiceLocator.getBAMService().recordServerInvocationError(throwable, bamTargetEntry);
070                        throw throwable;
071                }
072        }
073}