001    /**
002     * Copyright 2005-2013 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.serviceproxies;
017    
018    import org.kuali.rice.core.api.config.property.ConfigContext;
019    import org.kuali.rice.core.api.exception.RiceRuntimeException;
020    import org.kuali.rice.core.api.util.ClassLoaderUtils;
021    import org.kuali.rice.core.api.util.ContextClassLoaderProxy;
022    import org.kuali.rice.ksb.api.bus.Endpoint;
023    import org.kuali.rice.ksb.api.messaging.AsynchronousCallback;
024    import org.kuali.rice.ksb.messaging.MessageServiceInvoker;
025    import org.kuali.rice.ksb.messaging.PersistedMessageBO;
026    import org.kuali.rice.ksb.util.KSBConstants;
027    
028    import java.io.Serializable;
029    import java.lang.reflect.Proxy;
030    import java.util.List;
031    
032    
033    /**
034     * Used to Call a service synchronously but through the messaging code within workflow. Used to when switching generally
035     * asynchronously called services to synchronously called services. Generally for testing purposes.
036     * 
037     * @author Kuali Rice Team (rice.collab@kuali.org)
038     * 
039     */
040    public class SynchronousServiceCallProxy extends AsynchronousServiceCallProxy {
041    
042        private SynchronousServiceCallProxy(List<Endpoint> endpoints, AsynchronousCallback callback,
043                Serializable context, String value1, String value2) {
044            super(endpoints, callback, context, value1, value2);
045        }
046    
047        public static Object createInstance(List<Endpoint> endpoints, AsynchronousCallback callback,
048                Serializable context, String value1, String value2) {
049            if (endpoints == null || endpoints.isEmpty()) {
050                throw new RuntimeException("Cannot create service proxy, no service(s) passed in.");
051            }
052            try {
053            return Proxy.newProxyInstance(ClassLoaderUtils.getDefaultClassLoader(), ContextClassLoaderProxy
054                    .getInterfacesToProxy(endpoints.get(0).getService()), new SynchronousServiceCallProxy(
055                                    endpoints, callback, context, value1, value2));
056            } catch (Exception e) {
057                throw new RiceRuntimeException(e);
058        }
059        }
060    
061        @Override
062        protected void executeMessage(PersistedMessageBO message) {
063            if (!Boolean.valueOf(ConfigContext.getCurrentContextConfig().getProperty(KSBConstants.Config.MESSAGING_OFF))) {
064                new MessageServiceInvoker(message).run();
065            }
066        }
067    }