package org.jboss.axis.configuration;
import org.jboss.axis.AxisEngine;
import org.jboss.axis.ConfigurationException;
import org.jboss.axis.Handler;
import org.jboss.axis.WSDDEngineConfiguration;
import org.jboss.axis.deployment.wsdd.WSDDDeployment;
import org.jboss.axis.deployment.wsdd.WSDDDocument;
import org.jboss.axis.deployment.wsdd.WSDDGlobalConfiguration;
import org.jboss.axis.encoding.TypeMappingRegistry;
import org.jboss.axis.handlers.soap.SOAPService;
import org.jboss.axis.utils.Admin;
import org.jboss.axis.utils.ClassUtils;
import org.jboss.axis.utils.Messages;
import org.jboss.axis.utils.XMLUtils;
import org.jboss.logging.Logger;
import org.w3c.dom.Document;
import javax.xml.namespace.QName;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.Hashtable;
import java.util.Iterator;
public class FileProvider implements WSDDEngineConfiguration
{
private static Logger log = Logger.getLogger(FileProvider.class.getName());
private WSDDDeployment deployment = null;
private String filename;
private File configFile = null;
private InputStream myInputStream = null;
private boolean readOnly = true;
private boolean searchClasspath = true;
public FileProvider(String filename)
{
this.filename = filename;
configFile = new File(filename);
check();
}
public FileProvider(String basepath, String filename)
throws ConfigurationException
{
this.filename = filename;
File dir = new File(basepath);
if (!dir.isDirectory() || !dir.canRead())
{
throw new ConfigurationException(Messages.getMessage
("invalidConfigFilePath",
basepath));
}
configFile = new File(basepath, filename);
check();
}
private void check()
{
readOnly = configFile.canRead() & !configFile.canWrite();
if (readOnly)
{
log.info(Messages.getMessage("readOnlyConfigFile"));
}
}
public FileProvider(InputStream is)
{
setInputStream(is);
}
public void setInputStream(InputStream is)
{
myInputStream = is;
}
private InputStream getInputStream()
{
return myInputStream;
}
public WSDDDeployment getDeployment()
{
return deployment;
}
public void setDeployment(WSDDDeployment deployment)
{
this.deployment = deployment;
}
public void setSearchClasspath(boolean searchClasspath)
{
this.searchClasspath = searchClasspath;
}
public void configureEngine(AxisEngine engine)
throws ConfigurationException
{
try
{
if (getInputStream() == null)
{
try
{
setInputStream(new FileInputStream(configFile));
}
catch (Exception e)
{
if (searchClasspath)
setInputStream(ClassUtils.getResourceAsStream(engine.getClass(), filename));
}
}
if (getInputStream() == null)
{
throw new ConfigurationException(Messages.getMessage("noConfigFile"));
}
WSDDDocument doc = new WSDDDocument(XMLUtils.
newDocument(getInputStream()));
deployment = doc.getDeployment();
deployment.configureEngine(engine);
engine.refreshGlobalOptions();
setInputStream(null);
}
catch (Exception e)
{
throw new ConfigurationException(e);
}
}
public void writeEngineConfig(AxisEngine engine)
throws ConfigurationException
{
if (!readOnly)
{
try
{
Document doc = Admin.listConfig(engine);
StringWriter writer = new StringWriter();
XMLUtils.DocumentToWriter(doc, writer);
writer.close();
FileOutputStream fos = new FileOutputStream(configFile);
fos.write(writer.getBuffer().toString().getBytes());
fos.close();
}
catch (Exception e)
{
throw new ConfigurationException(e);
}
}
}
public Handler getHandler(QName qname) throws ConfigurationException
{
return deployment.getHandler(qname);
}
public SOAPService getService(QName qname) throws ConfigurationException
{
return deployment.getService(qname);
}
public SOAPService getServiceByNamespaceURI(String namespace)
throws ConfigurationException
{
return deployment.getServiceByNamespaceURI(namespace);
}
public Handler getTransport(QName qname) throws ConfigurationException
{
return deployment.getTransport(qname);
}
public TypeMappingRegistry getTypeMappingRegistry()
throws ConfigurationException
{
return deployment.getTypeMappingRegistry();
}
public Handler getGlobalRequest() throws ConfigurationException
{
return deployment.getGlobalRequest();
}
public Handler getGlobalResponse() throws ConfigurationException
{
return deployment.getGlobalResponse();
}
public Hashtable getGlobalOptions() throws ConfigurationException
{
WSDDGlobalConfiguration globalConfig
= deployment.getGlobalConfiguration();
if (globalConfig != null)
return globalConfig.getParametersTable();
return null;
}
public Iterator getDeployedServices() throws ConfigurationException
{
return deployment.getDeployedServices();
}
}