001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.tools.wsdlto.frontend.jaxws.processor.internal;
019:
020: import java.util.Collection;
021: import java.util.List;
022:
023: import org.apache.cxf.common.util.StringUtils;
024: import org.apache.cxf.service.model.FaultInfo;
025: import org.apache.cxf.service.model.MessagePartInfo;
026: import org.apache.cxf.tools.common.ToolConstants;
027: import org.apache.cxf.tools.common.ToolContext;
028: import org.apache.cxf.tools.common.ToolException;
029: import org.apache.cxf.tools.common.model.JavaException;
030: import org.apache.cxf.tools.common.model.JavaExceptionClass;
031: import org.apache.cxf.tools.common.model.JavaField;
032: import org.apache.cxf.tools.common.model.JavaMethod;
033: import org.apache.cxf.tools.common.model.JavaModel;
034: import org.apache.cxf.tools.util.ClassCollector;
035: import org.apache.cxf.tools.util.NameUtil;
036: import org.apache.cxf.tools.wsdlto.frontend.jaxws.customization.JAXWSBinding;
037:
038: public class FaultProcessor extends AbstractProcessor {
039: private ClassCollector collector;
040:
041: public FaultProcessor(ToolContext penv) {
042: super (penv);
043: collector = penv.get(ClassCollector.class);
044: }
045:
046: public void process(JavaMethod method, Collection<FaultInfo> faults)
047: throws ToolException {
048: if (faults == null) {
049: return;
050: }
051:
052: for (FaultInfo fault : faults) {
053: processFault(method, fault);
054: }
055: }
056:
057: private boolean isNameCollision(String packageName, String className) {
058: if (context.optionSet(ToolConstants.CFG_GEN_OVERWRITE)) {
059: return false;
060: }
061: boolean collision = collector.containTypesClass(packageName,
062: className)
063: || collector.containSeiClass(packageName, className);
064: return collision;
065: }
066:
067: @SuppressWarnings("unchecked")
068: private void processFault(JavaMethod method, FaultInfo faultMessage)
069: throws ToolException {
070: JavaModel model = method.getInterface().getJavaModel();
071:
072: String name = NameUtil.mangleNameToClassName(faultMessage
073: .getName().getLocalPart());
074: String namespace = faultMessage.getName().getNamespaceURI();
075: String packageName = ProcessorUtil.parsePackageName(namespace,
076: context.mapPackageName(namespace));
077: if (namespace.equals(method.getInterface().getNamespace())) {
078: packageName = method.getInterface().getPackageName();
079: }
080:
081: JAXWSBinding jaxwsBinding = faultMessage
082: .getExtensor(JAXWSBinding.class);
083: if (jaxwsBinding != null) {
084: if (jaxwsBinding.getPackage() != null) {
085: packageName = jaxwsBinding.getPackage();
086: }
087: if (jaxwsBinding.getJaxwsClass() != null
088: && jaxwsBinding.getJaxwsClass().getClassName() != null) {
089: name = jaxwsBinding.getJaxwsClass().getClassName();
090: }
091: }
092:
093: while (isNameCollision(packageName, name)) {
094: name = name + "_Exception";
095: }
096:
097: String fullClassName = packageName + "." + name;
098: collector.addExceptionClassName(packageName, name,
099: fullClassName);
100:
101: boolean samePackage = method.getInterface().getPackageName()
102: .equals(packageName);
103: method.addException(new JavaException(name, samePackage ? name
104: : fullClassName, namespace));
105:
106: List<MessagePartInfo> faultParts = faultMessage
107: .getMessageParts();
108:
109: JavaExceptionClass expClass = new JavaExceptionClass(model);
110: expClass.setName(name);
111: expClass.setNamespace(namespace);
112: expClass.setPackageName(packageName);
113:
114: for (MessagePartInfo part : faultParts) {
115: String fName = null;
116: String fNamespace = null;
117:
118: if (part.getElementQName() != null) {
119: fNamespace = part.getElementQName().getNamespaceURI();
120: //fNamespace = part.getConcreteName().getNamespaceURI();
121: fName = part.getConcreteName().getLocalPart();
122: } else {
123: fNamespace = part.getTypeQName().getNamespaceURI();
124: fName = part.getConcreteName().getLocalPart();
125: }
126:
127: if (StringUtils.isEmpty(fNamespace)) {
128: fNamespace = namespace;
129: }
130:
131: String fType = ProcessorUtil.getType(part, context, false);
132:
133: //REVISIT - custom JAXB package names
134: String fPackageName = method.getInterface()
135: .getPackageName();
136:
137: JavaField fField = new JavaField(fName, fType, fNamespace);
138: fField.setQName(ProcessorUtil.getElementName(part));
139:
140: if (!method.getInterface().getPackageName().equals(
141: fPackageName)) {
142: fField.setClassName(ProcessorUtil.getFullClzName(part,
143: context, false));
144: }
145: if (!fType.equals(ProcessorUtil.resolvePartType(part))) {
146: fField.setClassName(ProcessorUtil.getType(part,
147: context, true));
148: }
149:
150: expClass.addField(fField);
151: }
152: model.addExceptionClass(packageName + "." + name, expClass);
153: }
154: }
|