WsdlInfo.java :  » ESB » cbesb-1.2 » com » bostechcorp » cbesb » common » wsdl » Java Open Source

Java Open Source » ESB » cbesb 1.2 
cbesb 1.2 » com » bostechcorp » cbesb » common » wsdl » WsdlInfo.java
/*
 * ChainBuilder ESB
 *          Visual Enterprise Integration
 * 
 * Copyright (C) 2007 Bostech Corporation
 * 
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the 
 * Free Software Foundation; either version 2 of the License, or (at your option) 
 * any later version.
 *
 * This program 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 General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along with 
 * this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *
 * $Id: WsdlInfo.java 11051 2007-12-19 02:23:48Z lzheng $
 */

package com.bostechcorp.cbesb.common.wsdl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import javax.wsdl.Definition;
import javax.wsdl.Service;
import javax.wsdl.Types;
import javax.wsdl.WSDLException;
import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLReader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.bostechcorp.cbesb.common.util.FileUtil;
import com.ibm.wsdl.extensions.schema.SchemaImpl;
import com.ibm.wsdl.extensions.schema.SchemaImportImpl;
import com.ibm.wsdl.extensions.schema.SchemaReferenceImpl;

public class WsdlInfo extends ComponentInfo {

  protected String wsdlBaseName;

  protected Definition wsdlDefinition;

  protected TypesInfo wsdlTypes;

  /** JWSDL Factory instance */
  protected WSDLFactory wsdlFactory = null;

  /** The default SOAP encoding to use. */
  public final static String DEFAULT_SOAP_ENCODING_STYLE = "http://schemas.xmlsoap.org/soap/encoding/";

  // List of services contained by the loaded WSDL
  private LinkedHashMap<String, ServiceInfo> services;

  protected static transient Log logger = LogFactory.getLog(WsdlInfo.class);

  private WsdlInfo() {
    services = new LinkedHashMap<String, ServiceInfo>();
    try {
      wsdlFactory = WSDLFactory.newInstance();
    }

    catch (Throwable t) {
      logger.error("Exception in WsdlInfo(): " + t.getMessage());
      if (logger.isDebugEnabled()) {
        logger.debug("Exception in WsdlInfo():", t);
      }
    }
  }

  public static WsdlInfo load(File wsdlFile) throws WSDLException {
    WsdlInfo wsdlInfo = new WsdlInfo();
    wsdlInfo.loadInstance(wsdlFile);

    return wsdlInfo;
  }

  private void loadInstance(File wsdlFile) throws WSDLException {
    setWsdlBaseNameFromFilename(wsdlFile);

    // Create the WSDL Reader object
    WSDLReader reader = wsdlFactory.newWSDLReader();

    // Read the WSDL and get the top-level Definition object
    wsdlDefinition = reader.readWSDL(null, wsdlFile.getAbsolutePath());

    // Process all schemas defined in the WSDL types
    wsdlTypes = TypesInfo.load(this);

    // Get the services defined in the document
    Map svcs = wsdlDefinition.getServices();

    if (svcs != null) {
      // Create a component for each service defined
      Iterator svcIter = svcs.values().iterator();

      for (int i = 0; svcIter.hasNext(); i++) {
        Service wsdlService = (Service) svcIter.next();
        ServiceInfo serviceInfo = ServiceInfo.load(this, wsdlService);
        services.put(serviceInfo.getName(), serviceInfo);
      }
    }
  }

  private void setWsdlBaseNameFromFilename(File wsdlFile) {
    // Remove the file extension from the wsdl if it exists,
    // and use that as the base name for the schemas.
    String wsdlFileName = wsdlFile.getName();
    int index = wsdlFileName.lastIndexOf(".");
    if (index > 0) {
      wsdlBaseName = wsdlFileName.substring(0, index);
    } else {
      wsdlBaseName = wsdlFileName;
    }
  }

  public String[] getServiceNames() {
    String[] retArray = null;
    Set serviceNames = services.keySet();
    retArray = new String[serviceNames.size()];
    int i = 0;
    for (Iterator iter = serviceNames.iterator(); iter.hasNext();) {
      retArray[i] = (String) iter.next();
      i++;
    }
    return retArray;
  }

  public List getServices() {
    return new ArrayList<ServiceInfo>(services.values());
  }

  public ServiceInfo getServiceInfo(String serviceName) {
    ServiceInfo service = null;
    if (services != null) {
      service = services.get(serviceName);
    }
    return service;
  }

  public void saveTypesAsSchemas(File destDir) throws IOException {
    wsdlTypes.saveSchemas(destDir, wsdlBaseName);
  }

  public String getDefaultLocationURI() {
    String locationURI = null;
    if (services.size() > 0) {
      List svcs = getServices();
      // Get the first Service
      ServiceInfo svc = (ServiceInfo) svcs.get(0);
      List ports = svc.getPorts();
      if (ports.size() > 0) {
        // Get the first Port
        PortInfo port = (PortInfo) ports.get(0);
        locationURI = port.getLocationURI();
      }
    }

    return locationURI;
  }

  /**
   * For debugging
   */
  public String toString() {
    StringBuffer buffer = new StringBuffer();
    buffer.append(wsdlTypes.toString() + "\n");
    for (Iterator iter = getServices().iterator(); iter.hasNext();) {
      ServiceInfo service = (ServiceInfo) iter.next();
      buffer.append(service.toString() + "\n");
    }
    return buffer.toString();
  }

  /**
   * @return the wsdlDefinition
   */
  public Definition getWsdlDefinition() {
    return wsdlDefinition;
  }

  /**
   * exctacts all external resources URI's, resolve the path and return the
   * Map<Source,Destination>
   * 
   * Note: this method soes not execute any copy operation 
   * Note: the sourceWsdl itself is not included in returned Map
   * 
   * @param sourceWsdl -
   *            absolute file to the wsdl file
   * @param destinationFolder -
   *            folder to which wsdl file will be copyed
   * @return - Map<Source,Destination>
   * @throws WSDLException
   */
  public static HashMap<String, String> extractFilesForCopy(
      String sourceWsdl, String destinationFolder) throws WSDLException {
    HashMap<String, String> map = new HashMap<String, String>();
    
    //first the wsdl file
    // pre preocess paths
    File sourceWsdlFile=new File(sourceWsdl);
    String sourceWsdlString=sourceWsdlFile.toURI().getPath().substring(1);
    String destinationFolderString=new File(destinationFolder).toURI().getPath().substring(1);
    if (!destinationFolderString.endsWith("/"))destinationFolderString += "/";
    //
    WsdlInfo wsdl = load(sourceWsdlFile);
    Types t = wsdl.getWsdlDefinition().getTypes();
    for (Object extElementObj : t.getExtensibilityElements()) {
      if (extElementObj instanceof SchemaImpl) {
        SchemaImpl schema = (SchemaImpl) extElementObj;
        // parsing imports
        schema.getImports().values();
        for (Object importObj : schema.getImports().values()) {
          if (importObj instanceof Vector) {
            Vector importVector = (Vector) importObj;
            for (Object importVectorItem : importVector) {
              if (importVectorItem instanceof SchemaImportImpl) {
                SchemaImportImpl sii = (SchemaImportImpl) importVectorItem;
                if(sii.getSchemaLocationURI()!=null){
                  String schemaSource = FileUtil.processLocationURI(
                      sourceWsdlString, sii.getSchemaLocationURI());
                  String schemaDestination = FileUtil.processLocationURI(
                      destinationFolderString,sii.getSchemaLocationURI());
                map.put(schemaSource, schemaDestination);
                }
              }
            }
          }
        }
        // parse includes
        for (Object includeObj : schema.getIncludes()) {
          if (includeObj instanceof SchemaReferenceImpl) {
            String schemaSource = FileUtil.processLocationURI(
                sourceWsdlString, ((SchemaReferenceImpl) includeObj)
                    .getSchemaLocationURI());
            String schemaDestination = FileUtil.processLocationURI(
                destinationFolderString, ((SchemaReferenceImpl) includeObj)
                .getSchemaLocationURI());
            map.put(schemaSource, schemaDestination);
          }
        }
      }
    }
    return map;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.