KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > newear > NewEarRunner


1 /*
2  * Created on Sep 22, 2003
3  *
4  */

5 package com.bull.eclipse.newear;
6
7 import java.io.File JavaDoc;
8 import java.io.FileWriter JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.util.Enumeration JavaDoc;
11 import java.util.Vector 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.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 /**
32  * Interface with the JOnAS 'newbean' functionality.
33  * @author steve krupanszky
34  *
35  */

36 public class NewEarRunner
37 {
38     //private JonasProject jonasNature;
39
private IFolder pkgfolder; // the folder in which the java files will go
40
private JonasProject project;
41     private Vector JavaDoc ejbListe;
42     private Vector JavaDoc webListe;
43     private Vector JavaDoc 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 JavaDoc ejbListe, Vector JavaDoc webListe, Vector JavaDoc rarListe)
52     {
53         project = proj;
54         this.ejbListe = ejbListe;
55         this.webListe = webListe;
56         this.rarListe = rarListe;
57     }
58
59     /**
60      * Copy file into web directory, if it doesn't already exist there.
61      */

62     private void copyFileToEarDirectory( File JavaDoc f )
63     {
64         IFile xmlfile = project.getProject().getFile( f.getName() );
65         try{ FileUtil.copy( f, xmlfile.getLocation().toFile() ); }
66         catch( IOException JavaDoc 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 JavaDoc 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 JavaDoc e) {}
79             b = f.delete();
80         }
81         return b;
82     }
83
84     public void RunNewear()
85     {
86         vContext = new VelocityContext();
87
88         String JavaDoc jonasRoot = JonasLauncherPlugin.getDefault().getJonasDir();
89         String JavaDoc earName = project.getProject().getName() + ".ear";
90         
91         vContext.put("earName",earName);
92         
93         if (!webListe.isEmpty()) {
94             String JavaDoc 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 JavaDoc outputDir = new File JavaDoc(project.getProject().getLocation().toOSString());
111         
112
113         // Generates webapps files
114
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 JavaDoc 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 JavaDoc e) {
129             error(e.toString());
130         }
131         // now to refresh project so it picks up the new files
132
try{ project.getProject().refreshLocal( IResource.DEPTH_INFINITE, null ); }
133         catch( CoreException e ) {}
134
135         if (!rarListe.isEmpty()) {
136             Enumeration JavaDoc ele = rarListe.elements();
137             String JavaDoc pathRars = project.getProject().getLocation().append("/resourceAdapter").toOSString();
138             while (ele.hasMoreElements()) {
139                 try {
140                     RarManagement.buildRarFile(pathRars + "/" + ele.nextElement(),project.getProject());
141                 } catch (IOException JavaDoc 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     /**
156      * Create a new array : [ s1, s2, s3 ]
157      * (TODO move this method to StringUtil)
158      */

159     public static String JavaDoc[] concat( String JavaDoc[] s1, String JavaDoc s2, String JavaDoc s3 )
160     {
161         String JavaDoc[] full = new String JavaDoc[ 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     /**
169      * Create a new array : [ s1, s2 ]
170      * (TODO move this method to StringUtil)
171      */

172     public static String JavaDoc[] concat( String JavaDoc[] s1, String JavaDoc s2 )
173     {
174         String JavaDoc[] full = new String JavaDoc[ 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 JavaDoc errMsg) {
181         System.err.println("NewBean fatal error: " + errMsg);
182         System.exit(EXIT_FAILURE);
183     }
184
185     /**
186       * Generates a file from the specified template.
187       * @param templateFileName the name of the template file
188       * @param targetFileName the name of the generated file
189       */

190      private void generate(String JavaDoc templateFileName,
191                            String JavaDoc targetFileName) throws Exception JavaDoc, IOException JavaDoc, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
192          FileWriter JavaDoc fileWriter = null;
193          fileWriter = new FileWriter JavaDoc(targetFileName);
194          vEngine.mergeTemplate(templateFileName, vContext, fileWriter);
195          fileWriter.close();
196      }
197
198     /**
199      * Display the specified error message.
200      * @param errMsg the error message to display
201      */

202     static void error(String JavaDoc errMsg) {
203         System.err.println("NewBean error: " + errMsg);
204     }
205
206
207 }
208
Popular Tags