Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2010, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.jboss.ws.common.utils;
 
 import static org.jboss.ws.common.Loggers.DEPLOYMENT_LOGGER;
 import static org.jboss.ws.common.Messages.MESSAGES;
 import static org.jboss.ws.common.integration.WSHelper.isJseDeployment;
 import static org.jboss.ws.common.integration.WSHelper.isWarArchive;
 
 import java.io.File;
 import java.net.URL;
 import java.util.List;
 
 
Abstract WSDL file publisher

Author(s):
alessio.soldano@jboss.com
Since:
25-Mar-2010
 
 public abstract class AbstractWSDLFilePublisher
 {
    // The deployment info for the web service archive
    protected final ArchiveDeployment dep;
    // The expected wsdl location in the deployment
    protected final String expLocation;
    // The server config
    protected final ServerConfig serverConfig;
    
    private static DocumentBuilder builder;
    
    {
       this. = dep;
       
       ServerConfig sc = dep.getAttachment(ServerConfig.class);
        = sc != null ? sc : getServerConfig();
       
       if (isJseDeployment(dep) || isWarArchive(dep))
       {
           = "WEB-INF/wsdl/";
       }
       else
       {
           = "META-INF/wsdl/";
       }
    }
    
    private static ServerConfig getServerConfig() {
       if(System.getSecurityManager() == null) {
          return AbstractServerConfig.getServerIntegrationServerConfig();
       }
    }
    
    private static synchronized DocumentBuilder getDocumentBuilder()
    {
      if ( == null)
      {
         final ClassLoader classLoader = SecurityActions.getContextClassLoader();
         SecurityActions.setContextClassLoader(AbstractWSDLFilePublisher.class.getClassLoader());
         try
         {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(false);
            factory.setNamespaceAware(true);
            factory.setExpandEntityReferences(false);
            try
            {
               factory.setFeature(.true);
            }
            catch (ParserConfigurationException pce)
            {
               .error(pce);
            }
             = DOMUtils.newDocumentBuilder(factory);
         }
         finally
         {
            SecurityActions.setContextClassLoader(classLoader);
         }
      }
      return ;
   }
   
   protected void publishWsdlImports(URL parentURLDefinition parentDefinitionList<Stringpublishedthrows Exception
   {
      this.publishWsdlImports(parentURLparentDefinitionpublished);
   }
   
   
Publish the wsdl imports for a given wsdl definition
   @SuppressWarnings("unchecked")
   protected void publishWsdlImports(URL parentURLDefinition parentDefinitionList<StringpublishedString expLocationthrows Exception
   {
      @SuppressWarnings("rawtypes")
      Iterator it = parentDefinition.getImports().values().iterator();
      while (it.hasNext())
      {
         for (Import wsdlImport : (List<Import>)it.next())
         {
            String locationURI = wsdlImport.getLocationURI();
            // its an external import, don't publish locally
            if (locationURI.startsWith("http://") == false && locationURI.startsWith("https://") == false)
            {
               // infinity loops prevention
               if (published.contains(locationURI))
               {
                  continue;
               }
               else
               {
                  published.add(locationURI);
               }
               
               String baseURI = parentURL.toExternalForm();
               URL targetURL = new URL(baseURI.substring(0, baseURI.lastIndexOf("/") + 1) + locationURI);
               File targetFile = new File(targetURL.getFile()); //JBWS-3488
               createParentDir(targetFile);
               Definition subdef = wsdlImport.getDefinition();
               WSDLFactory wsdlFactory = WSDLFactory.newInstance();
               javax.wsdl.xml.WSDLWriter wsdlWriter = wsdlFactory.newWSDLWriter();
               FileWriter fw = new FileWriter(targetFile);
               wsdlWriter.writeWSDL(subdeffw);
               fw.close();
               .wsdlImportPublishedTo(targetURL);
               // recursively publish imports
               publishWsdlImports(targetURLsubdefpublishedexpLocation);
               // Publish XMLSchema imports
               Element subdoc = DOMUtils.parse(targetURL.openStream(), getDocumentBuilder());
               publishSchemaImports(targetURLsubdocpublishedexpLocation);
            }
         }
      }
   }
   
   protected void createParentDir(File targetFile)
   {
      File parentFile = targetFile.getParentFile();
      if (parentFile != null) {
         if (!parentFile.mkdirs()) {
            ; // exception will be thrown later in this code
         }
      }
   }
   
   protected void publishSchemaImports(URL parentURLElement elementList<Stringpublishedthrows Exception
   {
      this.publishSchemaImports(parentURLelementpublished);
   }

   
Publish the schema imports for a given wsdl definition
   protected void publishSchemaImports(URL parentURLElement elementList<StringpublishedString expLocationthrows Exception
   {
      Element childElement = getFirstChildElement(element);
      while (childElement != null) {
         //first check on namespace only to avoid doing anything on any other wsdl/schema elements
         final String ns = childElement.getNamespaceURI();
         if (..equals(ns)) {
            final String ln = childElement.getLocalName();
            if ("import".equals(ln) || "include".equals(ln)) {
               String schemaLocation = childElement.getAttribute("schemaLocation");
               if (schemaLocation.length() > 0 && schemaLocation.startsWith("http://") == false  && schemaLocation.startsWith("https://") == false)
               {
                  // infinity loops prevention
                  if (!published.contains(schemaLocation))
                  {
                     published.add(schemaLocation);
                     String baseURI = parentURL.toExternalForm();
                     URL xsdURL = new URL(baseURI.substring(0, baseURI.lastIndexOf("/") + 1) + schemaLocation);
                     File targetFile = new File(xsdURL.getFile()); //JBWS-3488
                     createParentDir(targetFile);
                     String deploymentName = .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 = .getResourceResolver().resolve(resourcePath);
                     InputStream is = new ResourceURL(resourceURL).openStream();
                     if (is == null)
                        throw .cannotFindSchemaImportInDeployment(resourcePathdeploymentName);
                     FileOutputStream fos = null;
                     try
                     {
                        fos = new FileOutputStream(targetFile);
                        IOUtils.copyStream(fosis);
                     }
                     finally
                     {
                        if (fos != nullfos.close();
                     }
                     .xmlSchemaImportPublishedTo(xsdURL);
                     // recursively publish imports
                     Element subdoc = DOMUtils.parse(xsdURL.openStream(), getDocumentBuilder());
                     publishSchemaImports(xsdURLsubdocpublishedexpLocation);
                  }
               }
            } else if ("schema".equals(ln)) {
               //recurse, as xsd:schema might contain an import
               publishSchemaImports(parentURLchildElementpublishedexpLocation);
            }
         } else if (..equals(ns) && "types".equals(childElement.getLocalName())) {
            //recurse as wsdl:types might contain a schema
            publishSchemaImports(parentURLchildElementpublishedexpLocation);
         }
         childElement = getNextSiblingElement(childElement);
      }
   }
   
   private static Element getFirstChildElement(Node node) {
      Node fc = node.getFirstChild();
      Element e = null;
      if (fc != null) {
         if (fc.getNodeType() == .) {
            e = (Element)fc;
         } else{
            e = getNextSiblingElement(fc);
         }
      }
      return e;
   }
   
   private static Element getNextSiblingElement(Node node) {
      Element e = null;
      Node nextSibling = node.getNextSibling();
      while (e == null && nextSibling != null) {
         if (nextSibling.getNodeType() == .) {
            e = (Element)nextSibling;
         }
         nextSibling = nextSibling.getNextSibling();
      }
      return e;
   }
   
   
Delete the published wsdl
   public void unpublishWsdlFiles() throws IOException
   {
      String deploymentDir = (.getParent() != null ? .getParent().getSimpleName() : .getSimpleName());
      File serviceDir = new File(.getServerDataDir().getCanonicalPath() + "/wsdl/" + deploymentDir);
      deleteWsdlPublishDirectory(serviceDir);
   }

   
Delete the published wsdl document, traversing down the dir structure
   protected void deleteWsdlPublishDirectory(File dirthrows IOException
   {
      String[] files = dir.list();
      for (int i = 0; files != null && i < files.lengthi++)
      {
         String fileName = files[i];
         File file = new File(dir + "/" + fileName);
         if (file.isDirectory())
         {
            deleteWsdlPublishDirectory(file);
         }
         else
         {
            if (file.delete() == false)
               .cannotDeletePublishedWsdlDoc(file.toURI().toURL());
         }
      }
      // delete the directory as well
      if (dir.delete() == false) {
      }
   }
New to GrepCode? Check out our FAQ X