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: */
019:
020: package org.apache.axis2.tool.codegen.eclipse;
021:
022: import java.io.File;
023: import java.io.FileInputStream;
024: import java.io.FileOutputStream;
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.io.OutputStream;
028: import java.lang.reflect.InvocationTargetException;
029: import java.util.HashMap;
030: import java.util.Map;
031:
032: import javax.wsdl.Definition;
033:
034: import org.apache.axis2.description.AxisService;
035: import org.apache.axis2.description.java2wsdl.Java2WSDLConstants;
036: import org.apache.axis2.tool.codegen.WSDL2JavaGenerator;
037: import org.apache.axis2.tool.codegen.eclipse.plugin.CodegenWizardPlugin;
038: import org.apache.axis2.tool.codegen.eclipse.ui.AbstractWizardPage;
039: import org.apache.axis2.tool.codegen.eclipse.ui.JavaSourceSelectionPage;
040: import org.apache.axis2.tool.codegen.eclipse.ui.JavaWSDLOptionsPage;
041: import org.apache.axis2.tool.codegen.eclipse.ui.JavaWSDLOutputLocationPage;
042: import org.apache.axis2.tool.codegen.eclipse.ui.OptionsPage;
043: import org.apache.axis2.tool.codegen.eclipse.ui.OutputPage;
044: import org.apache.axis2.tool.codegen.eclipse.ui.ToolSelectionPage;
045: import org.apache.axis2.tool.codegen.eclipse.ui.WSDLFileSelectionPage;
046: import org.apache.axis2.tool.codegen.eclipse.util.SettingsConstants;
047: import org.apache.axis2.tool.codegen.eclipse.util.UIConstants;
048: import org.apache.axis2.tool.codegen.eclipse.util.WSDLPropertyReader;
049: import org.apache.axis2.tool.core.JarFileWriter;
050: import org.apache.axis2.tool.core.SrcCompiler;
051: import org.apache.axis2.util.CommandLineOptionConstants;
052: import org.apache.axis2.wsdl.codegen.CodeGenConfiguration;
053: import org.apache.axis2.wsdl.codegen.CodeGenerationEngine;
054: import org.apache.ws.java2wsdl.Java2WSDLCodegenEngine;
055: import org.apache.ws.java2wsdl.utils.Java2WSDLCommandLineOption;
056: import org.eclipse.core.resources.IWorkspace;
057: import org.eclipse.core.resources.ResourcesPlugin;
058: import org.eclipse.core.runtime.CoreException;
059: import org.eclipse.core.runtime.IProgressMonitor;
060: import org.eclipse.core.runtime.NullProgressMonitor;
061: import org.eclipse.jface.dialogs.MessageDialog;
062: import org.eclipse.jface.viewers.IStructuredSelection;
063: import org.eclipse.jface.wizard.IWizardPage;
064: import org.eclipse.jface.wizard.Wizard;
065: import org.eclipse.ui.INewWizard;
066: import org.eclipse.ui.IWorkbench;
067: import org.eclipse.ui.IWorkbenchWizard;
068: import org.eclipse.ui.actions.WorkspaceModifyOperation;
069:
070: /**
071: * The main wizard for the codegen wizard
072: */
073:
074: public class CodeGenWizard extends Wizard implements INewWizard,
075: Java2WSDLConstants {
076: private ToolSelectionPage toolSelectionPage;
077:
078: private WSDLFileSelectionPage wsdlSelectionPage;
079:
080: private OptionsPage optionsPage;
081:
082: private OutputPage outputPage;
083:
084: private JavaWSDLOptionsPage java2wsdlOptionsPage;
085:
086: private JavaSourceSelectionPage javaSourceSelectionPage;
087:
088: private JavaWSDLOutputLocationPage java2wsdlOutputLocationPage;
089:
090: private int selectedWizardType = SettingsConstants.WSDL_2_JAVA_TYPE;//TODO change this
091:
092: private int selectedCodegenOptionType = SettingsConstants.CODEGEN_DEFAULT_TYPE;//TODO change this
093:
094: /**
095: * Constructor for CodeGenWizard.
096: */
097: public CodeGenWizard() {
098: super ();
099: setNeedsProgressMonitor(true);
100: this
101: .setWindowTitle(org.apache.axis2.tool.codegen.eclipse.plugin.CodegenWizardPlugin
102: .getResourceString("general.name"));
103: }
104:
105: /**
106: * Adding the page to the wizard.
107: */
108:
109: public void addPages() {
110: toolSelectionPage = new ToolSelectionPage();
111: addPage(toolSelectionPage);
112:
113: //add the wsdl2java wizard pages
114: wsdlSelectionPage = new WSDLFileSelectionPage();
115: addPage(wsdlSelectionPage);
116:
117: optionsPage = new OptionsPage();
118: addPage(optionsPage);
119: outputPage = new OutputPage();
120: addPage(outputPage);
121:
122: //add java2wsdl wizard pages
123: javaSourceSelectionPage = new JavaSourceSelectionPage();
124: addPage(javaSourceSelectionPage);
125: java2wsdlOptionsPage = new JavaWSDLOptionsPage();
126: addPage(java2wsdlOptionsPage);
127: java2wsdlOutputLocationPage = new JavaWSDLOutputLocationPage();
128: addPage(java2wsdlOutputLocationPage);
129:
130: }
131:
132: /*
133: * (non-Javadoc)
134: *
135: * @see org.eclipse.jface.wizard.IWizard#canFinish()
136: */
137: public boolean canFinish() {
138: IWizardPage[] pages = getPages();
139: AbstractWizardPage wizardPage = null;
140: for (int i = 0; i < pages.length; i++) {
141: wizardPage = (AbstractWizardPage) pages[i];
142: if (wizardPage.getPageType() == this .selectedWizardType) {
143: if (!(wizardPage.isPageComplete()))
144: return false;
145: }
146: }
147: return true;
148: }
149:
150: public IWizardPage getNextPage(IWizardPage page) {
151: AbstractWizardPage currentPage = (AbstractWizardPage) page;
152: AbstractWizardPage pageout = (AbstractWizardPage) super
153: .getNextPage(page);
154:
155: while (pageout != null
156: && selectedWizardType != pageout.getPageType()) {
157: AbstractWizardPage temp = pageout;
158: pageout = (AbstractWizardPage) super
159: .getNextPage(currentPage);
160: currentPage = temp;
161: }
162: return pageout;
163: }
164:
165: /**
166: * This method is called when 'Finish' button is pressed in the wizard. We
167: * will create an operation and run it using wizard as execution context.
168: */
169: public boolean performFinish() {
170: try {
171: switch (selectedWizardType) {
172: case SettingsConstants.WSDL_2_JAVA_TYPE:
173: doFinishWSDL2Java();
174: break;
175: case SettingsConstants.JAVA_2_WSDL_TYPE:
176: doFinishJava2WSDL();
177: break;
178: case SettingsConstants.UNSPECIFIED_TYPE:
179: break; //Do nothing
180: default:
181: throw new RuntimeException(CodegenWizardPlugin
182: .getResourceString("general.invalid.state"));
183: }
184: } catch (Exception e) {
185: MessageDialog.openError(getShell(), CodegenWizardPlugin
186: .getResourceString("general.Error"),
187: CodegenWizardPlugin
188: .getResourceString("general.Error.prefix")
189: + e.getMessage());
190: return false;
191: }
192: MessageDialog
193: .openInformation(this .getShell(), CodegenWizardPlugin
194: .getResourceString("general.name"),
195: CodegenWizardPlugin
196: .getResourceString("wizard.success"));
197: return true;
198: }
199:
200: /**
201: * The worker method, generates the code itself.
202: */
203: private void doFinishWSDL2Java() {
204: WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
205: protected void execute(IProgressMonitor monitor)
206: throws CoreException, InvocationTargetException,
207: InterruptedException {
208: if (monitor == null)
209: monitor = new NullProgressMonitor();
210:
211: /*
212: * "3" is the total amount of steps, see below monitor.worked(amount)
213: */
214: monitor.beginTask(CodegenWizardPlugin
215: .getResourceString("generator.generating"), 3);
216:
217: try {
218: /*
219: * TODO: Introduce a progress monitor interface for CodeGenerationEngine.
220: * Since this monitor here doesn't make much sense, we
221: * should either remove the progress monitor from the CodeGenWizard,
222: * or give a (custom) progress monitor to the generate() method, so
223: * we will be informed by Axis2 about the progress of code generation.
224: */
225: WSDL2JavaGenerator generator = new WSDL2JavaGenerator();
226: monitor.subTask(CodegenWizardPlugin
227: .getResourceString("generator.readingWOM"));
228: AxisService service = generator
229: .getAxisService(wsdlSelectionPage
230: .getFileName());
231: monitor.worked(1);
232:
233: //The generate all fix (Axis2-1862)
234: boolean isServerside, isServiceXML, isGenerateServerSideInterface = false;
235: if (optionsPage.getGenerateAll()) {
236: isServerside = true;
237: isServiceXML = true;
238: isGenerateServerSideInterface = true;
239: } else {
240: isServerside = optionsPage.isServerside();
241: isServiceXML = optionsPage.isServerXML();
242: isGenerateServerSideInterface = optionsPage
243: .getGenerateServerSideInterface();
244: }
245: Map optionsMap = generator.fillOptionMap(
246: optionsPage.isAsyncOnlyOn(), optionsPage
247: .isSyncOnlyOn(), isServerside,
248: isServiceXML, optionsPage
249: .isGenerateTestCase(), optionsPage
250: .getGenerateAll(), optionsPage
251: .getServiceName(), optionsPage
252: .getPortName(), optionsPage
253: .getDatabinderName(),
254: wsdlSelectionPage.getFileName(),
255: optionsPage.getPackageName(), optionsPage
256: .getSelectedLanguage(), outputPage
257: .getOutputLocation(), optionsPage
258: .getNs2PkgMapping(),
259: isGenerateServerSideInterface);
260:
261: //Fix for the CodeGenConfiguration Contructor Change
262: //CodeGenConfiguration codegenConfig = new CodeGenConfiguration(service, optionsMap);
263: CodeGenConfiguration codegenConfig = new CodeGenConfiguration(
264: optionsMap);
265: codegenConfig.addAxisService(service);
266:
267: //set the wsdl definision for codegen config for skeleton generarion.
268: WSDLPropertyReader reader = new WSDLPropertyReader();
269: reader.readWSDL(wsdlSelectionPage.getFileName());
270: Definition wsdlDefinition = reader
271: .getWsdlDefinition();
272: codegenConfig.setWsdlDefinition(wsdlDefinition);
273:
274: //set the baseURI
275: codegenConfig
276: .setBaseURI(generator
277: .getBaseUri(wsdlSelectionPage
278: .getFileName()));
279: monitor.worked(1);
280:
281: monitor.subTask(CodegenWizardPlugin
282: .getResourceString("generator.generating"));
283:
284: new CodeGenerationEngine(codegenConfig).generate();
285:
286: //TODO refresh the eclipse project space to show the generated files
287:
288: //Add the codegen libs that are coming with the plugin to the project lib that has been created
289: if (outputPage
290: .getAxis2PluginLibCopyCheckBoxSelection()) {
291: String eclipseHome = System
292: .getProperty("user.dir");
293: String pluginLibLocation = eclipseHome
294: + File.separator
295: + UIConstants.PLUGINS
296: + File.separator
297: + UIConstants.AXIS_CODEGEN_PLUGIN_FOLDER
298: + File.separator + UIConstants.LIB;
299: addLibsToProjectLib(pluginLibLocation,
300: outputPage.getOutputLocation());
301: }
302:
303: //Also another requirement arises
304: //If the codegen project was newly buided project or else the eclipse
305: //project intended to save this generated code does not have the required libs
306: //to compile the generated code. We need to add the relevent libs to a lib directory
307: //of the <code>outputPage.getOutputLocation()</code>
308:
309: //Add the libraries on the plugin lib directory to the created project lib
310: if (outputPage.getAxisLibCopyCheckBoxSelection()
311: && outputPage.oktoLoadLibs()) {
312: // String libDirectory = outputPage.getAxisHomeLocation()+File.separator+
313: // UIConstants.TARGET+File.separator+UIConstants.LIB;
314: String libDirectory = outputPage
315: .getAxisJarsLocation();
316: addLibsToProjectLib(libDirectory, outputPage
317: .getOutputLocation());
318: }
319:
320: //This will Create a jar file from the codegen results and add to the output
321: //locations lib directory
322: if (outputPage.getCreateJarCheckBoxSelection()) {
323: IWorkspace workspace = ResourcesPlugin
324: .getWorkspace();
325: String tempCodegenLocation = workspace
326: .getRoot().getLocation().toString()
327: + File.separator + "codegen";
328: String tempProjectSrcLocation = tempCodegenLocation
329: + File.separator
330: + "codegen_temp_src_"
331: + System.currentTimeMillis();
332: String tempProjectClassLocation = tempCodegenLocation
333: + File.separator
334: + "codegen_temp_class_"
335: + System.currentTimeMillis();
336: File tempCodegenFile = new File(
337: tempCodegenLocation);
338: File tempSrcFile = new File(
339: tempProjectSrcLocation);
340: File tempClassFile = new File(
341: tempProjectClassLocation);
342: tempCodegenFile.mkdir();
343: tempSrcFile.mkdir();
344: tempClassFile.mkdir();
345: copyDirectory(new File(outputPage
346: .getOutputLocation()), tempSrcFile);
347: //Compile the source to another directory
348: SrcCompiler srcCompileTool = new SrcCompiler();
349: srcCompileTool.compileSource(tempClassFile,
350: tempProjectSrcLocation);
351: //create the jar file and add that to the lib directory
352: String projectLib = outputPage
353: .getOutputLocation()
354: + File.separator + "lib";
355: JarFileWriter jarFileWriter = new JarFileWriter();
356: String jarFileName = "CodegenResults.jar";
357: if (!outputPage.getJarFilename().equals("")) {
358: jarFileName = outputPage.getJarFilename();
359: }
360: outputPage.setJarFileName(jarFileName);
361: jarFileWriter.writeJarFile(
362: new File(projectLib), jarFileName,
363: tempClassFile);
364:
365: //Delete the temp folders
366: deleteDir(tempCodegenFile);
367:
368: }
369:
370: monitor.worked(1);
371: } catch (Exception e) {
372: ///////////////////////////////
373: e.printStackTrace();
374: /////////////////////////////
375: throw new InterruptedException(e.getMessage());
376: }
377:
378: monitor.done();
379: }
380: };
381:
382: /*
383: * Start the generation as new Workbench Operation, so the user
384: * can see the progress and, if needed, can stop the operation.
385: */
386: try {
387: getContainer().run(false, true, op);
388: } catch (InvocationTargetException e1) {
389: /////////////////////////
390: e1.printStackTrace();
391: ////////////////////////
392: throw new RuntimeException(e1);
393: } catch (InterruptedException e1) {
394: throw new RuntimeException(e1);
395: } catch (Exception e) {
396: throw new RuntimeException(e);
397: }
398:
399: }
400:
401: private void doFinishJava2WSDL() throws Exception {
402:
403: WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
404: protected void execute(IProgressMonitor monitor) {
405: if (monitor == null)
406: monitor = new NullProgressMonitor();
407:
408: /*
409: * "2" is the total amount of steps, see below
410: * monitor.worked(amount)
411: */
412: monitor.beginTask(CodegenWizardPlugin
413: .getResourceString("generator.generating"), 3);
414:
415: try {
416: monitor.worked(1);
417: //fill the option map
418: Map optionsMap = new HashMap();
419: Java2WSDLCommandLineOption option = new Java2WSDLCommandLineOption(
420: CLASSNAME_OPTION,
421: new String[] { javaSourceSelectionPage
422: .getClassName() });
423: optionsMap.put(CLASSNAME_OPTION, option);
424:
425: option = new Java2WSDLCommandLineOption(
426: CLASSPATH_OPTION, javaSourceSelectionPage
427: .getClassPathList());
428: optionsMap.put(CLASSPATH_OPTION, option);
429:
430: option = new Java2WSDLCommandLineOption(
431: TARGET_NAMESPACE_OPTION,
432: new String[] { java2wsdlOptionsPage
433: .getTargetNamespace() });
434: optionsMap.put(TARGET_NAMESPACE_OPTION, option);
435:
436: option = new Java2WSDLCommandLineOption(
437: TARGET_NAMESPACE_PREFIX_OPTION,
438: new String[] { java2wsdlOptionsPage
439: .getTargetNamespacePrefix() });
440: optionsMap.put(TARGET_NAMESPACE_PREFIX_OPTION,
441: option);
442:
443: option = new Java2WSDLCommandLineOption(
444: SCHEMA_TARGET_NAMESPACE_OPTION,
445: new String[] { java2wsdlOptionsPage
446: .getSchemaTargetNamespace() });
447: optionsMap.put(SCHEMA_TARGET_NAMESPACE_OPTION,
448: option);
449:
450: option = new Java2WSDLCommandLineOption(
451: SERVICE_NAME_OPTION,
452: new String[] { java2wsdlOptionsPage
453: .getServiceName() });
454: optionsMap.put(SERVICE_NAME_OPTION, option);
455:
456: option = new Java2WSDLCommandLineOption(
457: SCHEMA_TARGET_NAMESPACE_PREFIX_OPTION,
458: new String[] { java2wsdlOptionsPage
459: .getSchemaTargetNamespacePrefix() });
460: optionsMap.put(
461: SCHEMA_TARGET_NAMESPACE_PREFIX_OPTION,
462: option);
463:
464: option = new Java2WSDLCommandLineOption(
465: OUTPUT_LOCATION_OPTION,
466: new String[] { java2wsdlOutputLocationPage
467: .getOutputLocation() });
468: optionsMap.put(OUTPUT_LOCATION_OPTION, option);
469:
470: option = new Java2WSDLCommandLineOption(
471: OUTPUT_FILENAME_OPTION,
472: new String[] { java2wsdlOutputLocationPage
473: .getOutputWSDLName() });
474: optionsMap.put(OUTPUT_FILENAME_OPTION, option);
475:
476: monitor.worked(1);
477:
478: new Java2WSDLCodegenEngine(optionsMap).generate();
479:
480: monitor.worked(1);
481:
482: } catch (Throwable e) {
483:
484: throw new RuntimeException(e);
485: }
486:
487: monitor.done();
488: }
489: };
490:
491: try {
492: getContainer().run(false, true, op);
493: } catch (InvocationTargetException e1) {
494: throw new RuntimeException(e1);
495: } catch (InterruptedException e1) {
496: throw new RuntimeException(CodegenWizardPlugin
497: .getResourceString("general.useraborted.state"));
498: } catch (Exception e) {
499: throw new RuntimeException(e);
500: }
501:
502: }
503:
504: /**
505: * We will accept the selection in the workbench to see if we can initialize
506: * from it.
507: *
508: * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
509: */
510: public void init(IWorkbench workbench,
511: IStructuredSelection selection) {
512: //do nothing
513: }
514:
515: /**
516: * @return Returns the selectedWizardType.
517: */
518: public int getSelectedWizardType() {
519: return selectedWizardType;
520: }
521:
522: /**
523: * @param selectedWizardType
524: * The selectedWizardType to set.
525: */
526: public void setSelectedWizardType(int selectedWizardType) {
527: this .selectedWizardType = selectedWizardType;
528: }
529:
530: /**
531: * @return Returns the codegenOptionType.
532: */
533: public int getSelectedCodegenOptionType() {
534: return selectedCodegenOptionType;
535: }
536:
537: /**
538: * @param selectedCodegenOptionType
539: * The selectedCodegenOptionType to set.
540: */
541: public void setSelectedCodegenOptionType(
542: int selectedCodegenOptionType) {
543: this .selectedCodegenOptionType = selectedCodegenOptionType;
544: }
545:
546: /**
547: * Get the selected WSDL from the WSDLselectionpage
548: * @return
549: */
550: public String getWSDLname() {
551: return wsdlSelectionPage.getFileName();
552: }
553:
554: /**
555: * populate the options page. Usually done after reloading the WSDL
556: *
557: */
558: public void populateOptions() {
559: optionsPage.populateParamsFromWSDL();
560: }
561:
562: public void setDefaultNamespaces(String fullyQualifiedClassName) {
563: java2wsdlOptionsPage
564: .setNamespaceDefaults(fullyQualifiedClassName);
565: }
566:
567: private void addLibsToProjectLib(String libDirectory,
568: String outputLocation) {
569: String newOutputLocation = outputLocation + File.separator
570: + UIConstants.LIB;
571: //Create a lib directory; all ancestor directories must exist
572: boolean success = (new File(newOutputLocation)).mkdir();
573: if (!success) {
574: // Directory creation failed
575: }
576: try {
577: copyDirectory(new File(libDirectory), new File(
578: newOutputLocation));
579: } catch (IOException e) {
580: e.printStackTrace();
581: }
582: }
583:
584: // Copies all files under srcDir to dstDir.
585: // If dstDir does not exist, it will be created.
586: public void copyDirectory(File srcDir, File dstDir)
587: throws IOException {
588: if (srcDir.isDirectory()) {
589: if (!dstDir.exists()) {
590: dstDir.mkdir();
591: }
592:
593: String[] children = srcDir.list();
594: for (int i = 0; i < children.length; i++) {
595: copyDirectory(new File(srcDir, children[i]), new File(
596: dstDir, children[i]));
597: }
598: } else {
599: copyFile(srcDir, dstDir);
600: }
601: }
602:
603: // Copies src file to dst file.
604: // If the dst file does not exist, it is created
605: private void copyFile(File src, File dst) throws IOException {
606: InputStream in = new FileInputStream(src);
607: OutputStream out = new FileOutputStream(dst);
608:
609: // Transfer bytes from in to out
610: byte[] buf = new byte[1024];
611: int len;
612: while ((len = in.read(buf)) > 0) {
613: out.write(buf, 0, len);
614: }
615: in.close();
616: out.close();
617: }
618:
619: // Deletes all files and subdirectories under dir.
620: // Returns true if all deletions were successful.
621: // If a deletion fails, the method stops attempting to delete and returns false.
622: private boolean deleteDir(File dir) {
623: if (dir.isDirectory()) {
624: String[] children = dir.list();
625: for (int i = 0; i < children.length; i++) {
626: boolean success = deleteDir(new File(dir, children[i]));
627: if (!success) {
628: return false;
629: }
630: }
631: }
632:
633: // The directory is now empty so delete it
634: return dir.delete();
635: }
636:
637: }
|