package org.jboss.invocation;
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ByteArrayInputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.Principal;
import java.util.Map;
import java.util.HashMap;
import javax.transaction.Transaction;
public class Invocation
{
public static final String[] INVOKE_SIGNATURE = { "org.jboss.invocation.Invocation" };
public Map transient_payload;
public Map as_is_payload;
public Map payload;
public InvocationContext invocationContext;
public Object[] args;
public Object objectName;
public Method method;
public InvocationType invocationType;
public Invocation()
{
}
public Invocation( Object id, Method m, Object[] args, Transaction tx,
Principal identity, Object credential )
{
setId(id);
setMethod(m);
setArguments(args);
setTransaction(tx);
setPrincipal(identity);
setCredential(credential);
}
public void setValue(Object key, Object value)
{
setValue(key, value, PayloadKey.PAYLOAD);
}
public void setValue(Object key, Object value, PayloadKey type)
{
if(type == PayloadKey.TRANSIENT)
{
getTransientPayload().put(key,value);
}
else if(type == PayloadKey.AS_IS)
{
getAsIsPayload().put(key,value);
}
else if(type == PayloadKey.PAYLOAD)
{
getPayload().put(key,value);
}
else
{
throw new IllegalArgumentException("Unknown PayloadKey: " + type);
}
}
public Object getValue(Object key)
{
Object rtn = getPayloadValue(key);
if (rtn != null) return rtn;
rtn = getAsIsValue(key);
if (rtn != null) return rtn;
rtn = getTransientValue(key);
return rtn;
}
public Object getPayloadValue(Object key)
{
if (payload == null) return null;
return payload.get(key);
}
public Object getTransientValue(Object key)
{
if (transient_payload == null) return null;
return transient_payload.get(key);
}
public Object getAsIsValue(Object key)
{
if (as_is_payload == null) return null;
return as_is_payload.get(key);
}
public void setTransaction(Transaction tx)
{
if( tx instanceof Serializable )
getAsIsPayload().put(InvocationKey.TRANSACTION, tx);
else
getTransientPayload().put(InvocationKey.TRANSACTION, tx);
}
public Transaction getTransaction()
{
Transaction tx = (Transaction) getAsIsPayload().get(InvocationKey.TRANSACTION);
if( tx == null )
tx = (Transaction) getTransientPayload().get(InvocationKey.TRANSACTION);
return tx;
}
public void setPrincipal(Principal principal)
{
getAsIsPayload().put(InvocationKey.PRINCIPAL, principal);
}
public Principal getPrincipal()
{
return (Principal) getAsIsPayload().get(InvocationKey.PRINCIPAL);
}
public void setCredential(Object credential)
{
getPayload().put(InvocationKey.CREDENTIAL, credential);
}
public Object getCredential()
{
return getPayloadValue(InvocationKey.CREDENTIAL);
}
public void setObjectName(Object objectName)
{
this.objectName = objectName;
}
public Object getObjectName()
{
return objectName;
}
public void setType(InvocationType type)
{
invocationType = type;
}
public InvocationType getType()
{
if (invocationType == null) return InvocationType.LOCAL;
return invocationType;
}
public void setId(Object id)
{
getPayload().put(InvocationKey.CACHE_ID, id);
}
public Object getId()
{
return getPayloadValue(InvocationKey.CACHE_ID);
}
public void setMethod(Method method)
{
this.method = method;
}
public Method getMethod()
{
return method;
}
public void setArguments(Object[] arguments)
{
this.args = arguments;
}
public Object[] getArguments()
{
return this.args;
}
public InvocationContext getInvocationContext()
{
return invocationContext;
}
public void setInvocationContext(InvocationContext ctx)
{
this.invocationContext = ctx;
}
public void setEnterpriseContext(Object ctx)
{
getTransientPayload().put(InvocationKey.ENTERPRISE_CONTEXT, ctx);
}
public Object getEnterpriseContext()
{
return getTransientPayload().get(InvocationKey.ENTERPRISE_CONTEXT);
}
public Map getTransientPayload()
{
if (transient_payload == null) transient_payload = new HashMap();
return transient_payload;
}
public Map getAsIsPayload()
{
if (as_is_payload == null) as_is_payload = new HashMap();
return as_is_payload;
}
public Map getPayload()
{
if (payload == null) payload = new HashMap();
return payload;
}
public Object performCall(Object instance, Method m, Object[] arguments)
throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, Exception
{
return m.invoke(instance,arguments);
}
public boolean isLocal()
{
InvocationType type = getType();
return (type == InvocationType.LOCAL || type == InvocationType.LOCALHOME);
}
}