KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > actions > ws > CreateDeployWSDD


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

5 package com.bull.eclipse.jonas.actions.ws;
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 CreateDeployWSDD
32 {
33     //private JonasProject jonasNature;
34
private IFolder pkgfolder; // the folder in which the java files will go
35
private JonasProject project;
36     private VelocityEngine vEngine = null;
37     private VelocityContext vContext = null;
38     
39     private static final int EXIT_SUCCESS = 0;
40     private static final int EXIT_FAILURE = 1;
41
42
43     public CreateDeployWSDD( JonasProject proj)
44     {
45         project = proj;
46     }
47
48     /**
49      * Copy file into web directory, if it doesn't already exist there.
50      */

51     private void copyFileToWebAppDirectory( File JavaDoc f )
52     {
53         IFile xmlfile = project.getProject().getFile( f.getName() );
54         try{ FileUtil.copy( f, xmlfile.getLocation().toFile() ); }
55         catch( IOException JavaDoc e ) {
56             JonasLauncherPlugin.log(
57                 "JOPE: exception copying file " + f.getName()
58                 + " to project " + project.getProject().getName() + ": " + e );
59         }
60     }
61
62     private boolean myDelete( File JavaDoc f )
63     {
64         boolean b = f.delete();
65         int retryCount = 5;
66         while( !b && retryCount-->0 ) {
67             try{ java.lang.Thread.sleep(1000); } catch(Exception JavaDoc e) {}
68             b = f.delete();
69         }
70         return b;
71     }
72
73     public void runUpdate(String JavaDoc endpointName, String JavaDoc jndiName, String JavaDoc homeClass,
74                     String JavaDoc remoteClass, String JavaDoc homeLocalClass, String JavaDoc localClass, String JavaDoc packName)
75     {
76         vContext = new VelocityContext();
77
78         String JavaDoc jonasRoot = JonasLauncherPlugin.getDefault().getJonasDir();
79         
80         vContext.put("endpoint",endpointName);
81                     
82         vContext.put("jndiName", jndiName);
83         vContext.put("localHomeClassName", homeLocalClass);
84         vContext.put("localClassName", localClass);
85         vContext.put("homeClassName", homeClass);
86         vContext.put("remoteClassName", remoteClass);
87         JonasLauncherPlugin.log("Deploy = " + project.getProject().getLocation().append(project.getSrcEJBDir() + File.separator + packName ));
88         File JavaDoc outputDir = new File JavaDoc(project.getProject().getLocation().append(project.getSrcEJBDir() + File.separator + packName ).toOSString());
89         
90
91         // Generates webapps files
92
try {
93             vEngine = new VelocityEngine();
94             vEngine.setProperty(VelocityEngine.VM_LIBRARY, "");
95             vEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "file");
96             vEngine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH,
97                             TemplateDirUtil.getTemplateDir().concat("/ws"));
98             try {
99                 vEngine.init();
100             } catch (Exception JavaDoc e) {
101                 fatalError("unable to initilise Velocity engine (" + e + ")");
102             }
103             generate("deploy.wsdd", outputDir + "/deploy.wsdd");
104         } catch (Exception JavaDoc e) {
105             error(e.toString());
106         }
107         // now to refresh project so it picks up the new files
108
try{ project.getProject().refreshLocal( IResource.DEPTH_INFINITE, null ); }
109         catch( CoreException e ) {}
110                 
111     }
112
113     public void setPkgFolder( IFolder f )
114     {
115         pkgfolder = f;
116     }
117
118     /**
119      * Create a new array : [ s1, s2, s3 ]
120      * (TODO move this method to StringUtil)
121      */

122     public static String JavaDoc[] concat( String JavaDoc[] s1, String JavaDoc s2, String JavaDoc s3 )
123     {
124         String JavaDoc[] full = new String JavaDoc[ s1.length + 2 ];
125         System.arraycopy( s1, 0, full, 0, s1.length );
126         full[ full.length-2 ] = s2;
127         full[ full.length-1 ] = s3;
128         return full;
129     }
130
131     /**
132      * Create a new array : [ s1, s2 ]
133      * (TODO move this method to StringUtil)
134      */

135     public static String JavaDoc[] concat( String JavaDoc[] s1, String JavaDoc s2 )
136     {
137         String JavaDoc[] full = new String JavaDoc[ s1.length + 1 ];
138         System.arraycopy( s1, 0, full, 0, s1.length );
139         full[ full.length-1 ] = s2;
140         return full;
141     }
142
143     static void fatalError(String JavaDoc errMsg) {
144         System.err.println("NewBean fatal error: " + errMsg);
145         System.exit(EXIT_FAILURE);
146     }
147
148     /**
149       * Generates a file from the specified template.
150       * @param templateFileName the name of the template file
151       * @param targetFileName the name of the generated file
152       */

153      private void generate(String JavaDoc templateFileName,
154                            String JavaDoc targetFileName) throws Exception JavaDoc, IOException JavaDoc, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
155          FileWriter JavaDoc fileWriter = null;
156          fileWriter = new FileWriter JavaDoc(targetFileName);
157          vEngine.mergeTemplate(templateFileName, vContext, fileWriter);
158          fileWriter.close();
159      }
160
161     /**
162      * Display the specified error message.
163      * @param errMsg the error message to display
164      */

165     static void error(String JavaDoc errMsg) {
166         System.err.println("NewBean error: " + errMsg);
167     }
168
169
170 }
171
Popular Tags