package org.jfox.ejb.invoker.soap;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.Vector;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.xmlrpc.XmlRpcClientLite;
import org.apache.xmlrpc.XmlRpcHandler;
import org.jfox.ejb.Invocation;
import org.jfox.ejb.invoker.ContainerRemote;
public class SOAPContainerServiceBinder implements ContainerRemote, Serializable {
private transient XmlRpcHandler client;
private String soapURL;
public SOAPContainerServiceBinder(String soapUrl) {
this.soapURL = soapUrl;
try {
client = new XmlRpcClientLite(soapUrl);
}
catch (Exception e) {
e.printStackTrace();
}
}
public Object invoke(Invocation invocation) throws RemoteException {
Vector params = new Vector(1);
params.add(marshelledObject(invocation));
try {
return client.execute("xmlrpc", params);
}
catch (Exception e) {
throw new RemoteException(e.getMessage(), e);
}
}
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
client = new XmlRpcClientLite(soapURL);
}
public void ping() throws RemoteException {
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection)new URL(soapURL).openConnection();
}
catch(Exception e){
throw new RemoteException("can't get connection with the soap url: "+ soapURL, e);
}
finally {
if(conn != null) {
try{
conn.disconnect();
}
catch(Exception e){}
}
}
}
public static byte[] marshelledObject(Serializable obj) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(obj);
oos.flush();
}
catch (IOException e) {
e.printStackTrace();
}
return out.toByteArray();
}
public static void main(String[] args) {
}
}