View Javadoc

1   /**
2    * Copyright 2005-2012 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;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
21  
22  import javax.xml.namespace.QName;
23  import java.lang.reflect.InvocationHandler;
24  import java.lang.reflect.Method;
25  import java.lang.reflect.Proxy;
26  
27  /**
28   * This class creates a proxy for services deployed on KSB. A 
29   * reference to the service is obtained only upon the first method
30   * invocation.
31   * 
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   *
34   */
35  public class KSBClientProxy implements InvocationHandler {
36  
37  private static final Logger LOG = Logger.getLogger(KSBClientProxy.class);
38      
39      private QName serviceName;
40      private volatile Object service;
41  
42  
43      public static <T> T newInstance(String serviceQName, Class<T> interfaceClass) throws InstantiationException, IllegalAccessException {
44          if (StringUtils.isBlank(serviceQName)) {
45              throw new IllegalArgumentException("the qname was blank");
46          }
47  
48          if (interfaceClass == null) {
49              throw new IllegalArgumentException("the interfaceClass was null");
50          }
51  
52          @SuppressWarnings("unchecked")
53          final T t = (T)Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class[] { interfaceClass }, new KSBClientProxy(serviceQName));
54          return t;
55      }
56  
57      public KSBClientProxy(String serviceQName){
58          if (StringUtils.isBlank(serviceQName)) {
59              throw new IllegalArgumentException("the qname was blank");
60          }
61  
62          this.serviceName = QName.valueOf(serviceQName);
63      }
64      
65      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
66          //using DCL idiom
67          //see effective java 2nd ed. pg. 71
68          Object s = service;
69          if (s == null) {
70              synchronized (this) {
71                  s = service;
72                  if (s == null) {
73                      service = s = GlobalResourceLoader.getService(serviceName);
74                  }
75              }
76          }
77  
78          if (s != null) {
79              return method.invoke(s, args);
80          }
81  
82          LOG.warn("serviceName: " + serviceName + " was not found");
83          return null;
84      }
85  }