1 5 6 package com.bull.eclipse.newbean; 7 8 import java.io.File ; 9 import java.io.FileWriter ; 10 import java.io.IOException ; 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.FileUtil; 26 import com.bull.eclipse.jonas.utils.TemplateDirUtil; 27 import com.bull.eclipse.jonas.wizard.CreateEJBWizardPage; 28 29 30 35 public class NewbeanRunner 36 { 37 private JonasProject jonasNature; 38 private IFolder pkgfolder; private JonasProject project; 40 41 private VelocityEngine vEngine = null; 42 private VelocityContext vContext = null; 43 44 private static final int EXIT_SUCCESS = 0; 45 private static final int EXIT_FAILURE = 1; 46 47 private final Integer SESSION_BEAN = new Integer (0); 48 private final Integer ENTITY_BEAN = new Integer (1); 49 private final Integer MESSAGE_DRIVEN_BEAN = new Integer (2); 50 private final Integer BMP = new Integer (0); 51 private final Integer CMP1 = new Integer (1); 52 private final Integer CMP2 = new Integer (2); 53 private final Integer STATELESS = new Integer (0); 54 private final Integer STATEFUL = new Integer (1); 55 private final Boolean FALSE = Boolean.FALSE; 56 private final Boolean TRUE = Boolean.TRUE; 57 58 59 60 61 62 public NewbeanRunner( JonasProject proj ) 63 { 64 project = proj; 65 } 66 67 71 private void copyFileToPackage( File f, String p ) 72 { 73 IFile javafile = pkgfolder.getFile( f.getName() ); 74 if( javafile.getLocation().toFile().exists() ) { 75 JonasLauncherPlugin.log( 76 "JOPE: file " + p + "/" + f.getName() 77 + " already exists in project " + project.getProject().getName() ); 78 } 79 else { 80 try{ FileUtil.copy( f, javafile.getLocation().toFile() ); } 81 catch( IOException e ) { 82 JonasLauncherPlugin.log( 83 "JOPE: exception copying file " + p + "/" + f.getName() 84 + " to project " + project.getProject().getName() + ": " + e ); 85 } 86 } 87 } 88 89 private void copyFileToComponents(File f) 90 { 91 File javafile = project.getProject().getLocation().append(project.getSrcEJBDir()).append("build.xml").toFile(); 92 try{ FileUtil.copy( f, javafile ); } 93 catch( IOException e ) { 94 JonasLauncherPlugin.log( 95 "JOPE: exception copying file " ); 96 } 97 } 98 99 102 private void copyFileToProject( File f ) 103 { 104 IFile xmlfile = project.getProject().getFile( f.getName() ); 105 if( xmlfile.getLocation().toFile().exists() ) { 106 JonasLauncherPlugin.log("JOPE: file " + f.getName() 107 + " already exists in project " + project.getProject().getName() ); 108 } 109 else { 110 try{ FileUtil.copy( f, xmlfile.getLocation().toFile() ); } 111 catch( IOException e ) { 112 JonasLauncherPlugin.log( 113 "JOPE: exception copying file " + f.getName() 114 + " to project " + project.getProject().getName() + ": " + e ); 115 } 116 } 117 } 118 119 private boolean myDelete( File f ) 120 { 121 boolean b = f.delete(); 122 int retryCount = 5; 123 while( !b && retryCount-->0 ) { 124 try{ java.lang.Thread.sleep(1000); } catch(Exception e) {} 125 b = f.delete(); 126 } 127 return b; 128 } 129 130 public void RunNewbean( CreateEJBWizardPage EjbPage ) 131 { 132 String jonasRoot = JonasLauncherPlugin.getDefault().getJonasDir(); 133 vContext = new VelocityContext(); 134 135 String beanName = EjbPage.getEJBName(); 136 vContext.put("beanName",beanName); 137 String pkgName = EjbPage.getPkgName(); 138 vContext.put("pkgName",pkgName); 139 String jarName = EjbPage.getJarName(); 140 Integer beanFlavor = null; 141 if( jarName.length()>0 ) 142 vContext.put("jarName",jarName); 143 String s = EjbPage.getEJBType(); 144 if( s.equals(CreateEJBWizardPage.STATEFUL) ) { 145 vContext.put("beanType","F"); 146 vContext.put("sessionType",STATEFUL); 147 beanFlavor = SESSION_BEAN; 148 vContext.put("beanFlavor", beanFlavor); 149 } else 150 if( s.equals(CreateEJBWizardPage.STATELESS) ) { 151 vContext.put("beanType","L"); 152 vContext.put("sessionType",STATELESS); 153 beanFlavor = SESSION_BEAN; 154 vContext.put("beanFlavor", beanFlavor); 155 } else 156 if( s.equals(CreateEJBWizardPage.ENTITYb) ) { 157 vContext.put("beanType","E"); 158 vContext.put("sessionType","B"); 159 beanFlavor = ENTITY_BEAN; 160 vContext.put("beanFlavor", beanFlavor); 161 vContext.put("persistenceManager", BMP); 162 } else 163 if( s.equals(CreateEJBWizardPage.ENTITYc1) ) { 164 vContext.put("beanType","E"); 165 vContext.put("sessionType","C"); 166 beanFlavor = ENTITY_BEAN; 167 vContext.put("beanFlavor", beanFlavor); 168 vContext.put("persistenceManager", CMP1); 169 } else 170 if( s.equals(CreateEJBWizardPage.ENTITYc2) ) { 171 vContext.put("beanType","E"); 172 vContext.put("sessionType","C2"); 173 beanFlavor = ENTITY_BEAN; 174 vContext.put("beanFlavor", beanFlavor); 175 vContext.put("persistenceManager", CMP2); 176 } else 177 if( s.equals(CreateEJBWizardPage.MDB) ) { 178 vContext.put("beanType","MD"); 179 beanFlavor = MESSAGE_DRIVEN_BEAN; 180 vContext.put("beanFlavor", beanFlavor); 181 } 182 s = EjbPage.getPrimaryKey(); 183 if( s.equals(CreateEJBWizardPage.STRING) ) { 184 vContext.put("primaryKey","S"); 185 vContext.put("pkClass", "java.lang.String"); 186 } else 187 if( s.equals(CreateEJBWizardPage.INTEGER) ) { 188 vContext.put("primaryKey","I"); 189 vContext.put("pkClass", "java.lang.Integer"); 190 } else 191 if( s.equals(CreateEJBWizardPage.OBJECT) ) { 192 vContext.put("primaryKey","O"); 193 vContext.put("pkClass", "java.lang.Object"); 194 } 195 s = EjbPage.getInterfaceName(); 196 if( s.equals(CreateEJBWizardPage.LOCAL) ) { 197 vContext.put("location","L"); 198 vContext.put("isLocal", TRUE); 199 } else 200 if( s.equals(CreateEJBWizardPage.REMOTE) ) { 201 vContext.put("location","R"); 202 vContext.put("isLocal", FALSE); 203 } 204 205 206 vContext.put("SESSION_BEAN", SESSION_BEAN); 207 vContext.put("ENTITY_BEAN", ENTITY_BEAN); 208 vContext.put("MESSAGE_DRIVEN_BEAN", MESSAGE_DRIVEN_BEAN); 209 vContext.put("STATELESS", STATELESS); 210 vContext.put("STATEFUL", STATEFUL); 211 vContext.put("CMP1", CMP1); 212 vContext.put("CMP2", CMP2); 213 vContext.put("BMP", BMP); 214 215 216 217 vContext.put("jonasroot",jonasRoot); 218 vContext.put("jonasbase",JonasLauncherPlugin.getDefault().getBaseDir()); 219 vContext.put("outputdirectory",project.getOutputFolder()); 220 vContext.put("outputJAR",project.getProject().getLocation().toOSString()); 221 vContext.put("projectName",project.getProject().getName()); 222 223 String fragment = "../.."; 225 for (int i=0;i<pkgName.length();i++) { 226 if (pkgName.charAt(i) == ".".charAt(0)) { 227 JonasLauncherPlugin.log("On passe dans if"); 228 fragment = fragment.concat("/.."); 229 } 230 } 231 232 vContext.put("relatifProjet",fragment); 233 vContext.put("pkgNameJava",pkgName); 234 235 pkgName = pkgName.replace(".".charAt(0),File.separatorChar); 236 237 Vector pkgListe = project.getPackageProjectFolder(); 238 239 try { 241 vEngine = new VelocityEngine(); 242 vEngine.setProperty(VelocityEngine.VM_LIBRARY, ""); 243 vEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "file"); 244 vEngine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, 245 jonasRoot.concat("/templates/newbean")); 246 247 try { 248 vEngine.init(); 249 } catch (Exception e) { 250 fatalError("unable to initilise Velocity engine (" + e + ")"); 251 } 252 253 Integer MESSAGE_DRIVEN_BEAN = 254 (Integer )vContext.get("MESSAGE_DRIVEN_BEAN"); 255 Integer beanFlavorInVC = (Integer )vContext.get("beanFlavor"); 256 257 boolean isClientToGenerate = true; 258 vContext.put("beanType","EJB"); 259 File fileGeneration = new File (project.getProject().getLocation().append(project.getSrcEJBDir()).append(pkgName).toOSString()); 260 generate("ejb-jar.vm", fileGeneration + "/" + jarName + ".xml"); 261 generate("jonas-ejb-jar.vm", fileGeneration + "/" + "jonas-" + jarName + ".xml"); 262 if (beanFlavorInVC != MESSAGE_DRIVEN_BEAN) { 263 String local = ""; 264 Boolean isLocal = (Boolean )vContext.get("isLocal"); 265 if (isLocal.booleanValue()) { 266 local = "Local"; 267 isClientToGenerate = false; 268 } 269 generate("remote.vm", fileGeneration + "/" + beanName + local + ".java"); 270 generate("home.vm", fileGeneration + "/" + beanName + local + "Home.java"); 271 } 272 vEngine = new VelocityEngine(); 273 vEngine.setProperty(VelocityEngine.VM_LIBRARY, ""); 274 vEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "file"); 275 vEngine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, 276 TemplateDirUtil.getTemplateDir().concat("/newbean")); 277 try { 278 vEngine.init(); 279 } catch (Exception e) { 280 fatalError("unable to initilise Velocity engine (" + e + ")"); 281 } 282 283 vContext.put("beanType","EJB"); 284 generate("bean.vm", fileGeneration + "/" + beanName + "EJB" + ".java"); 285 if (isClientToGenerate) { 286 generate("client.vm", fileGeneration + "/" + beanName + "Client.java"); 287 } 288 generate("build.vm", fileGeneration + "/" + "build.xml"); 289 vContext.put("pkgListe", pkgListe); 290 generate("buildgene.vm", project.getProject().getLocation().append(project.getSrcEJBDir()).append("build.xml").toOSString()); 291 System.out.println("Your bean files have been created. You can now customize them."); 292 293 } catch (Exception e) { 294 error(e.toString()); 295 } 296 297 298 299 try{ 301 project.setPackageProjectFolder(pkgName); 302 project.setJarEjbName(EjbPage.getJarName()); 303 project.getProject().refreshLocal( IResource.DEPTH_INFINITE, null ); 305 } 306 catch( CoreException e ) {} 307 308 } 309 310 public void setNature( JonasProject nature ) 311 { 312 jonasNature = nature; 313 } 314 315 public void setPkgFolder( IFolder f ) 316 { 317 pkgfolder = f; 318 } 319 320 324 public static String [] concat( String [] s1, String s2, String s3 ) 325 { 326 String [] full = new String [ s1.length + 2 ]; 327 System.arraycopy( s1, 0, full, 0, s1.length ); 328 full[ full.length-2 ] = s2; 329 full[ full.length-1 ] = s3; 330 return full; 331 } 332 333 337 public static String [] concat( String [] s1, String s2 ) 338 { 339 String [] full = new String [ s1.length + 1 ]; 340 System.arraycopy( s1, 0, full, 0, s1.length ); 341 full[ full.length-1 ] = s2; 342 return full; 343 } 344 345 350 private void generate(String templateFileName, 351 String targetFileName) throws Exception , IOException , ResourceNotFoundException, ParseErrorException, MethodInvocationException { 352 FileWriter fileWriter = null; 353 fileWriter = new FileWriter (targetFileName); 354 vEngine.mergeTemplate(templateFileName, vContext, fileWriter); 355 fileWriter.close(); 356 } 357 358 359 363 static void error(String errMsg) { 364 System.err.println("NewBean error: " + errMsg); 365 } 366 367 368 373 static void fatalError(String errMsg) { 374 System.err.println("NewBean fatal error: " + errMsg); 375 System.exit(EXIT_FAILURE); 376 } 377 378 } 379 | Popular Tags |