View Javadoc

1   /*
2    * Copyright 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.student.common.spring;
17  
18  import java.io.Externalizable;
19  import java.io.IOException;
20  import java.io.ObjectInput;
21  import java.io.ObjectOutput;
22  import java.io.Serializable;
23  import java.lang.reflect.InvocationHandler;
24  import java.lang.reflect.Method;
25  
26  import javax.xml.namespace.QName;
27  
28  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
29  import org.springframework.util.ReflectionUtils;
30  
31  /**
32   * 
33   * A Serializable Proxy Invokation Handler.  This handler is serializable and knows how to look up the service using the GlobalResourceLocator.
34   * 
35   * @author Kuali Student Team
36   * 
37   */
38  public class SerializableProxyInvokationHandler implements InvocationHandler, Externalizable {
39  
40  	/**
41  	 * 
42  	 */
43  	private static final long serialVersionUID = 1L;
44  
45  	private transient Object serviceDelegate;
46  
47  	private QName serviceName;
48  
49  	/**
50  	 * 
51  	 */
52  	public SerializableProxyInvokationHandler() {
53  	}
54  
55  	
56  	/**
57  	 * @param serviceName the serviceName to set
58  	 */
59  	public void setServiceName(QName serviceName) {
60  		this.serviceName = serviceName;
61  	}
62  
63  
64  	/*
65  	 * (non-Javadoc)
66  	 * 
67  	 * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
68  	 * java.lang.reflect.Method, java.lang.Object[])
69  	 */
70  	@Override
71  	public Object invoke(Object proxy, Method method, Object[] args)
72  			throws Throwable {
73  
74  		if (serviceDelegate == null) {
75  			// first time or just after reserialization.
76  			try {
77  	            serviceDelegate = GlobalResourceLoader.getService(serviceName);
78              } catch (Exception e) {
79              	
80              	if (method.getName().equals("toString")) {
81              		// fake toString
82              		// spring has an assertion that expects this to work so we will just fake it
83              		// once the delegate is resolved the normal toString method will be used.
84              		return getClass().getName() + " (serviceName=" + serviceName + ")";
85              	}
86              	else
87              		throw e;
88              }
89  		}
90  
91  		// delegate the call to the actual service to fulfill.
92  		return ReflectionUtils.invokeMethod(method, serviceDelegate, args);
93  	}
94  
95  	/* (non-Javadoc)
96  	 * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput)
97  	 */
98  	@Override
99  	public void writeExternal(ObjectOutput out) throws IOException {
100 		
101 		QName name = new QName(serviceName.getNamespaceURI(), serviceName.getLocalPart());
102 		
103 		out.writeObject(name);
104 		
105 	}
106 
107 	/* (non-Javadoc)
108 	 * @see java.io.Externalizable#readExternal(java.io.ObjectInput)
109 	 */
110 	@Override
111 	public void readExternal(ObjectInput in) throws IOException,
112 			ClassNotFoundException {
113 		
114 		QName name = (QName) in.readObject();
115 	
116 		this.serviceName = new QName (name.getNamespaceURI(), name.getLocalPart());
117 		
118 	}
119 	
120 	
121 
122 }