KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > actions > ws > GenerateWSClientActionDelegate


1 /*
2  * Created on 29 juil. 2004
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */

7 package com.bull.eclipse.jonas.actions.ws;
8
9 import java.io.File JavaDoc;
10 import java.io.FileWriter JavaDoc;
11 import java.io.IOException JavaDoc;
12
13 import org.apache.velocity.VelocityContext;
14 import org.apache.velocity.app.VelocityEngine;
15 import org.apache.velocity.exception.MethodInvocationException;
16 import org.apache.velocity.exception.ParseErrorException;
17 import org.apache.velocity.exception.ResourceNotFoundException;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.jdt.core.IPackageFragment;
20
21 import com.bull.eclipse.jonas.JonasLauncherPlugin;
22 import com.bull.eclipse.jonas.JonasProject;
23 import com.bull.eclipse.jonas.actions.JonasPackageAbstractActionDelegate;
24 import com.bull.eclipse.jonas.utils.AntUtils;
25 import com.bull.eclipse.jonas.utils.EjbManagement;
26 import com.bull.eclipse.jonas.utils.TemplateDirUtil;
27
28 /**
29  * @author riase
30  *
31  * TODO To change the template for this generated type comment go to
32  * Window - Preferences - Java - Code Style - Code Templates
33  */

34 public class GenerateWSClientActionDelegate extends JonasPackageAbstractActionDelegate {
35     private VelocityEngine vEngine = null;
36     private VelocityContext vContext = null;
37
38     
39     public boolean doActionOn(IPackageFragment pack) throws Exception JavaDoc {
40         // Update build.properties with JONAS_ROOT
41

42         JonasProject jp = (JonasProject)pack.getJavaProject().getProject().getNature(JonasLauncherPlugin.NATURE_ID);
43         File JavaDoc wsSrcDir = jp.getProject().getLocation().append("webservicessrc").toFile();
44         boolean createDir = true;
45         if (!wsSrcDir.exists()) {
46             createDir = wsSrcDir.mkdir();
47             jp.setSrcAsSourceFolder("webservicessrc");
48         }
49         if (!createDir)
50             throw new IOException JavaDoc("Error until creation web services directory");
51
52         String JavaDoc buildPath = pack.getPath().append("build.xml").toOSString();
53         
54         AntUtils.runAnt(buildPath, "wsdl2java");
55         
56         generateTestWS(jp, pack.getElementName());
57         
58         jp.getProject().refreshLocal( IResource.DEPTH_INFINITE, null );
59             
60         return false;
61     }
62     
63     private void generateTestWS(JonasProject jp, String JavaDoc packEjb) {
64         vContext = new VelocityContext();
65
66         String JavaDoc pkgName = packEjb + ".ws.client";
67         vContext.put("pkgName", pkgName);
68         String JavaDoc pkgNameWithSepar = packEjb.replace('.', File.separatorChar);
69         JonasLauncherPlugin.log("pkgName " + pkgNameWithSepar);
70         String JavaDoc ejbname = EjbManagement.getEjbName(pkgNameWithSepar,jp);
71         JonasLauncherPlugin.log("ejbName " + ejbname);
72         String JavaDoc beanname = ejbname.substring(0,1).toUpperCase() + ejbname.substring(1);
73         JonasLauncherPlugin.log("beanname " + beanname);
74         vContext.put("beanname", ejbname);
75         vContext.put("beanName", beanname);
76         
77
78         // Generates webapps files
79
try {
80             vEngine = new VelocityEngine();
81             vEngine.setProperty(VelocityEngine.VM_LIBRARY, "");
82             vEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "file");
83             vEngine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH,
84                             TemplateDirUtil.getTemplateDir().concat("/ws"));
85             try {
86                 vEngine.init();
87             } catch (Exception JavaDoc e) {
88                 e.printStackTrace();
89             }
90             String JavaDoc target = jp.getProject().getLocation().append("webservicessrc").append(pkgNameWithSepar).append("ws").append("client").append("TestWS" + beanname + ".java").toOSString();
91             JonasLauncherPlugin.log("Path = " + target);
92             generate("TestWS.vm", target);
93             jp.getProject().refreshLocal( IResource.DEPTH_INFINITE, null );
94         } catch (Exception JavaDoc e) {
95             e.printStackTrace();
96         }
97
98     }
99     
100      private void generate(String JavaDoc templateFileName, String JavaDoc targetFileName)
101             throws Exception JavaDoc, IOException JavaDoc, ResourceNotFoundException,
102             ParseErrorException, MethodInvocationException {
103         FileWriter JavaDoc fileWriter = null;
104         fileWriter = new FileWriter JavaDoc(targetFileName);
105         vEngine.mergeTemplate(templateFileName, vContext, fileWriter);
106         fileWriter.close();
107     }
108
109 }
110
Popular Tags