1 5 package com.bull.eclipse.newear; 6 7 import java.io.File ; 8 import java.io.FileWriter ; 9 import java.io.IOException ; 10 import java.util.Enumeration ; 11 import java.util.Vector ; 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.IFile; 19 import org.eclipse.core.resources.IFolder; 20 import org.eclipse.core.resources.IResource; 21 import org.eclipse.core.runtime.CoreException; 22 23 import com.bull.eclipse.jonas.JonasLauncherPlugin; 24 import com.bull.eclipse.jonas.JonasProject; 25 import com.bull.eclipse.jonas.utils.AntUtils; 26 import com.bull.eclipse.jonas.utils.EjbManagement; 27 import com.bull.eclipse.jonas.utils.FileUtil; 28 import com.bull.eclipse.jonas.utils.RarManagement; 29 import com.bull.eclipse.jonas.utils.TemplateDirUtil; 30 31 36 public class NewEarRunner 37 { 38 private IFolder pkgfolder; private JonasProject project; 41 private Vector ejbListe; 42 private Vector webListe; 43 private Vector rarListe; 44 private VelocityEngine vEngine = null; 45 private VelocityContext vContext = null; 46 47 private static final int EXIT_SUCCESS = 0; 48 private static final int EXIT_FAILURE = 1; 49 50 51 public NewEarRunner( JonasProject proj, Vector ejbListe, Vector webListe, Vector rarListe) 52 { 53 project = proj; 54 this.ejbListe = ejbListe; 55 this.webListe = webListe; 56 this.rarListe = rarListe; 57 } 58 59 62 private void copyFileToEarDirectory( File f ) 63 { 64 IFile xmlfile = project.getProject().getFile( f.getName() ); 65 try{ FileUtil.copy( f, xmlfile.getLocation().toFile() ); } 66 catch( IOException e ) { 67 JonasLauncherPlugin.log( 68 "JOPE: exception copying file " + f.getName() 69 + " to project " + project.getProject().getName() + ": " + e ); 70 } 71 } 72 73 private boolean myDelete( File f ) 74 { 75 boolean b = f.delete(); 76 int retryCount = 5; 77 while( !b && retryCount-->0 ) { 78 try{ java.lang.Thread.sleep(1000); } catch(Exception e) {} 79 b = f.delete(); 80 } 81 return b; 82 } 83 84 public void RunNewear() 85 { 86 vContext = new VelocityContext(); 87 88 String jonasRoot = JonasLauncherPlugin.getDefault().getJonasDir(); 89 String earName = project.getProject().getName() + ".ear"; 90 91 vContext.put("earName",earName); 92 93 if (!webListe.isEmpty()) { 94 String warContext = project.getWebContext(); 95 vContext.put("warContext",warContext); 96 vContext.put("webDir",project.getWebdirName()); 97 } 98 if (!ejbListe.isEmpty()) { 99 vContext.put("ejbListe",ejbListe); 100 vContext.put("pkgListe",EjbManagement.getPackageNames(ejbListe,project)); 101 } 102 103 if (!rarListe.isEmpty()) { 104 vContext.put("rarListe",rarListe); 105 } 106 107 vContext.put("outputEAR",project.getProject().getLocation().toOSString()); 108 vContext.put("srcEjbDir", project.getProject().getLocation().toOSString() + File.separator + project.getSrcEJBDir()); 109 110 File outputDir = new File (project.getProject().getLocation().toOSString()); 111 112 113 try { 115 vEngine = new VelocityEngine(); 116 vEngine.setProperty(VelocityEngine.VM_LIBRARY, ""); 117 vEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "file"); 118 vEngine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, 119 TemplateDirUtil.getTemplateDir().concat("/newear")); 120 try { 121 vEngine.init(); 122 } catch (Exception e) { 123 fatalError("unable to initilise Velocity engine (" + e + ")"); 124 } 125 generate("build.vm", outputDir + "/build.xml"); 126 generate("application.vm", outputDir + "/application.xml"); 127 System.out.println("Your build.xml file for ear have been created. You can now customize them."); 128 } catch (Exception e) { 129 error(e.toString()); 130 } 131 try{ project.getProject().refreshLocal( IResource.DEPTH_INFINITE, null ); } 133 catch( CoreException e ) {} 134 135 if (!rarListe.isEmpty()) { 136 Enumeration ele = rarListe.elements(); 137 String pathRars = project.getProject().getLocation().append("/resourceAdapter").toOSString(); 138 while (ele.hasMoreElements()) { 139 try { 140 RarManagement.buildRarFile(pathRars + "/" + ele.nextElement(),project.getProject()); 141 } catch (IOException ioe) { 142 JonasLauncherPlugin.log("Problems when creating the rars files"); 143 } 144 } 145 } 146 147 AntUtils.runAnt(project.getProject().getFullPath().append("/build.xml").toOSString()); 148 } 149 150 public void setPkgFolder( IFolder f ) 151 { 152 pkgfolder = f; 153 } 154 155 159 public static String [] concat( String [] s1, String s2, String s3 ) 160 { 161 String [] full = new String [ s1.length + 2 ]; 162 System.arraycopy( s1, 0, full, 0, s1.length ); 163 full[ full.length-2 ] = s2; 164 full[ full.length-1 ] = s3; 165 return full; 166 } 167 168 172 public static String [] concat( String [] s1, String s2 ) 173 { 174 String [] full = new String [ s1.length + 1 ]; 175 System.arraycopy( s1, 0, full, 0, s1.length ); 176 full[ full.length-1 ] = s2; 177 return full; 178 } 179 180 static void fatalError(String errMsg) { 181 System.err.println("NewBean fatal error: " + errMsg); 182 System.exit(EXIT_FAILURE); 183 } 184 185 190 private void generate(String templateFileName, 191 String targetFileName) throws Exception , IOException , ResourceNotFoundException, ParseErrorException, MethodInvocationException { 192 FileWriter fileWriter = null; 193 fileWriter = new FileWriter (targetFileName); 194 vEngine.mergeTemplate(templateFileName, vContext, fileWriter); 195 fileWriter.close(); 196 } 197 198 202 static void error(String errMsg) { 203 System.err.println("NewBean error: " + errMsg); 204 } 205 206 207 } 208 | Popular Tags |