001: package org.objectweb.celtix.tools.processors.wsdl2;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.FileOutputStream;
006: import java.io.IOException;
007: import java.util.ArrayList;
008: import java.util.HashSet;
009: import java.util.Iterator;
010: import java.util.List;
011: import java.util.Map;
012: import java.util.Set;
013:
014: import javax.wsdl.Definition;
015: import javax.wsdl.PortType;
016:
017: import com.sun.codemodel.JCodeModel;
018: import com.sun.codemodel.writer.FileCodeWriter;
019: import com.sun.tools.xjc.api.S2JJAXBModel;
020:
021: import org.objectweb.celtix.common.i18n.Message;
022: import org.objectweb.celtix.tools.common.ToolConstants;
023: import org.objectweb.celtix.tools.common.ToolException;
024: import org.objectweb.celtix.tools.common.model.JavaModel;
025: import org.objectweb.celtix.tools.extensions.jaxws.CustomizationParser;
026: import org.objectweb.celtix.tools.extensions.jaxws.JAXWSBinding;
027: import org.objectweb.celtix.tools.generators.wsdl2.AntGenerator;
028: import org.objectweb.celtix.tools.generators.wsdl2.ClientGenerator;
029: import org.objectweb.celtix.tools.generators.wsdl2.FaultGenerator;
030: import org.objectweb.celtix.tools.generators.wsdl2.ImplGenerator;
031: import org.objectweb.celtix.tools.generators.wsdl2.SEIGenerator;
032: import org.objectweb.celtix.tools.generators.wsdl2.ServerGenerator;
033: import org.objectweb.celtix.tools.generators.wsdl2.ServiceGenerator;
034: import org.objectweb.celtix.tools.processors.wsdl2.compiler.Compiler;
035: import org.objectweb.celtix.tools.processors.wsdl2.internal.ClassCollector;
036: import org.objectweb.celtix.tools.processors.wsdl2.internal.PortTypeProcessor;
037: import org.objectweb.celtix.tools.processors.wsdl2.internal.SEIAnnotationProcessor;
038: import org.objectweb.celtix.tools.processors.wsdl2.internal.ServiceProcessor;
039:
040: public class WSDLToJavaProcessor extends WSDLToProcessor {
041:
042: protected void registerGenerators(JavaModel jmodel) {
043: addGenerator(ToolConstants.SEI_GENERATOR, new SEIGenerator(
044: jmodel, getEnvironment()));
045: addGenerator(ToolConstants.FAULT_GENERATOR, new FaultGenerator(
046: jmodel, getEnvironment()));
047: addGenerator(ToolConstants.SVR_GENERATOR, new ServerGenerator(
048: jmodel, getEnvironment()));
049: addGenerator(ToolConstants.IMPL_GENERATOR, new ImplGenerator(
050: jmodel, getEnvironment()));
051: addGenerator(ToolConstants.CLT_GENERATOR, new ClientGenerator(
052: jmodel, getEnvironment()));
053: addGenerator(ToolConstants.SERVICE_GENERATOR,
054: new ServiceGenerator(jmodel, getEnvironment()));
055: addGenerator(ToolConstants.ANT_GENERATOR, new AntGenerator(
056: jmodel, getEnvironment()));
057: }
058:
059: public void process() throws ToolException {
060: validateWSDL();
061: init();
062: if (isSOAP12Binding(wsdlDefinition)) {
063: Message msg = new Message("SOAP12_UNSUPPORTED", LOG);
064: throw new ToolException(msg);
065: }
066: generateTypes();
067: JavaModel jmodel = wsdlDefinitionToJavaModel(getWSDLDefinition());
068: if (jmodel == null) {
069: Message msg = new Message("FAIL_TO_CREATE_JAVA_MODEL", LOG);
070: throw new ToolException(msg);
071: }
072: registerGenerators(jmodel);
073: doGeneration();
074: if (env.get(ToolConstants.CFG_COMPILE) != null) {
075: compile();
076: }
077: }
078:
079: private void generateTypes() throws ToolException {
080: if (rawJaxbModelGenCode == null) {
081: return;
082: }
083: try {
084: if (rawJaxbModelGenCode instanceof S2JJAXBModel) {
085: S2JJAXBModel schem2JavaJaxbModel = (S2JJAXBModel) rawJaxbModelGenCode;
086: JCodeModel jcodeModel = schem2JavaJaxbModel
087: .generateCode(null, null);
088: String dir = (String) env
089: .get(ToolConstants.CFG_OUTPUTDIR);
090: FileCodeWriter fileCodeWriter = new FileCodeWriter(
091: new File(dir));
092: jcodeModel.build(fileCodeWriter);
093: } else {
094: return;
095: }
096: } catch (IOException e) {
097: Message msg = new Message("FAIL_TO_GENERATE_TYPES", LOG);
098: throw new ToolException(msg);
099:
100: }
101: }
102:
103: private JavaModel wsdlDefinitionToJavaModel(Definition definition)
104: throws ToolException {
105: JavaModel javaModel = new JavaModel();
106: getEnvironment().put(ToolConstants.RAW_JAXB_MODEL,
107: getRawJaxbModel());
108:
109: javaModel.setJAXWSBinding(customizing(definition));
110:
111: Map portTypes = definition.getPortTypes();
112:
113: for (Iterator iter = portTypes.keySet().iterator(); iter
114: .hasNext();) {
115: PortType portType = (PortType) portTypes.get(iter.next());
116: PortTypeProcessor portTypeProcessor = new PortTypeProcessor(
117: getEnvironment());
118: portTypeProcessor.process(javaModel, portType);
119: }
120:
121: ServiceProcessor serviceProcessor = new ServiceProcessor(env,
122: getWSDLDefinition());
123: serviceProcessor.process(javaModel);
124:
125: SEIAnnotationProcessor seiAnnotationProcessor = new SEIAnnotationProcessor(
126: env);
127: seiAnnotationProcessor.process(javaModel, definition);
128:
129: return javaModel;
130: }
131:
132: private JAXWSBinding customizing(Definition def) {
133: JAXWSBinding binding = CustomizationParser.getInstance()
134: .getDefinitionExtension();
135: if (binding != null) {
136: return binding;
137: }
138:
139: List extElements = def.getExtensibilityElements();
140: if (extElements.size() > 0) {
141: Iterator iterator = extElements.iterator();
142: while (iterator.hasNext()) {
143: Object obj = iterator.next();
144: if (obj instanceof JAXWSBinding) {
145: binding = (JAXWSBinding) obj;
146: }
147: }
148: }
149:
150: if (binding == null) {
151: binding = new JAXWSBinding();
152: }
153: return binding;
154: }
155:
156: private void compile() throws ToolException {
157: ClassCollector classCollector = (ClassCollector) env
158: .get(ToolConstants.GENERATED_CLASS_COLLECTOR);
159: List<String> argList = new ArrayList<String>();
160:
161: String javaClasspath = System.getProperty("java.class.path");
162: // hard code celtix.jar
163: boolean classpathSetted = javaClasspath != null
164: && (javaClasspath.indexOf("celtix.jar") >= 0);
165: if (env.isVerbose()) {
166: argList.add("-verbose");
167: }
168:
169: if (!classpathSetted) {
170: System.setProperty("java.ext.dirs", this .getClass()
171: .getClassLoader().getResource(".").getFile()
172: + "../lib/");
173: } else {
174: argList.add("-classpath");
175: argList.add(javaClasspath);
176: }
177:
178: if (env.get(ToolConstants.CFG_CLASSDIR) != null) {
179: argList.add("-d");
180: argList.add((String) env.get(ToolConstants.CFG_CLASSDIR));
181: }
182:
183: Set<String> dirSet = new HashSet<String>();
184: Iterator ite = classCollector.getGeneratedFileInfo().iterator();
185: while (ite.hasNext()) {
186: String fileName = (String) ite.next();
187: fileName = fileName.replaceAll("\\.", "/");
188: String dirName = fileName.substring(0, fileName
189: .lastIndexOf("/") + 1);
190: String outPutDir = (String) env
191: .get(ToolConstants.CFG_OUTPUTDIR);
192: if (!dirSet.contains(dirName)) {
193: String path = outPutDir + "/" + dirName;
194: dirSet.add(path);
195: File file = new File(path);
196: if (file.isDirectory()) {
197: for (String str : file.list()) {
198: if (str.endsWith("java")) {
199: argList.add(path + File.separator + str);
200: } else {
201: // copy generated xml file or others to class
202: // directory
203: File otherFile = new File(path
204: + File.separator + str);
205: if (otherFile.isFile()
206: && str.toLowerCase()
207: .endsWith("xml")
208: && env
209: .get(ToolConstants.CFG_CLASSDIR) != null) {
210: String targetDir = (String) env
211: .get(ToolConstants.CFG_CLASSDIR);
212:
213: File targetFile = new File(targetDir
214: + File.separator + dirName
215: + File.separator + str);
216: copyXmlFile(otherFile, targetFile);
217: }
218: }
219: }
220: }
221: }
222:
223: }
224:
225: String[] args = new String[argList.size()];
226: int i = 0;
227: for (Object obj : argList.toArray()) {
228: String arg = (String) obj;
229: args[i] = arg;
230: i++;
231: }
232:
233: Compiler compiler = new Compiler(System.out);
234:
235: if (!compiler.internalCompile(args)) {
236: Message msg = new Message("FAIL_TO_COMPILE_GENERATE_CODES",
237: LOG);
238: throw new ToolException(msg);
239: }
240:
241: }
242:
243: private void copyXmlFile(File from, File to) throws ToolException {
244:
245: try {
246: String dir = to.getCanonicalPath().substring(0,
247: to.getCanonicalPath().lastIndexOf(File.separator));
248: File dirFile = new File(dir);
249: dirFile.mkdirs();
250: FileInputStream input = new FileInputStream(from);
251: FileOutputStream output = new FileOutputStream(to);
252: byte[] b = new byte[1024 * 3];
253: int len = 0;
254: while (len != -1) {
255: len = input.read(b);
256: if (len != -1) {
257: output.write(b, 0, len);
258: }
259: }
260: output.flush();
261: output.close();
262: input.close();
263: } catch (Exception e) {
264: Message msg = new Message(
265: "FAIL_TO_COPY_GENERATED_RESOURCE_FILE", LOG);
266: throw new ToolException(msg, e);
267: }
268: }
269:
270: private boolean isSOAP12Binding(Definition def) {
271: String namespace = "";
272: for (Iterator ite = def.getNamespaces().values().iterator(); ite
273: .hasNext();) {
274: namespace = (String) ite.next();
275: if (namespace != null
276: && namespace.toLowerCase().indexOf(
277: "http://schemas.xmlsoap.org/wsdl/soap12") >= 0) {
278: return true;
279: }
280: }
281: return false;
282: }
283:
284: }
|