?? abstractservicegenerator.java
字號:
package org.codehaus.xfire.gen.jsr181;import java.io.File;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import javax.xml.namespace.QName;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.codehaus.xfire.gen.GenerationContext;import org.codehaus.xfire.gen.GenerationException;import org.codehaus.xfire.gen.GeneratorPlugin;import org.codehaus.xfire.gen.SchemaSupport;import org.codehaus.xfire.service.Binding;import org.codehaus.xfire.service.FaultInfo;import org.codehaus.xfire.service.MessageInfo;import org.codehaus.xfire.service.MessagePartInfo;import org.codehaus.xfire.service.OperationInfo;import org.codehaus.xfire.service.Service;import org.codehaus.xfire.service.ServiceInfo;import org.codehaus.xfire.soap.AbstractSoapBinding;import com.sun.codemodel.ClassType;import com.sun.codemodel.JClass;import com.sun.codemodel.JCommentPart;import com.sun.codemodel.JDefinedClass;import com.sun.codemodel.JMethod;import com.sun.codemodel.JMod;import com.sun.codemodel.JPrimitiveType;import com.sun.codemodel.JType;import com.sun.codemodel.JVar;public abstract class AbstractServiceGenerator extends AbstractPlugin implements GeneratorPlugin{ private static final Log log = LogFactory.getLog(AbstractServiceGenerator.class); private Service currentService; public void generate(GenerationContext context) throws Exception { for (Iterator itr = context.getServices().values().iterator(); itr.hasNext();) { List services = (List) itr.next(); for (Iterator sitr = services.iterator(); sitr.hasNext();) { generate(context, (Service) sitr.next()); } } } public void generate(GenerationContext context, Service service) throws Exception { for (Iterator itr = service.getBindings().iterator(); itr.hasNext();) { Binding b = (Binding) itr.next(); if (b instanceof AbstractSoapBinding) { generate(context, service, b); } else { log.info("Unknown binding: " + b.getClass().getName()); } } } public void generate(GenerationContext context, Service service, Binding binding) throws Exception { setCurrentService(service); if (!isWritten(context)); ServiceInfo serviceInfo = service.getServiceInfo(); String clsName = getClassName(context, service); log.info("Creating class " + clsName); File classFile = new File(context.getOutputDirectory(), clsName.replace('.', File.separatorChar) + ".java"); if (classFile.exists() && !overwriteClass(context, service, clsName, classFile)) { return; } JDefinedClass jc = context.getCodeModel()._class(clsName, getClassType()); SchemaSupport schema = context.getSchemaGenerator(); String documentation = service.getServiceInfo().getDocumentation(); if( documentation != null ){ jc.javadoc().add(documentation); } annotate(context, service, jc, binding); // Process operations for (Iterator itr = serviceInfo.getOperations().iterator(); itr.hasNext();) { OperationInfo op = (OperationInfo) itr.next(); JType returnType = getReturnType(context, schema, op); String name = javify(op.getName()); name = name.substring(0, 1).toLowerCase() + name.substring(1); JMethod method = jc.method(JMod.PUBLIC, returnType, name); String opDocumentation = op.getDocumenation(); if( opDocumentation != null ){ method.javadoc().add(opDocumentation); } annotate(context, op, method); generateOperation(context, op, method); } } protected boolean overwriteClass(GenerationContext context, Service service, String clsName, File classFile) { return true; } public Service getCurrentService() { return currentService; } public void setCurrentService(Service currentService) { this.currentService = currentService; } protected boolean isWritten(GenerationContext context) { return false; } public static class ParamInfo{ private JType paramType; private String varName; private boolean in; private boolean out; private boolean header; private QName name; private MessagePartInfo part; public ParamInfo(JType paramType, String name){ this.paramType = paramType; this.varName = name; } public ParamInfo(JType paramType, String name, boolean header){ this(paramType,name); this.header = header; } public MessagePartInfo getPart() { return part; } public void setPart(MessagePartInfo part) { this.part = part; } public void setParamType(JType paramType) { this.paramType = paramType; } public QName getName() { return name; } public void setName(QName name) { this.name = name; } public boolean isHeader() { return header; } public void setHeader(boolean header) { this.header = header; } public boolean isIn() { return in; } public void setIn(boolean in) { this.in = in; } public boolean isOut() { return out; } public void setOut(boolean out) { this.out = out; } public JType getParamType() { return paramType; } public String getVarName() { return varName; } } private void generateOperation(GenerationContext context, OperationInfo op, JMethod method) throws GenerationException { Collection bindings = getCurrentService().getBindings(); SchemaSupport schema = context.getSchemaGenerator(); List<String> partNames = new ArrayList<String>(); Map<String, ParamInfo> params = new HashMap<String, ParamInfo>(); // input parts MessageInfo inputMsg = op.getInputMessage(); for (Iterator pitr = inputMsg.getMessageParts().iterator(); pitr.hasNext();) { MessagePartInfo part = (MessagePartInfo) pitr.next(); String varName = getUniqueName(javify(part.getName().getLocalPart()), partNames); partNames.add(varName); JType paramType = schema.getType(context, part); ParamInfo param = new ParamInfo(paramType,varName); param.setIn(true); param.setName(part.getName()); param.setPart(part); params.put(varName, param ); } // input parts for each binding for (Iterator itr = bindings.iterator(); itr.hasNext();) { Binding binding = (Binding) itr.next(); annotate(context, op, method, binding); List headers = binding.getHeaders(inputMsg).getMessageParts(); for (Iterator bitr = headers.iterator(); bitr.hasNext();)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -