package org.jboss.net.axis;
import org.jboss.axis.ConfigurationException;
import org.jboss.axis.EngineConfiguration;
import org.jboss.axis.EngineConfigurationFactory;
import org.jboss.axis.client.Call;
import org.jboss.axis.client.Service;
import org.jboss.axis.configuration.EngineConfigurationFactoryFinder;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class AxisInvocationHandler implements InvocationHandler, Serializable
{
static final long serialVersionUID = -8250523938712824229L;
protected Map methodToInterface;
protected Map methodToName;
transient protected Call call;
protected String rootContext;
protected String endPoint;
public AxisInvocationHandler(Call call, Map methodMap, Map interfaceMap)
{
this.call = call;
this.methodToInterface = interfaceMap;
this.methodToName = methodMap;
EngineConfiguration myEngineConfig = call.getService().getEngine().getConfig();
try
{
rootContext = (String)myEngineConfig.getGlobalOptions().get(Constants.CONFIGURATION_CONTEXT);
}
catch (ConfigurationException e)
{
}
catch (NullPointerException e)
{
}
if (call.getTargetEndpointAddress() != null)
{
endPoint = call.getTargetEndpointAddress().toString();
}
}
public AxisInvocationHandler(URL endpoint,
String soapAction,
Service service,
Map methodMap,
Map interfaceMap)
{
this(new Call(service), methodMap, interfaceMap);
call.setTargetEndpointAddress(endpoint);
call.setSOAPActionURI(soapAction);
setBasicAuthentication(endpoint);
endPoint = endpoint.toString();
}
public AxisInvocationHandler(URL endPoint,
String soapAction,
Map methodMap,
Map interfaceMap,
boolean maintainSession)
{
this(endPoint, soapAction, new Service(), methodMap, interfaceMap);
call.setMaintainSession(maintainSession);
}
public AxisInvocationHandler(URL endPoint, String soapAction, Map methodMap, Map interfaceMap)
{
this(endPoint, soapAction, methodMap, interfaceMap, true);
}
public AxisInvocationHandler(URL endPoint, String soapAction, Map methodMap)
{
this(endPoint, soapAction, methodMap, new DefaultInterfaceMap());
}
public AxisInvocationHandler(URL endPoint, String soapAction)
{
this(endPoint, soapAction, new DefaultMethodMap());
}
protected void setBasicAuthentication(URL target)
{
String userInfo = target.getUserInfo();
if (userInfo != null)
{
java.util.StringTokenizer tok = new java.util.StringTokenizer(userInfo, ":");
if (tok.hasMoreTokens())
{
call.setUsername(tok.nextToken());
if (tok.hasMoreTokens())
{
call.setPassword(tok.nextToken());
}
}
}
}
public Object invoke(String serviceName, String methodName, Object[] args)
throws java.rmi.RemoteException
{
try
{
return call.invoke(serviceName, methodName, args);
}
finally
{
call.setReturnType(null);
call.removeAllParameters();
}
}
public Object invoke(String serviceName,
String methodName,
Object[] args,
Class[] parameters)
throws java.rmi.RemoteException
{
return invoke(serviceName, methodName, args);
}
public java.lang.Object invoke(java.lang.Object target,
java.lang.reflect.Method method,
java.lang.Object[] args)
throws java.lang.Throwable
{
return invoke((String)methodToInterface.get(method),
(String)methodToName.get(method),
args,
method.getParameterTypes());
}
public static Object createAxisService(Class _interface, URL endpoint, String soapAction)
{
return createAxisService(_interface, new AxisInvocationHandler(endpoint, soapAction));
}
public static Object createAxisService(Class _interface,
URL endpoint, String soapAction, Service service)
{
return createAxisService(_interface,
new AxisInvocationHandler(endpoint, soapAction, service, new DefaultMethodMap(), new DefaultInterfaceMap()));
}
public static Object createAxisService(Class _interface,
Call call)
{
return createAxisService(_interface,
new AxisInvocationHandler(call, new DefaultMethodMap(), new DefaultInterfaceMap()));
}
public static Object createAxisService(Class _interface,
AxisInvocationHandler handler)
{
return Proxy.newProxyInstance(_interface.getClassLoader(),
new Class[]{_interface},
handler);
}
public static class DefaultInterfaceMap extends HashMap
{
public DefaultInterfaceMap()
{
super(0);
}
public Object get(Object key)
{
Object result = super.get(((Method)key).getName());
if (result == null)
{
result = super.get(((Method)key).getDeclaringClass().getName());
if (result == null)
{
String sresult = ((Method)key).getDeclaringClass().getName();
if (sresult.indexOf(".") != -1)
sresult = sresult.substring(sresult.lastIndexOf(".") + 1);
result = sresult;
}
}
return result;
}
public Object put(Object key, Object value)
{
if (key instanceof Method)
{
return super.put(((Method)key).getName(), value);
}
else if (key instanceof Class)
{
return super.put(((Class)key).getName(), value);
}
else
{
return super.put(key, value);
}
}
}
public static class DefaultMethodMap extends HashMap
{
public DefaultMethodMap()
{
super(0);
}
public Object get(Object key)
{
Object result = super.get(((Method)key).getName());
if (result == null)
{
result = ((Method)key).getName();
}
return result;
}
public Object put(Object key, Object value)
{
if (key instanceof Method)
{
return super.put(((Method)key).getName(), value);
}
else
{
return super.put(key, value);
}
}
}
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException
{
stream.defaultReadObject();
try
{
EngineConfigurationFactory factory = EngineConfigurationFactoryFinder.
newFactory(new ObjectName(rootContext));
EngineConfiguration engine = null;
if (factory != null)
{
engine = factory.getClientEngineConfig();
}
if (engine != null)
{
call = new Call(new Service(engine));
}
else
{
call = new Call(new Service());
}
}
catch (MalformedObjectNameException e)
{
throw new IOException("Could not contact jmx configuration factory." + e);
}
URL endpoint = new URL(endPoint);
call.setTargetEndpointAddress(endpoint);
setBasicAuthentication(endpoint);
}
}