001    /**
002     * Copyright 2005-2012 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     */
016    package org.kuali.rice.ksb.messaging.bam;
017    
018    import org.kuali.rice.core.api.config.property.Config;
019    import org.kuali.rice.core.api.config.property.ConfigContext;
020    import org.kuali.rice.core.api.util.ClassLoaderUtils;
021    import org.kuali.rice.core.api.util.ContextClassLoaderProxy;
022    import org.kuali.rice.core.api.util.reflect.BaseTargetedInvocationHandler;
023    import org.kuali.rice.ksb.api.bus.ServiceConfiguration;
024    import org.kuali.rice.ksb.messaging.bam.service.BAMService;
025    import org.kuali.rice.ksb.service.KSBServiceLocator;
026    
027    import java.lang.reflect.InvocationTargetException;
028    import java.lang.reflect.Method;
029    import java.lang.reflect.Proxy;
030    
031    
032    /**
033     * A client-side proxy for that records an entry in the BAM for invocations
034     * on the proxied service.
035     *
036     * @see BAMService
037     *
038     * @author Kuali Rice Team (rice.collab@kuali.org)
039     */
040    public class BAMClientProxy extends BaseTargetedInvocationHandler {
041    
042            private ServiceConfiguration serviceConfiguration;
043            
044            private BAMClientProxy(Object target, ServiceConfiguration serviceConfiguration) {
045                    super(target);
046                    this.serviceConfiguration = serviceConfiguration;
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, ServiceConfiguration serviceConfiguration) {
054                    if (!isBamSupported()) {
055                            return target;
056                    }
057                    return Proxy.newProxyInstance(ClassLoaderUtils.getDefaultClassLoader(), ContextClassLoaderProxy.getInterfacesToProxy(target), new BAMClientProxy(target, serviceConfiguration));
058            }
059            
060            protected Object invokeInternal(Object proxyObject, Method method, Object[] arguments) throws Throwable {
061                    BAMTargetEntry bamTargetEntry = KSBServiceLocator.getBAMService().recordClientInvocation(this.serviceConfiguration, getTarget(), method, arguments);
062                    try {
063                            return method.invoke(getTarget(), arguments);   
064                    } catch (Throwable throwable) {
065                            if (throwable instanceof InvocationTargetException) {
066                                    throwable = throwable.getCause();
067                            }
068                            KSBServiceLocator.getBAMService().recordClientInvocationError(throwable, bamTargetEntry);
069                            throw throwable;
070                    }
071            }
072    }