Author: alessio.soldano(a)jboss.com
Date: 2010-03-31 13:09:25 -0400 (Wed, 31 Mar 2010)
New Revision: 11912
Modified:
stack/native/trunk/modules/core/src/main/java/org/jboss/wsf/stack/jbws/WSDLFilePublisher.java
Log:
[JBWS-2986] Extending AbstractWsdlFilePublisher
Modified:
stack/native/trunk/modules/core/src/main/java/org/jboss/wsf/stack/jbws/WSDLFilePublisher.java
===================================================================
---
stack/native/trunk/modules/core/src/main/java/org/jboss/wsf/stack/jbws/WSDLFilePublisher.java 2010-03-31
16:14:58 UTC (rev 11911)
+++
stack/native/trunk/modules/core/src/main/java/org/jboss/wsf/stack/jbws/WSDLFilePublisher.java 2010-03-31
17:09:25 UTC (rev 11912)
@@ -22,39 +22,27 @@
package org.jboss.wsf.stack.jbws;
import java.io.File;
-import java.io.FileOutputStream;
-import java.io.FileWriter;
import java.io.IOException;
-import java.io.InputStream;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
-import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.wsdl.Definition;
-import javax.wsdl.Import;
-import javax.wsdl.factory.WSDLFactory;
import org.jboss.logging.Logger;
import org.jboss.util.NotImplementedException;
import org.jboss.ws.Constants;
import org.jboss.ws.WSException;
-import org.jboss.ws.core.utils.ResourceURL;
+import org.jboss.ws.core.utils.AbstractWSDLFilePublisher;
import org.jboss.ws.metadata.umdm.ServiceMetaData;
import org.jboss.ws.metadata.umdm.UnifiedMetaData;
import org.jboss.ws.metadata.wsdl.WSDLDefinitions;
import org.jboss.ws.tools.wsdl.WSDLWriter;
-import org.jboss.wsf.common.DOMUtils;
import org.jboss.wsf.common.IOUtils;
-import org.jboss.wsf.spi.SPIProvider;
-import org.jboss.wsf.spi.SPIProviderResolver;
import org.jboss.wsf.spi.deployment.ArchiveDeployment;
-import org.jboss.wsf.spi.management.ServerConfig;
-import org.jboss.wsf.spi.management.ServerConfigFactory;
import org.w3c.dom.Document;
-import org.w3c.dom.Element;
/** A helper class that publishes the wsdl files and their imports to the
server/data/wsdl directory.
*
@@ -62,33 +50,14 @@
* @author Thomas.Diesler(a)jboss.org
* @since 02-June-2004
*/
-public class WSDLFilePublisher
+public class WSDLFilePublisher extends AbstractWSDLFilePublisher
{
// provide logging
private static final Logger log = Logger.getLogger(WSDLFilePublisher.class);
- // The deployment info for the web service archive
- private ArchiveDeployment dep;
- // The expected wsdl location in the deployment
- private String expLocation;
- // The server config
- private ServerConfig serverConfig;
-
public WSDLFilePublisher(ArchiveDeployment dep)
{
- this.dep = dep;
-
- SPIProvider spiProvider = SPIProviderResolver.getInstance().getProvider();
- serverConfig = spiProvider.getSPI(ServerConfigFactory.class).getServerConfig();
-
- if (dep.getType().toString().endsWith("JSE"))
- {
- expLocation = "WEB-INF/wsdl/";
- }
- else
- {
- expLocation = "META-INF/wsdl/";
- }
+ super(dep);
}
/** Publish the deployed wsdl file to the data directory
@@ -100,7 +69,7 @@
// For each service
for (ServiceMetaData serviceMetaData : wsMetaData.getServices())
{
- File wsdlFile = getPublishLocation(deploymentName, serviceMetaData);
+ File wsdlFile = getPublishLocation(deploymentName,
serviceMetaData.getWsdlFileOrLocation().toExternalForm(),
serviceMetaData.getWsdlPublishLocation());
if (wsdlFile == null)
continue;
@@ -114,22 +83,22 @@
WSDLDefinitions wsdlDefinitions = serviceMetaData.getWsdlDefinitions();
new WSDLWriter(wsdlDefinitions).write(fWriter,
Constants.DEFAULT_XML_CHARSET);
- URL wsdlPublishURL = wsdlFile.toURL();
+ URL wsdlPublishURL = wsdlFile.toURI().toURL();
log.info("WSDL published to: " + wsdlPublishURL);
// udpate the wsdl file location
- serviceMetaData.setWsdlLocation(wsdlFile.toURL());
+ serviceMetaData.setWsdlLocation(wsdlPublishURL);
// Process the wsdl imports
Definition wsdl11Definition = wsdlDefinitions.getWsdlOneOneDefinition();
if (wsdl11Definition != null)
{
List<String> published = new LinkedList<String>();
- publishWsdlImports(wsdlFile.toURL(), wsdl11Definition, published);
+ publishWsdlImports(wsdlPublishURL, wsdl11Definition, published);
// Publish XMLSchema imports
Document document = wsdlDefinitions.getWsdlDocument();
- publishSchemaImports(wsdlFile.toURL(), document.getDocumentElement(),
published);
+ publishSchemaImports(wsdlPublishURL, document.getDocumentElement(),
published);
}
else
{
@@ -153,192 +122,23 @@
}
}
}
-
- /** Publish the wsdl imports for a given wsdl definition
- */
- private void publishWsdlImports(URL parentURL, Definition parentDefinition,
List<String> published) throws Exception
- {
- String baseURI = parentURL.toExternalForm();
-
- Iterator it = parentDefinition.getImports().values().iterator();
- while (it.hasNext())
- {
- for (Import wsdlImport : (List<Import>)it.next())
- {
- String locationURI = wsdlImport.getLocationURI();
- Definition subdef = wsdlImport.getDefinition();
-
- // its an external import, don't publish locally
- if (locationURI.startsWith("http://") == false)
- {
- // infinity loops prevention
- if (published.contains(locationURI))
- {
- continue;
- }
- else
- {
- published.add(locationURI);
- }
-
- URL targetURL = new URL(baseURI.substring(0,
baseURI.lastIndexOf("/") + 1) + locationURI);
- File targetFile = new File(targetURL.getPath());
- targetFile.getParentFile().mkdirs();
-
- WSDLFactory wsdlFactory = WSDLFactory.newInstance();
- javax.wsdl.xml.WSDLWriter wsdlWriter = wsdlFactory.newWSDLWriter();
- FileWriter fw = new FileWriter(targetFile);
- wsdlWriter.writeWSDL(subdef, fw);
- fw.close();
-
- if (log.isDebugEnabled())
- log.debug("WSDL import published to: " + targetURL);
-
- // recursively publish imports
- publishWsdlImports(targetURL, subdef, published);
-
- // Publish XMLSchema imports
- Element subdoc = DOMUtils.parse(targetURL.openStream());
- publishSchemaImports(targetURL, subdoc, published);
- }
- }
- }
- }
-
- /** Publish the schema imports for a given wsdl definition
- */
- private void publishSchemaImports(URL parentURL, Element element, List<String>
published) throws Exception
- {
- String baseURI = parentURL.toExternalForm();
-
- Iterator it = DOMUtils.getChildElements(element);
- while (it.hasNext())
- {
- Element childElement = (Element)it.next();
- if ("import".equals(childElement.getLocalName()) ||
"include".equals(childElement.getLocalName()))
- {
- String schemaLocation =
childElement.getAttribute("schemaLocation");
- if (schemaLocation.length() > 0)
- {
- if (schemaLocation.startsWith("http://") == false)
- {
- // infinity loops prevention
- if (published.contains(schemaLocation))
- {
- continue;
- }
- else
- {
- published.add(schemaLocation);
- }
-
- URL xsdURL = new URL(baseURI.substring(0,
baseURI.lastIndexOf("/") + 1) + schemaLocation);
- File targetFile = new File(xsdURL.getPath());
- targetFile.getParentFile().mkdirs();
-
- String deploymentName = dep.getCanonicalName();
-
- // get the resource path including the separator
- int index = baseURI.indexOf(deploymentName) + 1;
- String resourcePath = baseURI.substring(index +
deploymentName.length());
- //check for sub-directories
- resourcePath = resourcePath.substring(0,
resourcePath.lastIndexOf("/") + 1);
-
- resourcePath = expLocation + resourcePath + schemaLocation;
- while (resourcePath.indexOf("//") != -1)
- {
- resourcePath = resourcePath.replace("//", "/");
- }
- URL resourceURL = dep.getMetaDataFileURL(resourcePath);
- InputStream is = new ResourceURL(resourceURL).openStream();
- if (is == null)
- throw new IllegalArgumentException("Cannot find schema import
in deployment: " + resourcePath);
-
- FileOutputStream fos = null;
- try
- {
- fos = new FileOutputStream(targetFile);
- IOUtils.copyStream(fos, is);
- }
- finally
- {
- if (fos != null) fos.close();
- }
-
- if (log.isDebugEnabled())
- log.debug("XMLSchema import published to: " + xsdURL);
-
- // recursively publish imports
- Element subdoc = DOMUtils.parse(xsdURL.openStream());
- publishSchemaImports(xsdURL, subdoc, published);
- }
- }
- }
- else
- {
- publishSchemaImports(parentURL, childElement, published);
- }
- }
- }
-
+
/**
- * Delete the published wsdl
- */
- public void unpublishWsdlFiles() throws IOException
- {
- String deploymentDir = (dep.getParent() != null ? dep.getParent().getSimpleName() :
dep.getSimpleName());
-
- File serviceDir = new File(serverConfig.getServerDataDir().getCanonicalPath() +
"/wsdl/" + deploymentDir);
- deleteWsdlPublishDirectory(serviceDir);
- }
-
- /**
- * Delete the published wsdl document, traversing down the dir structure
- */
- private void deleteWsdlPublishDirectory(File dir) throws IOException
- {
- String[] files = dir.list();
- for (int i = 0; files != null && i < files.length; i++)
- {
- String fileName = files[i];
- File file = new File(dir + "/" + fileName);
- if (file.isDirectory())
- {
- deleteWsdlPublishDirectory(file);
- }
- else
- {
- if (file.delete() == false)
- log.warn("Cannot delete published wsdl document: " +
file.toURL());
- }
- }
-
- // delete the directory as well
- dir.delete();
- }
-
- /**
* Get the file publish location
*/
- private File getPublishLocation(String archiveName, ServiceMetaData serviceMetaData)
throws IOException
+ private File getPublishLocation(String archiveName, String wsdlLocation, String
wsdlPublishLocation) throws IOException
{
- String wsdlLocation = null;
- if (serviceMetaData.getWsdlLocation() != null)
- wsdlLocation = serviceMetaData.getWsdlLocation().toExternalForm();
- else if (serviceMetaData.getWsdlFile() != null)
- wsdlLocation = serviceMetaData.getWsdlFile();
-
if (wsdlLocation == null)
{
- log.warn("Cannot obtain wsdl location for: " +
serviceMetaData.getServiceName());
+ log.warn("Cannot get wsdl publish location for null wsdl location!");
return null;
}
- if (log.isDebugEnabled())
- log.debug("Publish WSDL file: " + wsdlLocation);
+ if (log.isTraceEnabled())
+ log.trace("Publishing WSDL file: " + wsdlLocation);
// Only file URLs are supported in <wsdl-publish-location>
- String publishLocation = serviceMetaData.getWsdlPublishLocation();
+ String publishLocation = wsdlPublishLocation;
boolean predefinedLocation = publishLocation != null &&
publishLocation.startsWith("file:");
File locationFile = null;
@@ -363,22 +163,22 @@
}
}
- File wsdlFile;
+ File result;
if (wsdlLocation.indexOf(expLocation) >= 0)
{
wsdlLocation = wsdlLocation.substring(wsdlLocation.indexOf(expLocation) +
expLocation.length());
- wsdlFile = new File(locationFile + "/" + wsdlLocation);
+ result = new File(locationFile + "/" + wsdlLocation);
}
else if (wsdlLocation.startsWith("vfsfile:") ||
wsdlLocation.startsWith("file:") || wsdlLocation.startsWith("jar:") ||
wsdlLocation.startsWith("vfszip:"))
{
wsdlLocation = wsdlLocation.substring(wsdlLocation.lastIndexOf("/") +
1);
- wsdlFile = new File(locationFile + "/" + wsdlLocation);
+ result = new File(locationFile + "/" + wsdlLocation);
}
else
{
- throw new WSException("Invalid wsdlFile '" + wsdlLocation +
"', expected in: " + expLocation);
+ throw new RuntimeException("Invalid wsdlFile '" + wsdlLocation +
"', expected in: " + expLocation);
}
- return wsdlFile;
+ return result;
}
}