View Javadoc

1   /**
2    * Copyright 2005-2014 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.serviceproxies;
17  
18  import org.kuali.rice.core.api.config.property.ConfigContext;
19  import org.kuali.rice.core.api.exception.RiceRuntimeException;
20  import org.kuali.rice.core.api.util.ClassLoaderUtils;
21  import org.kuali.rice.core.api.util.ContextClassLoaderProxy;
22  import org.kuali.rice.ksb.api.bus.Endpoint;
23  import org.kuali.rice.ksb.api.messaging.AsynchronousCallback;
24  import org.kuali.rice.ksb.messaging.MessageServiceInvoker;
25  import org.kuali.rice.ksb.messaging.PersistedMessageBO;
26  import org.kuali.rice.ksb.util.KSBConstants;
27  
28  import java.io.Serializable;
29  import java.lang.reflect.Proxy;
30  import java.util.List;
31  
32  
33  /**
34   * Used to Call a service synchronously but through the messaging code within workflow. Used to when switching generally
35   * asynchronously called services to synchronously called services. Generally for testing purposes.
36   * 
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   * 
39   */
40  public class SynchronousServiceCallProxy extends AsynchronousServiceCallProxy {
41  
42      private SynchronousServiceCallProxy(List<Endpoint> endpoints, AsynchronousCallback callback,
43  	    Serializable context, String value1, String value2) {
44  	super(endpoints, callback, context, value1, value2);
45      }
46  
47      public static Object createInstance(List<Endpoint> endpoints, AsynchronousCallback callback,
48  	    Serializable context, String value1, String value2) {
49  	if (endpoints == null || endpoints.isEmpty()) {
50  	    throw new RuntimeException("Cannot create service proxy, no service(s) passed in.");
51  	}
52  	try {
53  	return Proxy.newProxyInstance(ClassLoaderUtils.getDefaultClassLoader(), ContextClassLoaderProxy
54  		.getInterfacesToProxy(endpoints.get(0).getService()), new SynchronousServiceCallProxy(
55  				endpoints, callback, context, value1, value2));
56  	} catch (Exception e) {
57  	    throw new RiceRuntimeException(e);
58      }
59      }
60  
61      @Override
62      protected void executeMessage(PersistedMessageBO message) {
63  	if (!Boolean.valueOf(ConfigContext.getCurrentContextConfig().getProperty(KSBConstants.Config.MESSAGING_OFF))) {
64  	    new MessageServiceInvoker(message).run();
65  	}
66      }
67  }