1 7 package com.bull.eclipse.jonas.actions.ws; 8 9 import java.io.File ; 10 import java.io.FileWriter ; 11 import java.io.IOException ; 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 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 { 40 42 JonasProject jp = (JonasProject)pack.getJavaProject().getProject().getNature(JonasLauncherPlugin.NATURE_ID); 43 File 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 ("Error until creation web services directory"); 51 52 String 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 packEjb) { 64 vContext = new VelocityContext(); 65 66 String pkgName = packEjb + ".ws.client"; 67 vContext.put("pkgName", pkgName); 68 String pkgNameWithSepar = packEjb.replace('.', File.separatorChar); 69 JonasLauncherPlugin.log("pkgName " + pkgNameWithSepar); 70 String ejbname = EjbManagement.getEjbName(pkgNameWithSepar,jp); 71 JonasLauncherPlugin.log("ejbName " + ejbname); 72 String 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 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 e) { 88 e.printStackTrace(); 89 } 90 String 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 e) { 95 e.printStackTrace(); 96 } 97 98 } 99 100 private void generate(String templateFileName, String targetFileName) 101 throws Exception , IOException , ResourceNotFoundException, 102 ParseErrorException, MethodInvocationException { 103 FileWriter fileWriter = null; 104 fileWriter = new FileWriter (targetFileName); 105 vEngine.mergeTemplate(templateFileName, vContext, fileWriter); 106 fileWriter.close(); 107 } 108 109 } 110 | Popular Tags |