001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.ws.processor.generator;
038:
039: import com.sun.codemodel.ClassType;
040: import com.sun.codemodel.JAnnotationUse;
041: import com.sun.codemodel.JBlock;
042: import com.sun.codemodel.JClass;
043: import com.sun.codemodel.JClassAlreadyExistsException;
044: import com.sun.codemodel.JDefinedClass;
045: import com.sun.codemodel.JDocComment;
046: import com.sun.codemodel.JExpr;
047: import com.sun.codemodel.JFieldRef;
048: import com.sun.codemodel.JFieldVar;
049: import com.sun.codemodel.JMethod;
050: import com.sun.codemodel.JMod;
051: import com.sun.codemodel.JType;
052: import com.sun.codemodel.JVar;
053: import com.sun.tools.ws.processor.model.Fault;
054: import com.sun.tools.ws.processor.model.Model;
055: import com.sun.tools.ws.wscompile.ErrorReceiver;
056: import com.sun.tools.ws.wscompile.WsimportOptions;
057:
058: import javax.xml.ws.WebFault;
059: import java.util.HashMap;
060: import java.util.Map;
061:
062: /**
063: *
064: * @author WS Development Team
065: */
066: public class CustomExceptionGenerator extends GeneratorBase {
067: private Map<String, JClass> faults = new HashMap<String, JClass>();
068:
069: public static void generate(Model model, WsimportOptions options,
070: ErrorReceiver receiver) {
071: CustomExceptionGenerator exceptionGen = new CustomExceptionGenerator(
072: model, options, receiver);
073: exceptionGen.doGeneration();
074: }
075:
076: private CustomExceptionGenerator(Model model,
077: WsimportOptions options, ErrorReceiver receiver) {
078: super (model, options, receiver);
079: }
080:
081: public GeneratorBase getGenerator(Model model,
082: WsimportOptions options, ErrorReceiver receiver) {
083: return new CustomExceptionGenerator(model, options, receiver);
084: }
085:
086: @Override
087: public void visit(Fault fault) throws Exception {
088: if (isRegistered(fault))
089: return;
090: registerFault(fault);
091: }
092:
093: private boolean isRegistered(Fault fault) {
094: if (faults.keySet()
095: .contains(fault.getJavaException().getName())) {
096: fault.setExceptionClass(faults.get(fault.getJavaException()
097: .getName()));
098: return true;
099: }
100: return false;
101: }
102:
103: private void registerFault(Fault fault) {
104: try {
105: write(fault);
106: faults.put(fault.getJavaException().getName(), fault
107: .getExceptionClass());
108: } catch (JClassAlreadyExistsException e) {
109: throw new GeneratorException(
110: "generator.nestedGeneratorError", e);
111: }
112: }
113:
114: private void write(Fault fault) throws JClassAlreadyExistsException {
115: String className = Names.customExceptionClassName(fault);
116:
117: JDefinedClass cls = cm._class(className, ClassType.CLASS);
118: JDocComment comment = cls.javadoc();
119: if (fault.getJavaDoc() != null) {
120: comment.add(fault.getJavaDoc());
121: comment.add("\n\n");
122: }
123:
124: for (String doc : getJAXWSClassComment()) {
125: comment.add(doc);
126: }
127:
128: cls._extends(java.lang.Exception.class);
129:
130: //@WebFault
131: JAnnotationUse faultAnn = cls.annotate(WebFault.class);
132: faultAnn.param("name", fault.getBlock().getName()
133: .getLocalPart());
134: faultAnn.param("targetNamespace", fault.getBlock().getName()
135: .getNamespaceURI());
136:
137: JType faultBean = fault.getBlock().getType().getJavaType()
138: .getType().getType();
139:
140: //faultInfo filed
141: JFieldVar fi = cls.field(JMod.PRIVATE, faultBean, "faultInfo");
142:
143: //add jaxb annotations
144: fault.getBlock().getType().getJavaType().getType().annotate(fi);
145:
146: fi.javadoc().add(
147: "Java type that goes as soapenv:Fault detail element.");
148: JFieldRef fr = JExpr.ref(JExpr._this (), fi);
149:
150: //Constructor
151: JMethod constrc1 = cls.constructor(JMod.PUBLIC);
152: JVar var1 = constrc1.param(String.class, "message");
153: JVar var2 = constrc1.param(faultBean, "faultInfo");
154: constrc1.javadoc().addParam(var1);
155: constrc1.javadoc().addParam(var2);
156: JBlock cb1 = constrc1.body();
157: cb1.invoke("super").arg(var1);
158:
159: cb1.assign(fr, var2);
160:
161: //constructor with Throwable
162: JMethod constrc2 = cls.constructor(JMod.PUBLIC);
163: var1 = constrc2.param(String.class, "message");
164: var2 = constrc2.param(faultBean, "faultInfo");
165: JVar var3 = constrc2.param(Throwable.class, "cause");
166: constrc2.javadoc().addParam(var1);
167: constrc2.javadoc().addParam(var2);
168: constrc2.javadoc().addParam(var3);
169: JBlock cb2 = constrc2.body();
170: cb2.invoke("super").arg(var1).arg(var3);
171: cb2.assign(fr, var2);
172:
173: //getFaultInfo() method
174: JMethod fim = cls
175: .method(JMod.PUBLIC, faultBean, "getFaultInfo");
176: fim.javadoc().addReturn().add(
177: "returns fault bean: " + faultBean.fullName());
178: JBlock fib = fim.body();
179: fib._return(fi);
180: fault.setExceptionClass(cls);
181:
182: }
183: }
|