Package org.apache.muse.tools.generator.synthesizer

Source Code of org.apache.muse.tools.generator.synthesizer.ServerInterfaceSynthesizer

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/

package org.apache.muse.tools.generator.synthesizer;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.xml.namespace.QName;

import org.apache.muse.core.AbstractCapability;
import org.apache.muse.tools.generator.util.Capability;
import org.apache.muse.tools.generator.util.ConfigurationData;
import org.apache.muse.tools.generator.util.ConfigurationDataDescriptor;
import org.apache.muse.tools.inspector.JavaMethod;
import org.apache.muse.tools.inspector.JavaProperty;
import org.apache.muse.ws.resource.impl.AbstractWsResourceCapability;

public class ServerInterfaceSynthesizer extends AbstractSynthesizer {

  static ConfigurationDataDescriptor[] REQUIRED_PARAMETERS =
    new ConfigurationDataDescriptor[] {
      ConfigurationData.CAPABILITIES_MAP_LIST_CONFIGURATION,
    };

  protected StringBuffer _headerCode;

  protected StringBuffer _bodyCode;

  protected String _className;

  protected Set _importSet;

  private Map[] _capabilityMaps;

  private Set[] _ignoreSets;

  private Map[] _filesMaps;
 
  public ConfigurationData synthesize(ConfigurationData configuration) throws Exception {
    ConfigurationData.checkConfiguration(this, configuration);

    loadParameters(configuration);   
   
    for(int i = 0; i < _capabilityMaps.length; i++) {
     
      Map capabilities = _capabilityMaps[i];
      if(_filesMaps[i] == null) {
        _filesMaps[i] = new HashMap();
      }
     
      if(_ignoreSets[i] == null) {
        _ignoreSets[i] = new HashSet();
      }
     
      for (Iterator j = capabilities.values().iterator(); j.hasNext();) {
        Capability capability = (Capability)j.next();
        if(!capability.isBuiltIn()) {
          generateCapability(capability, _filesMaps[i], _ignoreSets[i]);
        }
      }
    }
   
    ConfigurationData resultData = (ConfigurationData) configuration.clone();
    resultData.addParameter(ConfigurationData.FILES_MAP_LIST, _filesMaps);
    resultData.addParameter(ConfigurationData.IGNORE_SET_LIST, _ignoreSets);    
   
    return resultData;
  }
 
  private void loadParameters(ConfigurationData configuration) {
    _capabilityMaps = (Map[])configuration.getParameter(ConfigurationData.CAPABILITIES_MAP_LIST);
    _ignoreSets = (Set[])configuration.getParameter(ConfigurationData.IGNORE_SET_LIST);
    _filesMaps = (Map[])configuration.getParameter(ConfigurationData.FILES_MAP_LIST);
   
    if(_filesMaps == null) {
      _filesMaps = new HashMap[_capabilityMaps.length];
    }
   
    if(_ignoreSets == null) {
      _ignoreSets = new HashSet[_capabilityMaps.length];
    }
  }

  protected void generateCapability(Capability capability, Map files, Set ignoreSet) {
    if(capability.isEmpty()) {
      return;
    }
   
    ClassInfo classInfo = new ClassInfo(capability);
    String className = generateClassName(INTERFACE_PREFIX, capability);
    initializeCode(className, classInfo);
   
    generateNSDeclarations(classInfo, _bodyCode);
    generatePropertyOperations(classInfo, _bodyCode);
    generateOperations(classInfo, _bodyCode);
   
    classInfo.addImports(_importSet);
   
    String classFileName = createFileName(className);   
    files.put(classFileName, generateCombinedCode(classInfo));
  }
 
  protected String generateCombinedCode(ClassInfo classInfo) {
    endHeaderCode(classInfo);
    endBodyCode();

    StringBuffer code = new StringBuffer();

    code.append(_headerCode);
    code.append(_bodyCode);

    return code.append(generateFooterCode()).toString();
  }

  protected void endBodyCode() {
    //do nothing
  }

  protected void endHeaderCode(ClassInfo classInfo) {
    generateImports(classInfo, _headerCode);
    generateClassDef(_className, true, _headerCode)
  }

  protected StringBuffer generateFooterCode() {
    StringBuffer footer = new StringBuffer();
    generateCloseBlock(footer);
    return footer;
  }

  protected void initializeCode(String className, ClassInfo classInfo) {
    _className = className;
    _headerCode = beginHeaderCode(_className);
    _bodyCode = beginBodyCode();
    _importSet = new HashSet();
  }
 
  protected StringBuffer beginBodyCode() {
    StringBuffer code = new StringBuffer();
    return code;
  }
 
  protected void generateOperations(ClassInfo classInfo, StringBuffer code) {
    Capability capability = classInfo.getCapability();
   
    for(Iterator i=capability.getOperations().iterator(); i.hasNext();) {
      JavaMethod method = (JavaMethod)i.next();   
     
      indent(code);
      code.append("public "
        + convertType(method.getReturnType(), classInfo)
        + " " + getMethodName(method));
     
      Class[] params = method.getParameterTypes();
      QName[] paramNames = method.getParameterTypeNames();
      code.append("(");
     
      if (params.length > 0) {
        int j;
       
        for (j = 0; j < params.length - 1; j++) {
                   
          code.append(convertType(params[j], classInfo)             
            + " "
            + getParamName(paramNames[j], j)
            + ", ");
        }
       
        code.append(convertType(params[j], classInfo)
          + " "
          + getParamName(paramNames[j], j));
      }
     
      code.append(") throws Exception")
     
      generateOperationBody(method, code);

      newLine(2,code);
    }
  }

  protected void generateOperationBody(JavaMethod method, StringBuffer code) {
    code.append(";");
  }

  protected void generatePropertyOperations(ClassInfo classInfo, StringBuffer code) {
    Capability capability = classInfo.getCapability();
   
    if(capability.getProperties().size() == 0) {
      return;
    }
   
    for(Iterator i=capability.getProperties().iterator(); i.hasNext();) {
      JavaProperty property = (JavaProperty)i.next();
     
      generateGetOperation(property, classInfo, code);
      genereateSetOperation(property, classInfo, code);

    }
  }

  protected void genereateSetOperation(JavaProperty property, ClassInfo classInfo, StringBuffer code) {   
    indent(code);
    code.append("public void");
    code.append(" set"
      + getPropertyName(property, true)
      + "("
      + convertType(property.getJavaType(), classInfo)
      + " param0)");
   
    generateSetOperationBody(property, code);
   
    newLine(2,code);
  }

  protected void generateSetOperationBody(JavaProperty property, StringBuffer code) {
    code.append(";");
  }

  protected void generateGetOperation(JavaProperty property, ClassInfo classInfo, StringBuffer code) {
    indent(code);
    code.append("public "
      + convertType(property.getJavaType(), classInfo)
      + " get"
      + getPropertyName(property, true)
      + "()");
   
    generateGetOperationBody(property, code);

    newLine(2,code);
  }

  protected void generateGetOperationBody(JavaProperty property, StringBuffer code) {
    code.append(";");
  }

  protected void generateNSDeclarations(ClassInfo classInfo, StringBuffer code) {
    Capability capability = classInfo.getCapability();
    indent(code);
    statement("String PREFIX = \"tns\";", code);
    newLine(2,code);
   
    indent(code);
    statement("String NAMESPACE_URI = \""+ capability.getURI() +"\";", code);
    newLine(2,code);
  }

  protected Class getBaseClass(Capability capability) {
    if(capability.getProperties().size() > 0) {
      return AbstractWsResourceCapability.class;
    }
        return AbstractCapability.class;
  }
 
  protected void addImports(Class[] classes) {
    for(int i=0; i < classes.length; i++) {
      addImport(classes[i]);
    }
  }
   
  protected void addImport(Class className) {
    _importSet.add(className);
  }

  public ConfigurationDataDescriptor[] getConfigurationDataDescriptions() {
    return REQUIRED_PARAMETERS;
  }
}
TOP

Related Classes of org.apache.muse.tools.generator.synthesizer.ServerInterfaceSynthesizer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.