1 5 package com.bull.eclipse.newwebapp; 6 7 import java.io.File ; 8 import java.io.FileWriter ; 9 import java.io.IOException ; 10 11 import org.apache.velocity.VelocityContext; 12 import org.apache.velocity.app.VelocityEngine; 13 import org.apache.velocity.exception.MethodInvocationException; 14 import org.apache.velocity.exception.ParseErrorException; 15 import org.apache.velocity.exception.ResourceNotFoundException; 16 import org.eclipse.core.resources.IFile; 17 import org.eclipse.core.resources.IFolder; 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.runtime.CoreException; 20 21 import com.bull.eclipse.jonas.JonasLauncherPlugin; 22 import com.bull.eclipse.jonas.JonasProject; 23 import com.bull.eclipse.jonas.utils.FileUtil; 24 import com.bull.eclipse.jonas.utils.TemplateDirUtil; 25 26 31 public class NewWebappsRunner 32 { 33 private static final int EXIT_SUCCESS = 0; 34 private static final int EXIT_FAILURE = 1; 35 36 private IFolder pkgfolder; private JonasProject project; 39 40 private VelocityEngine vEngine = null; 41 private VelocityContext vContext = null; 42 43 44 public NewWebappsRunner( JonasProject proj ) 45 { 46 project = proj; 47 } 48 49 52 private void copyFileToWebDirectory( File f ) 53 { 54 IFile xmlfile = project.getWebDirFolder().getFile( f.getName() ); 55 System.out.println("On est dans copy file to project"); 56 if( xmlfile.getLocation().toFile().exists() ) { 57 JonasLauncherPlugin.log("JOPE: file " + f.getName() 58 + " already exists in project " + project.getProject().getName() ); 59 } 60 else { 61 System.out.println("On est dans else avec xmlfile = " + xmlfile); 62 System.out.println("On est dans else avec f = " + f); 63 try{ FileUtil.copy( f, xmlfile.getLocation().toFile() ); } 64 catch( IOException e ) { 65 JonasLauncherPlugin.log( 66 "JOPE: exception copying file " + f.getName() 67 + " to project " + project.getProject().getName() + ": " + e ); 68 } 69 } 70 } 71 72 private boolean myDelete( File f ) 73 { 74 boolean b = f.delete(); 75 int retryCount = 5; 76 while( !b && retryCount-->0 ) { 77 try{ java.lang.Thread.sleep(1000); } catch(Exception e) {} 78 b = f.delete(); 79 } 80 return b; 81 } 82 83 public void RunNewwebapps() 84 { 85 String jonasRoot = JonasLauncherPlugin.getDefault().getJonasDir(); 86 87 vContext = new VelocityContext(); 88 String templateDir = TemplateDirUtil.getTemplateDir() + "/" + "newwebapp"; 89 vContext.put("jonasroot", jonasRoot); 90 vContext.put("jonasbase", JonasLauncherPlugin.getDefault().getBaseDir()); 91 vContext.put("outputdirectory",project.getOutputFolder()); 92 vContext.put("outputWAR",project.getProject().getLocation().toOSString()); 93 vContext.put("projectName",project.getProject().getName()); 94 String webappsName = project.getWebContext() + ".war"; 95 vContext.put("webappsName",webappsName); 96 vContext.put("webresourcesdir",project.getWarLocation()); 97 98 System.out.println("Creating webapps " 99 + webappsName 100 + " resourceDir " 101 + project.getWarLocation() + "templateDir " + templateDir); 102 103 vEngine = new VelocityEngine(); 104 vEngine.setProperty(VelocityEngine.VM_LIBRARY, ""); 105 vEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "file"); 106 vEngine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, 107 templateDir); 108 109 try { 110 vEngine.init(); 111 } catch (Exception e) { 112 fatalError("unable to initilise Velocity engine (" + e + ")"); 113 } 114 115 File outputDir = new File (project.getProject().getLocation().append(project.getWebdirName()).toOSString()); 116 JonasLauncherPlugin.log("Directory = " + outputDir); 117 118 try { 119 generate("build.vm", outputDir + "/build.xml"); 120 } catch (Exception e) { 121 e.printStackTrace(); 122 } 123 124 125 126 try{ project.getProject().refreshLocal( IResource.DEPTH_INFINITE, null ); } 128 catch( CoreException e ) { 129 e.printStackTrace(); 130 } 131 132 } 133 134 140 public void setPkgFolder( IFolder f ) 141 { 142 pkgfolder = f; 143 } 144 145 149 public static String [] concat( String [] s1, String s2, String s3 ) 150 { 151 String [] full = new String [ s1.length + 2 ]; 152 System.arraycopy( s1, 0, full, 0, s1.length ); 153 full[ full.length-2 ] = s2; 154 full[ full.length-1 ] = s3; 155 return full; 156 } 157 158 162 public static String [] concat( String [] s1, String s2 ) 163 { 164 String [] full = new String [ s1.length + 1 ]; 165 System.arraycopy( s1, 0, full, 0, s1.length ); 166 full[ full.length-1 ] = s2; 167 return full; 168 } 169 170 175 static void fatalError(String errMsg) { 176 System.err.println("NewWebapps fatal error: " + errMsg); 177 System.exit(EXIT_FAILURE); 178 } 179 180 185 private void generate(String templateFileName, 186 String targetFileName) throws Exception , IOException , ResourceNotFoundException, ParseErrorException, MethodInvocationException { 187 FileWriter fileWriter = null; 188 fileWriter = new FileWriter (targetFileName); 189 vEngine.mergeTemplate(templateFileName, vContext, fileWriter); 190 fileWriter.close(); 191 } 192 193 } 194 | Popular Tags |