KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > newwebapp > NewWebappsRunner


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

5 package com.bull.eclipse.newwebapp;
6
7 import java.io.File JavaDoc;
8 import java.io.FileWriter JavaDoc;
9 import java.io.IOException JavaDoc;
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 /**
27  * Interface with the JOnAS 'newbean' functionality.
28  * @author steve krupanszky
29  *
30  */

31 public class NewWebappsRunner
32 {
33     private static final int EXIT_SUCCESS = 0;
34     private static final int EXIT_FAILURE = 1;
35
36     //private JonasProject jonasNature;
37
private IFolder pkgfolder; // the folder in which the java files will go
38
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     /**
50      * Copy file into web directory, if it doesn't already exist there.
51      */

52     private void copyFileToWebDirectory( File JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc e) {}
78             b = f.delete();
79         }
80         return b;
81     }
82
83     public void RunNewwebapps()
84     {
85         String JavaDoc jonasRoot = JonasLauncherPlugin.getDefault().getJonasDir();
86
87         vContext = new VelocityContext();
88         String JavaDoc 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 JavaDoc 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 JavaDoc e) {
112             fatalError("unable to initilise Velocity engine (" + e + ")");
113         }
114         
115         File JavaDoc outputDir = new File JavaDoc(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 JavaDoc e) {
121             e.printStackTrace();
122         }
123
124         
125
126         // now to refresh project so it picks up the new files
127
try{ project.getProject().refreshLocal( IResource.DEPTH_INFINITE, null ); }
128         catch( CoreException e ) {
129             e.printStackTrace();
130         }
131
132     }
133
134 /*
135     public void setNature( JonasProject nature )
136     {
137         jonasNature = nature;
138     }
139 */

140     public void setPkgFolder( IFolder f )
141     {
142         pkgfolder = f;
143     }
144
145     /**
146      * Create a new array : [ s1, s2, s3 ]
147      * (TODO move this method to StringUtil)
148      */

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

162     public static String JavaDoc[] concat( String JavaDoc[] s1, String JavaDoc s2 )
163     {
164         String JavaDoc[] full = new String JavaDoc[ s1.length + 1 ];
165         System.arraycopy( s1, 0, full, 0, s1.length );
166         full[ full.length-1 ] = s2;
167         return full;
168     }
169
170     /**
171      * Display the specified error message and exits with an
172      * EXIT_FAILURE status.
173      * @param errMsg the error message to display
174      */

175     static void fatalError(String JavaDoc errMsg) {
176         System.err.println("NewWebapps fatal error: " + errMsg);
177         System.exit(EXIT_FAILURE);
178     }
179
180     /**
181      * Generates a file from the specified template.
182      * @param templateFileName the name of the template file
183      * @param targetFileName the name of the generated file
184      */

185     private void generate(String JavaDoc templateFileName,
186                           String JavaDoc targetFileName) throws Exception JavaDoc, IOException JavaDoc, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
187         FileWriter JavaDoc fileWriter = null;
188         fileWriter = new FileWriter JavaDoc(targetFileName);
189         vEngine.mergeTemplate(templateFileName, vContext, fileWriter);
190         fileWriter.close();
191     }
192
193 }
194
Popular Tags