KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > preferencepages > JonasDBMPreferencePage


1 package com.bull.eclipse.jonas.preferencepages;
2
3 /*
4  * (c) Copyright Bull SA 2003.
5  * All Rights Reserved.
6  */

7
8 import java.io.File JavaDoc;
9 import java.io.FileWriter JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.text.FieldPosition JavaDoc;
12 import java.text.SimpleDateFormat JavaDoc;
13 import java.util.Date JavaDoc;
14
15 import org.apache.velocity.VelocityContext;
16 import org.apache.velocity.app.VelocityEngine;
17 import org.apache.velocity.exception.MethodInvocationException;
18 import org.apache.velocity.exception.ParseErrorException;
19 import org.apache.velocity.exception.ResourceNotFoundException;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.jdt.internal.debug.ui.launcher.ComboFieldEditor;
22 import org.eclipse.jface.preference.BooleanFieldEditor;
23 import org.eclipse.jface.preference.FieldEditor;
24 import org.eclipse.jface.preference.FileFieldEditor;
25 import org.eclipse.jface.preference.PreferencePage;
26 import org.eclipse.jface.preference.StringFieldEditor;
27 import org.eclipse.jface.util.IPropertyChangeListener;
28 import org.eclipse.jface.util.PropertyChangeEvent;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Control;
34 import org.eclipse.ui.IWorkbench;
35 import org.eclipse.ui.IWorkbenchPreferencePage;
36
37 import com.bull.eclipse.jonas.JonasLauncherPlugin;
38 import com.bull.eclipse.jonas.JonasPluginResources;
39 import com.bull.eclipse.jonas.utils.FileUtil;
40 import com.bull.eclipse.jonas.utils.TemplateDirUtil;
41 import com.bull.eclipse.jonas.utils.Zipper;
42
43
44 public class JonasDBMPreferencePage extends PreferencePage implements IWorkbenchPreferencePage, JonasPluginResources {
45
46     static final private int FIELD_WIDTH = 50;
47     static final private String JavaDoc[][] mapperListe = new String JavaDoc[7][2];
48     static final private String JavaDoc[][] urldriverListe = new String JavaDoc[7][2];
49
50     private static final int TEXT_FIELD_WIDTH = 200;
51
52     private ComboFieldEditor databaseChoice;
53     private StringFieldEditor urlDBM;
54     private StringFieldEditor driverDBM;
55     private StringFieldEditor user;
56     private StringFieldEditor password;
57     private StringFieldEditor datasource;
58     private StringFieldEditor rdbmapper;
59     private StringFieldEditor schema;
60     private StringFieldEditor catalog;
61     private FileFieldEditor jdbcDriver;
62     private BooleanFieldEditor automaticDeploy;
63     
64     private VelocityEngine vEngine = null;
65     private VelocityContext vContext = null;
66     private static final int EXIT_SUCCESS = 0;
67     private static final int EXIT_FAILURE = 1;
68
69
70     
71     public JonasDBMPreferencePage() {
72         super();
73         setPreferenceStore(JonasLauncherPlugin.getDefault().getPreferenceStore());
74         
75         mapperListe[0][0] = "DB2";
76         mapperListe[0][1] = "rdb.db2";
77         mapperListe[1][0] = "Oracle 9";
78         mapperListe[1][1] = "rdb.oracle";
79         mapperListe[2][0] = "Default";
80         mapperListe[2][1] = "rdb";
81         mapperListe[3][0] = "McKoi";
82         mapperListe[3][1] = "rdb.mckoi";
83         mapperListe[4][0] = "Postgres";
84         mapperListe[4][1] = "rdb.postgres";
85         mapperListe[5][0] = "Oracle 8";
86         mapperListe[5][1] = "rdb.oracle8";
87         mapperListe[6][0] = "MySQL";
88         mapperListe[6][1] = "rdb.mysql";
89         
90
91         urldriverListe[0][0] = "url:db2:";
92         urldriverListe[0][1] = "driver DB2";
93         urldriverListe[1][0] = "jdbc:oracle:thin:@<your-hostname>:1521:<your-db>";
94         urldriverListe[1][1] = "oracle.jdbc.driver.OracleDriver";
95         urldriverListe[2][0] = "jdbc:url";
96         urldriverListe[2][1] = "Driver";
97         urldriverListe[3][0] = "jdbc:mckoi://localhost";
98         urldriverListe[3][1] = "com.mckoi.JDBCDriver";
99         urldriverListe[4][0] = "jdbc:postgresql://localhost:5432/db_jonas";
100         urldriverListe[4][1] = "org.postgresql.Driver";
101         urldriverListe[5][0] = "jdbc:oracle:thin:@<your-hostname>:1521:<your-db>";
102         urldriverListe[5][1] = "oracle.jdbc.driver.OracleDriver";
103         urldriverListe[6][0] = "jdbc:mysql://<your-hostname>/<your-db>";
104         urldriverListe[6][1] = "com.mysql.jdbc.Driver";
105
106         
107 // setDescription("");
108
}
109
110     /*
111      * @see PreferencePage#createContents(Composite)
112      */

113     protected Control createContents(Composite parent) {
114         Composite composite = new Composite(parent, SWT.NULL);
115         GridLayout gl = new GridLayout();
116         gl.numColumns = 1;
117         composite.setLayout(gl);
118         GridData gd = new GridData(GridData.FILL_BOTH);
119         gd.grabExcessVerticalSpace = true;
120         gd.grabExcessHorizontalSpace = true;
121         composite.setLayoutData(gd);
122                 
123         Composite group1 = new Composite(composite,SWT.NONE);
124         GridLayout gl1 = new GridLayout(3,false);
125         group1.setLayout(gl1);
126         gd = new GridData();
127         gd.horizontalAlignment = GridData.FILL;
128         group1.setLayoutData(gd);
129
130         Composite group2 = new Composite(composite,SWT.NONE);
131         GridLayout gl2 = new GridLayout(3,false);
132         group1.setLayout(gl2);
133         gd = new GridData();
134         gd.horizontalAlignment = GridData.FILL;
135         group2.setLayoutData(gd);
136
137         Composite group3 = new Composite(composite,SWT.NONE);
138         GridLayout gl3 = new GridLayout(3,false);
139         group1.setLayout(gl3);
140         gd = new GridData();
141         gd.horizontalAlignment = GridData.FILL;
142         group3.setLayoutData(gd);
143
144         databaseChoice = new ComboFieldEditor(
145                 JonasLauncherPlugin.JONAS_PREF_DATABASE_KEY,
146                 PREF_PAGE_DATABASE_LABEL,
147                 mapperListe,
148                 group1);
149
150
151         urlDBM = new StringFieldEditor("url",URL_DBM_LABEL,60,group1);
152         driverDBM = new StringFieldEditor("driver",DRIVER_DBM_LABEL,60,group1);
153         user = new StringFieldEditor("user",USER_DBM_LABEL,60,group1);
154         password = new StringFieldEditor("password",PASSWORD_DBM_LABEL,60,group1);
155         datasource = new StringFieldEditor("datasource",DATASOURCE_DBM_LABEL,60,group1);
156         rdbmapper = new StringFieldEditor("rdbmapper",RDBMAPPER_DBM_LABEL,60,group1);
157         schema = new StringFieldEditor("schema",SCHEMA_DBM_LABEL,60,group1);
158         catalog = new StringFieldEditor("catalog",CATALOG_DBM_LABEL,60,group1);
159         jdbcDriver = new FileFieldEditor("jdbcDriver",
160                                 JDBC_DBM_LABEL,
161                                 group2);
162
163         automaticDeploy = new BooleanFieldEditor(
164                 JonasLauncherPlugin.JONAS_PREF_AUTOMATICDEPLOY_KEY,
165                 PREF_PAGE_AUTOMATICDEPLOY_LABEL,
166                 group3);
167
168         databaseChoice.setPropertyChangeListener(new IPropertyChangeListener() {
169             public void propertyChange(PropertyChangeEvent event) {
170                 String JavaDoc newValue = (String JavaDoc)event.getNewValue();
171                 for(int i = 0;i<mapperListe.length;i++) {
172                     if (newValue.equals(mapperListe[i][1])) {
173                         rdbmapper.setStringValue(newValue);
174                         urlDBM.setStringValue(urldriverListe[i][0]);
175                         driverDBM.setStringValue(urldriverListe[i][1]);
176                         break;
177                     }
178                 }
179                 }
180             }
181         );
182         
183         this.initField(databaseChoice);
184         this.initField(urlDBM);
185         this.initField(driverDBM);
186         this.initField(user);
187         this.initField(password);
188         this.initField(datasource);
189         this.initField(rdbmapper);
190         this.initField(schema);
191         this.initField(catalog);
192         this.initField(jdbcDriver);
193         this.initField(automaticDeploy);
194         
195         return composite;
196     }
197
198     /*
199      * @see IWorkbenchPreferencePage#init(IWorkbench)
200      */

201     public void init(IWorkbench workbench) {
202     }
203
204
205     public boolean performOk() {
206         databaseChoice.store();
207         urlDBM.store();
208         driverDBM.store();
209         user.store();
210         password.store();
211         datasource.store();
212         rdbmapper.store();
213         schema.store();
214         catalog.store();
215         jdbcDriver.store();
216         automaticDeploy.store();
217         
218         JonasLauncherPlugin.getDefault().savePluginPreferences();
219         if ((datasource.getStringValue() != null) && (!datasource.getStringValue().equals("")))
220             runNewds();
221         // Add the RA jdbc rar file in JONAS_BASE autoload
222

223         return true;
224     }
225     
226     private void initField(FieldEditor field) {
227         field.setPreferenceStore(getPreferenceStore());
228         field.setPreferencePage(this);
229         field.load();
230     }
231
232
233
234     private void runNewds()
235     {
236         String JavaDoc jonasRoot = JonasLauncherPlugin.getDefault().getJonasDir();
237         String JavaDoc tmp = System.getProperty("java.io.tmpdir");
238         SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc("yyyyMMdd_HHmmssSSS");
239         StringBuffer JavaDoc datim = df.format( new Date JavaDoc(), new StringBuffer JavaDoc(), new FieldPosition JavaDoc(0) );
240         File JavaDoc tmpdir = new File JavaDoc( tmp + "/JOPE_" + datim);
241         boolean iscreate = tmpdir.mkdir();
242         if( !iscreate ) {
243             JonasLauncherPlugin.log("JOPE: unable to create temp dir: "+tmpdir);
244             return;
245         }
246         File JavaDoc tmpdirMETA = new File JavaDoc(tmpdir,"META-INF");
247         iscreate = tmpdirMETA.mkdir();
248         if( !iscreate ) {
249             JonasLauncherPlugin.log("JOPE: unable to create temp META dir: "+tmpdir);
250             return;
251         }
252
253         vContext = new VelocityContext();
254
255         vContext.put("url", urlDBM.getStringValue() );
256         vContext.put("datasource", datasource.getStringValue() );
257         vContext.put("driver", driverDBM.getStringValue() );
258         vContext.put("user", user.getStringValue() );
259         vContext.put("password", password.getStringValue() );
260         vContext.put("mapper", rdbmapper.getStringValue() );
261         
262         vEngine = new VelocityEngine();
263         vEngine.setProperty(VelocityEngine.VM_LIBRARY, "");
264         vEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "file");
265         vEngine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH,
266                 TemplateDirUtil.getTemplateDir());
267
268         try {
269             vEngine.init();
270         } catch (Exception JavaDoc e) {
271             fatalError("unable to initilise Velocity engine (" + e + ")");
272         }
273         
274         // Generates bean files
275
try {
276             generate("jonas-ra.vm", tmpdirMETA.getAbsolutePath() + "/jonas-ra.xml");
277         } catch (Exception JavaDoc e) {
278             error(e.toString());
279         }
280
281         
282         try {
283             File JavaDoc jarFile = new File JavaDoc(tmpdir,"ow_jonas_jdbc_ra.jar");
284             File JavaDoc raFile = new File JavaDoc(tmpdirMETA, "ra.xml");
285             File JavaDoc jdbcdriverFile = null;
286             JonasLauncherPlugin.log("jdbcDriver = " + jdbcDriver.getStringValue());
287             if ((jdbcDriver.getStringValue() != null) && (!jdbcDriver.getStringValue().equals("")))
288                 jdbcdriverFile = new File JavaDoc(jdbcDriver.getStringValue());
289             File JavaDoc jdbcdriver = new File JavaDoc(tmpdir,"jdbc_driver.jar");
290             FileUtil.copy(new File JavaDoc(JonasLauncherPlugin.getDefault().getJonasDir().concat("/lib/common/ow_jonas_jdbc_ra.jar")), jarFile);
291             FileUtil.copy(new File JavaDoc(TemplateDirUtil.getTemplateDir().concat("/ra.xml")), raFile);
292             if (jdbcdriverFile != null)
293                 FileUtil.copy(jdbcdriverFile, jdbcdriver);
294             String JavaDoc ds = datasource.getStringValue().replaceAll("/","");
295             Zipper zipRar = new Zipper(new File JavaDoc(JonasLauncherPlugin.getDefault().getBaseDir().concat("/rars/JOPE_" + ds + ".rar")),tmpdir);
296             zipRar.zip();
297             jarFile.delete();
298             raFile.delete();
299             jdbcdriver.delete();
300             (new File JavaDoc(tmpdirMETA,"jonas-ra.xml")).delete();
301             
302             // automatic deployment
303
if (automaticDeploy.getBooleanValue()) {
304                 try {
305                     String JavaDoc[] options = {"-a", "JOPE_" + datasource.getStringValue() + ".rar"};
306                     JonasLauncherPlugin.getDefault().getJonasBootstrap().admin(options);
307                 } catch (CoreException ce) {
308                     JonasLauncherPlugin.log("DS deploy problem ");
309                     ce.printStackTrace();
310                 }
311             }
312         } catch (IOException JavaDoc ioe) {
313             JonasLauncherPlugin.log( "JOPE: " + ioe.toString() );
314         }
315         
316     }
317
318     /**
319      * Generates a file from the specified template.
320      * @param templateFileName the name of the template file
321      * @param targetFileName the name of the generated file
322      */

323     private void generate(String JavaDoc templateFileName,
324                           String JavaDoc targetFileName) throws Exception JavaDoc, IOException JavaDoc, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
325         FileWriter JavaDoc fileWriter = null;
326         fileWriter = new FileWriter JavaDoc(targetFileName);
327         vEngine.mergeTemplate(templateFileName, vContext, fileWriter);
328         fileWriter.close();
329     }
330
331
332     /**
333      * Create a new array : [ s1, s2, s3 ]
334      * (TODO move this method to StringUtil)
335      */

336     private static String JavaDoc[] concat( String JavaDoc[] s1, String JavaDoc s2, String JavaDoc s3 )
337     {
338         String JavaDoc[] full = new String JavaDoc[ s1.length + 2 ];
339         System.arraycopy( s1, 0, full, 0, s1.length );
340         full[ full.length-2 ] = s2;
341         full[ full.length-1 ] = s3;
342         return full;
343     }
344
345     /**
346      * Create a new array : [ s1, s2 ]
347      * (TODO move this method to StringUtil)
348      */

349     private static String JavaDoc[] concat( String JavaDoc[] s1, String JavaDoc s2 )
350     {
351         String JavaDoc[] full = new String JavaDoc[ s1.length + 1 ];
352         System.arraycopy( s1, 0, full, 0, s1.length );
353         full[ full.length-1 ] = s2;
354         return full;
355     }
356
357     /**
358      * Display the specified error message.
359      * @param errMsg the error message to display
360      */

361     static void error(String JavaDoc errMsg) {
362         System.err.println("NewBean error: " + errMsg);
363     }
364
365
366     /**
367      * Display the specified error message and exits with an
368      * EXIT_FAILURE status.
369      * @param errMsg the error message to display
370      */

371     static void fatalError(String JavaDoc errMsg) {
372         System.err.println("NewBean fatal error: " + errMsg);
373         System.exit(EXIT_FAILURE);
374     }
375
376     
377 }
378
379
Popular Tags