package org.jboss.metadata;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import java.util.HashSet;
import org.w3c.dom.Element;
import org.jboss.deployment.DeploymentException;
import org.jboss.invocation.InvocationType;
public class MethodMetaData extends MetaData
{
public static final int ANY_METHOD = -1;
public static String HOME_TYPE = "Home";
public static String LOCAL_HOME_TYPE = "LocalHome";
public static String REMOTE_TYPE = "Remote";
public static String LOCAL_TYPE = "Local";
public static String SERVICE_ENDPOINT_TYPE = "ServiceEndpoint";
private static final ArrayList EMPTY_PARAM_LIST = new ArrayList();
private String methodName;
private String ejbName;
private boolean intf = false;
private InvocationType methodType = null;
private boolean param = false;
private boolean unchecked = false;
private boolean excluded = false;
private ArrayList paramList = EMPTY_PARAM_LIST;
private byte transactionType;
private Set roles = new HashSet();
public MethodMetaData()
{
}
public String getMethodName()
{
return methodName;
}
public String getEjbName()
{
return ejbName;
}
public boolean isHomeMethod()
{
return methodType == InvocationType.HOME;
}
public boolean isRemoteMethod()
{
return methodType == InvocationType.REMOTE;
}
public boolean isLocalHomeMethod()
{
return methodType == InvocationType.LOCALHOME;
}
public boolean isLocalMethod()
{
return methodType == InvocationType.LOCAL;
}
public boolean isServiceEndpointMethod()
{
return methodType == InvocationType.SERVICE_ENDPOINT;
}
public String getInterfaceType()
{
String type = null;
if( isHomeMethod() )
type = HOME_TYPE;
if( isLocalHomeMethod() )
type = LOCAL_HOME_TYPE;
if( isRemoteMethod() )
type = REMOTE_TYPE;
if( isLocalMethod() )
type = LOCAL_TYPE;
if( isServiceEndpointMethod() )
type = SERVICE_ENDPOINT_TYPE;
return type;
}
public boolean isUnchecked()
{
return unchecked;
}
public boolean isExcluded()
{
return excluded;
}
public boolean isIntfGiven()
{
return intf;
}
public boolean isParamGiven()
{
return param;
}
public Iterator getParams()
{
return paramList.iterator();
}
public String[] getMethodParams()
{
String[] params = new String[paramList.size()];
paramList.toArray(params);
return params;
}
public byte getTransactionType()
{
return transactionType;
}
public void setTransactionType(byte type)
{
transactionType = type;
}
public Set getRoles()
{
return roles;
}
public void setRoles(Set perm)
{
roles = perm;
}
public void setUnchecked()
{
unchecked = true;
}
public void setExcluded()
{
excluded = true;
}
public boolean patternMatches(String name, Class[] arg, InvocationType iface)
{
return patternMatches(name, getClassNames(arg), iface);
}
public boolean patternMatches(String name, String[] arg, InvocationType iface)
{
if (getMethodName().equals("*"))
{
if (methodType != null && methodType != iface)
return false;
return true;
}
if (getMethodName().equals(name) == false)
{
return false;
}
else
{
if (methodType != null && methodType != iface)
return false;
if (isParamGiven() == false)
{
return true;
}
else
{
return sameParams(arg);
}
}
}
public void importEjbJarXml(Element element) throws DeploymentException
{
methodName = getElementContent(getUniqueChild(element, "method-name"));
ejbName = getElementContent(getUniqueChild(element, "ejb-name"));
Element intfElement = getOptionalChild(element, "method-intf");
if (intfElement != null)
{
intf = true;
String methodIntf = getElementContent(intfElement);
if (methodIntf.equals("Home"))
{
methodType = InvocationType.HOME;
}
else if (methodIntf.equals("Remote"))
{
methodType = InvocationType.REMOTE;
}
else if (methodIntf.equals("LocalHome"))
{
methodType = InvocationType.LOCALHOME;
}
else if (methodIntf.equals("Local"))
{
methodType = InvocationType.LOCAL;
}
else if (methodIntf.equals("ServiceEndpoint"))
{
methodType = InvocationType.SERVICE_ENDPOINT;
}
else
{
throw new DeploymentException("method-intf tag should be one of: 'Home', 'Remote', 'LocalHome', 'Local', 'ServiceEndpoint'");
}
}
Element paramsElement = getOptionalChild(element, "method-params");
if (paramsElement != null)
{
param = true;
paramList = new ArrayList();
Iterator paramsIterator = getChildrenByTagName(paramsElement, "method-param");
while (paramsIterator.hasNext())
{
paramList.add(getElementContent((Element) paramsIterator.next()));
}
}
}
private static String[] getClassNames(Class[] source)
{
String out[] = new String[source.length];
for (int i = 0; i < out.length; i++)
{
String brackets = "";
Class cls = source[i];
while (cls.isArray())
{
brackets += "[]";
cls = cls.getComponentType();
}
out[i] = cls.getName() + brackets;
}
return out;
}
private boolean sameParams(String[] arg)
{
if (arg.length != paramList.size()) return false;
for (int i = 0; i < arg.length; i++)
if (!arg[i].equals(paramList.get(i)))
return false;
return true;
}
}