package org.jboss.axis.deployment.wsdd;
import org.jboss.axis.ConfigurationException;
import org.jboss.axis.EngineConfiguration;
import org.jboss.axis.Handler;
import org.jboss.axis.encoding.SerializationContext;
import org.jboss.axis.providers.java.JavaProvider;
import org.jboss.axis.utils.ClassUtils;
import org.jboss.axis.utils.JavaUtils;
import org.jboss.axis.utils.LockableHashtable;
import org.jboss.axis.utils.XMLUtils;
import org.jboss.logging.Logger;
import org.w3c.dom.Element;
import org.xml.sax.helpers.AttributesImpl;
import javax.xml.namespace.QName;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public abstract class WSDDDeployableItem
extends WSDDElement
{
public static final int SCOPE_PER_ACCESS = 0;
public static final int SCOPE_PER_REQUEST = 1;
public static final int SCOPE_SINGLETON = 2;
public static String[] scopeStrings = {"per-access",
"per-request",
"singleton"};
private static Logger log = Logger.getLogger(WSDDDeployableItem.class.getName());
protected LockableHashtable parameters;
protected QName qname;
protected QName type;
protected int scope = SCOPE_SINGLETON;
protected Handler singletonInstance = null;
public WSDDDeployableItem()
{
}
public WSDDDeployableItem(Element e)
throws WSDDException
{
super(e);
String name = e.getAttribute(ATTR_NAME);
if (name != null && !name.equals(""))
{
qname = new QName("", name);
}
String typeStr = e.getAttribute(ATTR_TYPE);
if (typeStr != null && !typeStr.equals(""))
{
type = XMLUtils.getQNameFromString(typeStr, e);
}
String scopeStr = e.getAttribute(JavaProvider.OPTION_SCOPE);
if (scopeStr != null)
{
for (int i = 0; i < scopeStrings.length; i++)
{
if (scopeStr.equals(scopeStrings[i]))
{
scope = i;
break;
}
}
}
if (parameters == null)
parameters = new LockableHashtable();
Element[] paramElements = getChildElements(e, ELEM_WSDD_PARAM);
for (int i = 0; i < paramElements.length; i++)
{
Element param = paramElements[i];
String pname = param.getAttribute(ATTR_NAME);
String value = param.getAttribute(ATTR_VALUE);
String locked = param.getAttribute(ATTR_LOCKED);
parameters.put(pname, value, JavaUtils.isTrueExplicitly(locked));
}
}
public void setName(String name)
{
qname = new QName(null, name);
}
public void setQName(QName qname)
{
this.qname = qname;
}
public QName getQName()
{
return qname;
}
public QName getType()
{
return type;
}
public void setType(QName type)
{
this.type = type;
}
public void setParameter(String name, String value)
{
if (parameters == null)
parameters = new LockableHashtable();
parameters.put(name, value);
}
public String getParameter(String name)
{
if (name == null)
return null;
return (String)parameters.get(name);
}
public LockableHashtable getParametersTable()
{
return parameters;
}
public void setOptionsHashtable(Hashtable hashtable)
{
if (hashtable == null)
return;
parameters = new LockableHashtable(hashtable);
}
public void writeParamsToContext(SerializationContext context)
throws IOException
{
if (parameters == null)
return;
Set entries = parameters.entrySet();
Iterator i = entries.iterator();
while (i.hasNext())
{
Map.Entry entry = (Map.Entry)i.next();
String name = (String)entry.getKey();
AttributesImpl attrs = new AttributesImpl();
attrs.addAttribute("", ATTR_NAME, ATTR_NAME, "CDATA", name);
attrs.addAttribute("", ATTR_VALUE, ATTR_VALUE, "CDATA",
entry.getValue().toString());
if (parameters.isKeyLocked(name))
{
attrs.addAttribute("", ATTR_LOCKED, ATTR_LOCKED, "CDATA", "true");
}
context.startElement(QNAME_PARAM, attrs);
context.endElement();
}
}
public void removeParameter(String name)
{
parameters.remove(name);
}
public final Handler getInstance(EngineConfiguration registry)
throws ConfigurationException
{
if (scope == SCOPE_SINGLETON)
{
synchronized (this)
{
if (singletonInstance == null)
singletonInstance = getNewInstance(registry);
}
return singletonInstance;
}
return getNewInstance(registry);
}
private Handler getNewInstance(EngineConfiguration registry)
throws ConfigurationException
{
QName type = getType();
if (type == null ||
WSDDConstants.URI_WSDD_JAVA.equals(type.getNamespaceURI()))
{
return makeNewInstance(registry);
}
else
{
return registry.getHandler(type);
}
}
protected Handler makeNewInstance(EngineConfiguration registry)
throws ConfigurationException
{
Class c = null;
Handler h = null;
try
{
c = getJavaClass();
}
catch (ClassNotFoundException e)
{
throw new ConfigurationException(e);
}
if (c != null)
{
try
{
h = (Handler)createInstance(c);
}
catch (Exception e)
{
throw new ConfigurationException(e);
}
if (h != null)
{
if (qname != null)
h.setName(qname.getLocalPart());
h.setOptions(getParametersTable());
try
{
h.init();
}
catch (Exception e)
{
String msg = e + JavaUtils.LS + JavaUtils.stackToString(e);
log.debug(msg);
throw new ConfigurationException(e);
}
catch (Error e)
{
String msg = e + JavaUtils.LS + JavaUtils.stackToString(e);
log.debug(msg);
throw new ConfigurationException(msg);
}
}
}
else
{
h = registry.getHandler(getType());
}
return h;
}
Object createInstance(Class _class)
throws InstantiationException, IllegalAccessException
{
return _class.newInstance();
}
public Class getJavaClass()
throws ClassNotFoundException
{
QName type = getType();
if (type != null &&
URI_WSDD_JAVA.equals(type.getNamespaceURI()))
{
return ClassUtils.forName(type.getLocalPart());
}
return null;
}
}